-
-
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
Honor return type of __new__
#7188
Conversation
This basically follows the approach Jukka laid out in #1020 four years ago: * If the return type is Any, ignore that and keep the class type as the return type * Otherwise respect `__new__`'s return type * Produce an error if the return type is not a subtype of the class. The main motivation for me in implementing this is to support overloading `__new__` in order to select type variable arguments, which will be useful for subprocess.Popen. Fixes #1020.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Generally looks good, but there is one suspicious place.
How hard would be to support a similar feature for __init__
? (Like in my comment on typeshed.) Essentially, people often implement such logic (type argument specification) in __init__
, see an important example #4236 (default value for type variable).
mypy/checkmember.py
Outdated
"""Create a type object type based on the signature of __init__.""" | ||
variables = [] # type: List[TypeVarDef] | ||
variables.extend(info.defn.type_vars) | ||
variables.extend(init_type.variables) | ||
|
||
is_new = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks suspicious for two reasons:
- You unconditionally override an argument with an opposite default.
- Just below it appears in an
if
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh hm. That was testing code that survived because it works without it, since NoneTyp won't trigger the isinstance check...
This basically follows the approach Jukka laid out in #1020 four years ago:
the return type
__new__
's return typeThe main motivation for me in implementing this is to support
overloading
__new__
in order to select type variable arguments,which will be useful for subprocess.Popen.
Fixes #1020.