-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
class decorators that change the type don't work #11117
Comments
Are there any plans for fixing this? I'm using a class decorator that is a class itself (thereby transforming the original class into an instance of the decorator), and running into issues where mypy is inferring the wrong type. Here is a simplified example: from typing import Generic, Type, TypeVar
T = TypeVar("T")
class Deco(Generic[T]):
def __init__(self, cls: Type[T]):
self._cls = cls
def get(self) -> T:
return self._cls()
@Deco
class Klass:
...
reveal_type(Deco) # note: Revealed type is 'def [T] (cls: Type[T`1]) -> __main__.Deco[T`1]'
reveal_type(Deco.get) # note: Revealed type is 'def [T] (self: __main__.Deco[T`1]) -> T`1'
reveal_type(Klass) # note: Revealed type is 'def () -> __main__.Klass'
reveal_type(Klass.get) # error: "Type[Klass]" has no attribute "get" |
Duplicate of #3135 |
@JelleZijlstra that looks like a different issue that's since been fixed, since mypy now shows an error on invalid class decorators: decorator: int = 3
@decorator # error: "int" not callable [operator]
class A:
pass my issue is that class decorators aren't able to change the type of the class they're decorating: def decorator(cls: type[object]) -> int: ...
@decorator
class A:
pass
reveal_type(A) # `def () -> __main__.A`, should be `int` so i suggest either closing the original issue and re-opening this one, or updating the original issue with a new example (and maybe change its title) |
same issue, does someone knows how to fix it? |
https://mypy-play.net/?mypy=latest&python=3.10&gist=705baa296d3181f0b670416a405978b4
The text was updated successfully, but these errors were encountered: