Repository navigation
Relax overload checks to allow return types to be regular subtypes - #5064
Merged
ilevkivskyi merged 1 commit intoMay 16, 2018
Merged
Conversation
This pull request relaxes the overlapping overload checks so that if the
parameter types of an alternative are a proper subtype of another, the
return type needs to only be a regular subtype, not a proper subtype.
The net effect is that the return type of the first alternative is
allowed to be 'Any'.
This *does* make overloads slightly less safe, but 'Any' was always
meant to be an escape hatch, so I'm figuring this is fine.
**Rationale**: My [proposed changes on overhauling overloads][0] require
a few changes to typeshed -- [relevant pull request here][1]. Rather then
trying to change both at the same time, I want to get typeshed working
first.
This was pretty easy to do, apart from the attrs stubs. The issue
basically boils down to this:
# Alternative 1
@overload
def attrib(x: Optional[T]) -> T: ...
# Alternative 2
@overload
def attrib(x: None = None) -> Any: ...
This code typechecks under the current system because alternative 1
completely shadows alternative 2. It fails to typecheck under the new
system for the exact same reason.
If we swap the two alternatives, it fails under the current system
because 'Any' is not a proper subtype of 'T'.
It *is*, however, regular subtype of 'T' -- hence this change.
[0]: python/typing#253 (comment)
[1]: python/typeshed#2138
ilevkivskyi
approved these changes
May 16, 2018
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request relaxes the overlapping overload checks so that if the parameter types of an alternative are a proper subtype of another, the return type needs to only be a regular subtype, not a proper subtype.
The net effect is that the return type of the first alternative is allowed to be 'Any'.
This does make overloads slightly less safe, but 'Any' was always meant to be an escape hatch, so I'm figuring this is fine.
Rationale: My proposed changes on overhauling overloads require a few changes to typeshed -- relevant pull request here. Rather then trying to change both at the same time, I want to get typeshed working first.
This was pretty easy to do, apart from the attrs stubs. The issue basically boils down to this:
This code typechecks under the current system because alternative 1 completely shadows alternative 2. It fails to typecheck under the new system for the exact same reason.
If we swap the two alternatives, it fails under the current system because 'Any' is not a proper subtype of 'T'.
It is, however, regular subtype of 'T' -- hence this change.