You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Guido and I found out about this in python/typeshed#1136 (comment). Looks like this can be fixed somewhere around analyze_class_decorator in semanal.py.
I'm thinking how this could/should be used in combination with Protocols to deal with class decorators adding some methods to a class
class FooBar(Protocol):
def bar(self) -> int:
return 1
T = TypeVar("T", bound=Type)
def add_bar(cls: T) -> Intersection[FooBar, T]:
def bar(self) -> int:
return 2
cls.bar = bar
@add_bar
class Foo: pass
Foo().bar()
Now this depends on Intersection which was described in PEP483, but not implemented; I don't see another way of doing this cleanly right now.
Reacted by Yasin Bahtiyar, Glyph, Kiran Bhattaram, Albin Skott, Vito De Tullio, Stevan Milic, Max Daniline, Patrick Doran, Ryan Morshead, Arne Küderle and 8 more
It looks like some of these examples (e.g. the x=17 example above) may have been fixed
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)
Reacted by Erez Shinan, Neil Girdhar, Robert-Jan Huijsman and Edgar Ramírez Mondragón
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:
Mypy does not yet recognize aliases of dataclasses.dataclass, and will probably never recognize dynamically computed decorators.
It would also obsolete the hacky and limited typing_extensions.dataclass_transform.
I don't think it would. The dataclass_transform marks that a class has fields made with field-makers. Whereas an annotated decorator can tell the type checker that a class can be passed to dataclasses functions like fields and replace.
These are separate concepts. You can have classes with fields that can't be passed to dataclass functions.
Activity
JelleZijlstra commentedon Apr 5, 2017
Guido and I found out about this in python/typeshed#1136 (comment). Looks like this can be fixed somewhere around
analyze_class_decoratorin semanal.py.ilevkivskyi commentedon Jun 1, 2017
A use case reported in #3483
might be non-trivial to implement. I am adding this example here, so that we will not forget about it.
euresti commentedon Feb 3, 2018
Another possibly easier to solve problem is that the functions called are not type-checked
Or even more fun
Simple type checking of class decorators
Type checking of class decorators (#4544)
reinhrst commentedon May 12, 2018
I'm thinking how this could/should be used in combination with
Protocols to deal with class decorators adding some methods to a classNow this depends on
Intersectionwhich was described in PEP483, but not implemented; I don't see another way of doing this cleanly right now.erezsh commentedon Jul 15, 2022
Are there plans to fix this? It's strange that mypy doesn't support a core syntax.
rileyjohngibbs commentedon Nov 11, 2022
It looks like some of these examples (e.g. the
x=17example above) may have been fixed, so in the interest of having a current, non-working example:The error for the final line is:
error: Unsupported operand types for + ("Type[Foo]" and "int") [operator].Function decorators seem to work fine though:
DetachHead commentedon Oct 10, 2023
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)
erezsh commentedon Oct 10, 2023
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
typing_extensions.dataclass_transform.Just my two (hopefully correct) cents.
NeilGirdhar commentedon Oct 10, 2023
I don't think it would. The
dataclass_transformmarks that a class has fields made with field-makers. Whereas an annotated decorator can tell the type checker that a class can be passed todataclassesfunctions likefieldsandreplace.These are separate concepts. You can have classes with fields that can't be passed to dataclass functions.
CaselIT commentedon Jan 27, 2026
This is really an annoying issue. I planned on adding support for typed tables in sqlalchemy sqlalchemy/sqlalchemy#13085
but this issue makes the decorator form non functional.
For reference in pyright it works perfectly