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

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

Merged
merged 1 commit into from May 16, 2018

Conversation

Projects
None yet
2 participants
@Michael0x2a
Collaborator

Michael0x2a commented May 16, 2018

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.

Relax overload checks to allow return types to be regular subtypes
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

@ilevkivskyi ilevkivskyi merged commit 74371df into python:master May 16, 2018

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@Michael0x2a Michael0x2a deleted the Michael0x2a:relax-overlapping-overload-checks branch Jul 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment