Skip to content
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

Merged
merged 2 commits into from
Jul 11, 2019
Merged

Honor return type of __new__ #7188

merged 2 commits into from
Jul 11, 2019

Conversation

msullivan
Copy link
Collaborator

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.

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.
Copy link
Member

@ilevkivskyi ilevkivskyi left a 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).

"""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
Copy link
Member

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

Copy link
Collaborator Author

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Honor return type of __new__
2 participants