Skip to content

Relax overload checks to allow return types to be regular subtypes #5064

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

Merged

Conversation

Michael0x2a
Copy link
Collaborator

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:

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

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
@Michael0x2a Michael0x2a requested a review from ilevkivskyi May 16, 2018 20:42
@ilevkivskyi ilevkivskyi merged commit 74371df into python:master May 16, 2018
@Michael0x2a Michael0x2a deleted the relax-overlapping-overload-checks branch July 9, 2018 04:18
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.

2 participants