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
The type for super().__new__(cls, value) is incorrectly inferred when used outside of __new__.
In the example below I used super().__new__(cls, value) in __init_subclass__ and the inferred type is the base class that defines __init_subclass__ instead of being Self. At the same time within __new__, the same expression is correctly inferred as Self.
To Reproduce
fromtypingimportClassVar, Self, reveal_typeclassLiteralBytes(bytes):
_instance_: ClassVar[Self] =NotImplementeddef__init_subclass__(cls, *, value: bytes=NotImplemented, **kw: object) ->None:
ifvalueisnotNotImplemented:
cls._instance_=reveal_type(super().__new__(cls, value))
super().__init_subclass__(**kw)
def__new__(cls) ->Self:
reveal_type(super().__new__(cls, b'')) # this is here just to showcase the difference in the inferred typeifcls._instance_isNotImplemented:
raiseTypeError(f'Cannot instantiate abstract literal bytes type {cls.__qualname__!r} that does not define its value')
returncls._instance_
The sample program above is also available here: playground link
Expected Behavior
I expect super().__new__(cls, ...) to always be inferred as Self no matter in which method is called as that expression always produces an instance of cls (as long as cls points to the class, i.e. we are in a class method, inside __new__, ...)
Actual Behavior
main.py:8: error: Incompatible types in assignment (expression has type "LiteralBytes", variable has type "Self") [assignment]
main.py:8: note: Revealed type is "__main__.LiteralBytes"
main.py:12: note: Revealed type is "Self`0"
Found 1 error in 1 file (checked 1 source file)
Your Environment
Mypy version used: mypy 1.12.0+dev.6aa46f097cf30f3143c5ee4bae7f0d2e7ce914bc (compiled: no)
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files):
Bug Report
The type for
super().__new__(cls, value)
is incorrectly inferred when used outside of__new__
.In the example below I used
super().__new__(cls, value)
in__init_subclass__
and the inferred type is the base class that defines__init_subclass__
instead of beingSelf
. At the same time within__new__
, the same expression is correctly inferred asSelf
.To Reproduce
The sample program above is also available here: playground link
Expected Behavior
I expect
super().__new__(cls, ...)
to always be inferred asSelf
no matter in which method is called as that expression always produces an instance ofcls
(as long ascls
points to the class, i.e. we are in a class method, inside__new__
, ...)Actual Behavior
Your Environment
mypy 1.12.0+dev.6aa46f097cf30f3143c5ee4bae7f0d2e7ce914bc (compiled: no)
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: