-
-
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 are not type checked #3135
Comments
Guido and I found out about this in python/typeshed#1136 (comment). Looks like this can be fixed somewhere around |
A use case reported in #3483 from typing import Type
class A:
pass
def deco(cls) -> Type[A]:
return A
@deco
class B:
pass
def func(cls: Type[A]):
assert cls is A
func(B) might be non-trivial to implement. I am adding this example here, so that we will not forget about it. |
Another possibly easier to solve problem is that the functions called are not type-checked
Or even more fun
|
This does the bare minimum, it checks that the calls are well-type. It does not check whether applying the decorator makes sense. Helps with python#3135
It checks that the calls are well-type and that applying the decorators works. It does not check/apply the end result. Helps with #3135
I'm thinking how this could/should be used in combination with
Now this depends on |
Are there plans to fix this? It's strange that mypy doesn't support a core syntax. |
It looks like some of these examples (e.g. the from typing import Any
def foo(_: Any) -> int:
return 5
@foo
class Foo:
pass
def test_foo() -> None:
assert Foo + 7 == 12 # Does not pass typechecking The error for the final line is: Function decorators seem to work fine though: def qux(func: Callable[[], str]) -> Callable[[], int]:
def wrap() -> int:
return int(func())
return wrap
@qux
def five() -> str:
return "5"
def test_qux() -> None:
assert five() + 10 == 15 # Passes typechecking |
yeah, class decorators are now typechecked, but the issue now is that they are unable to change the type of the class they're decorating. see #11117 (comment) |
Imho, this is important functionality. For example, it could be useful for implementers of alternative dataclasses, such as pydantic or sqlmodel. Especially once support for intersection gets into mypy. I believe it would be much better than the current approach, that states:
(taken from https://mypy.readthedocs.io/en/stable/additional_features.html) It would also obsolete the hacky and limited Just my two (hopefully correct) cents. |
I don't think it would. The These are separate concepts. You can have classes with fields that can't be passed to dataclass functions. |
Mypy doesn't appear to do any checking of class decorators. Applying the decorator manually works.
The text was updated successfully, but these errors were encountered: