-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MyPy errors when using sync objects #491
Comments
Yeah I can reproduce that. I also see a similar error of I try and assign to a typed variable. from kr8s.objects import Pod
pods: list[Pod] = Pod.list() $ mypy test.py
test.py:3: error: Incompatible types in assignment (expression has type "Coroutine[Any, Any, APIObject | list[APIObject]]", variable has type "list[Pod]") [assignment]
Found 1 error in 1 file (checked 1 source file) I think there are two potential solutions here:
|
A third option could be to do away with the # kr8s/objects.py
from kr8s._objects import Pod as _Pod
from kr8s._async_utils import run_sync
class Pod(_Pod):
"""..."""
...
@classmethod
def list(cls, **kwargs) -> APIObject | list[APIObject]:
"""List objects in Kubernetes.
Args:
**kwargs: Keyword arguments to pass to :func:`kr8s.get`.
Returns:
A list of objects.
"""
return run_sync(super().list)(**kwargs)
... The upsides of this are:
But the downsides are:
|
I've tried several approaches, but none seem to work for the first option. The third option is also problematic because:
There doesn't seem to be an easy fix |
Thanks for taking the time to dig into this. I did think more about option 1 and I guess it's problematic because even if we write a plugin specifically for kr8s, all code that depends on kr8s would need the plugin, which is not something we can ask users to do. I agree for option 3 it would be best to return For option 2 I wonder if we need some custom stub generation script that we can invoke as part of the pre-commit hooks. This way we can explicitly tell mypy what to expect, but we don't need to worry about drift if they are auto-generated. |
Which project are you reporting a bug for?
kr8s
What happened?
It seems that MyPy continues to interpret methods as asynchronous for sync objects, even though at runtime they have been converted to synchronous methods with the
sync
decorator. This leads to MyPy errors like:with code like:
Anything else?
No response
The text was updated successfully, but these errors were encountered: