I'd like to request that the TC consider adoption of a new chapter in the typing spec that spells out type checking behaviors related to constructor calls.
Links to PR & Discussion
The PR can be found here. The latest draft incorporates feedback from PR reviews and the discussion.
Current Type Checker Behaviors
None of the type checkers fully comply with the proposed behavior.
I haven't written a full test suite, but I created a partial suite to test current behaviors. Here are the results.
Area
mypy
pyright
pyre
pytype
Honors metaclass __call__
no
yes
yes
no
__new__ basic specialization
yes
yes
yes
partial
Honors __new__ return type
no
partial
no
no
Skips __init__ based on __new__ return type
no
yes
no
no
Validates binding of __new__ cls
no
partial
no
no
__init__ basic specialization
yes
yes
yes
partial
Validates binding of __init__ self
no
partial
no
no
Honors __init__ self annotation (basic)
partial
yes
yes
no
Honors __init__ self annotation (func TypeVar)
no
no
yes
no
Rejects __init__ self annotation (class TypeVar)
no
no
no
no
Constructor specialization defaults
partial
yes
no
no
Converting constructor to callable
partial
partial
partial
partial
Controversial Issues
~There is one potentially controversial issue related to the handling of a metaclass __call__ that has a return type (implicit or explicit) of Any or a union that includes Any. If you're interested in the details, refer to the discussion thread.~
EDIT: We've resolved this issue through a compromise suggested by Rebecca. This is reflected in the latest draft.
I'd like to request that the TC consider adoption of a new chapter in the typing spec that spells out type checking behaviors related to constructor calls.
Links to PR & Discussion The PR can be found here. The latest draft incorporates feedback from PR reviews and the discussion.
The discussion can be found here.
TC Sign-off
Current Type Checker Behaviors None of the type checkers fully comply with the proposed behavior.
I haven't written a full test suite, but I created a partial suite to test current behaviors. Here are the results.
__call__
__new__
basic specialization__new__
return type__init__
based on__new__
return type__new__
cls__init__
basic specialization__init__
self__init__
self annotation (basic)__init__
self annotation (func TypeVar)__init__
self annotation (class TypeVar)Controversial Issues ~There is one potentially controversial issue related to the handling of a metaclass
__call__
that has a return type (implicit or explicit) ofAny
or a union that includesAny
. If you're interested in the details, refer to the discussion thread.~EDIT: We've resolved this issue through a compromise suggested by Rebecca. This is reflected in the latest draft.