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

Conversation

@msullivan
Copy link
Collaborator

commented Jul 10, 2019

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.

@msullivan msullivan requested review from JukkaL and ilevkivskyi Jul 10, 2019
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.
@msullivan msullivan force-pushed the __new__ branch from dfbfad2 to f291d54 Jul 10, 2019
Copy link
Collaborator

left a comment

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

This comment has been minimized.

Copy link
@ilevkivskyi

ilevkivskyi Jul 10, 2019

Collaborator

This looks suspicious for two reasons:

  • You unconditionally override an argument with an opposite default.
  • Just below it appears in an if

This comment has been minimized.

Copy link
@msullivan

msullivan Jul 10, 2019

Author Collaborator

Oh hm. That was testing code that survived because it works without it, since NoneTyp won't trigger the isinstance check...

@msullivan msullivan force-pushed the __new__ branch from 14a92c9 to 9c2cc84 Jul 10, 2019
@msullivan msullivan merged commit c9c071b into master Jul 11, 2019
2 checks passed
2 checks passed
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@msullivan msullivan deleted the __new__ branch Jul 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.