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

allow overloads to be distinguished by contained type of a generic #4159

Closed
carljm opened this issue Oct 25, 2017 · 6 comments
Closed

allow overloads to be distinguished by contained type of a generic #4159

carljm opened this issue Oct 25, 2017 · 6 comments

Comments

@carljm
Copy link
Member

carljm commented Oct 25, 2017

This is a follow-up to the discussion beginning at python/typing#253 (comment)

Mypy's choice to not distinguish overload signatures by contained types rules out some natural API choices, e.g.

@overload
def map(ids: List[int]) -> Dict[int, MyObj]: ...

@overload
def map(ids: List[str]) -> Dict[str, MyObj]: ...

We've run into this limitation several times already at Instagram, and there aren't any nice workarounds for it. From a user point of view, the "empty list" rationale doesn't feel right; it seems like it should be possible to only error in specific calls with an unknown contained type.

There is some more discussion in the above-linked thread of how to handle this. In any case, I think it deserves tracking as a mypy limitation.

@JelleZijlstra
Copy link
Member

This particular case could be handled by a TypeVar('T', int, str), unless I'm missing something. Is your actual use case more complicated?

@carljm
Copy link
Member Author

carljm commented Oct 25, 2017

Yes, good point, I over-simplified the example. In our real cases the types involved are not always the same (there is a third overload for a different type of id, which still results in int keys in the returned mapping).

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 25, 2017

The old worry about empty lists is no longer valid. In the early days of mypy it was a bigger concern since @overload actually did runtime type dispatch, but now that it's only used for static checking we can relax the rules.

@ilevkivskyi
Copy link
Member

but now that it's only used for static checking we can relax the rules.

I think that it is not well specified in PEP 484 what overloads mean. I think the first step here should be to make a PR to PEP 484 with a detailed specification of how overloads work, what is allowed, and what is not.

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 26, 2017

I think that it is not well specified in PEP 484 what overloads mean. I think the first step here should be to make a PR to PEP 484 with a detailed specification of how overloads work, what is allowed, and what is not.

I agree that PEP 484 should specify how overloads should work, but I think that it's better to write the specification together with an implementation so that we can immediately test how the specification works in practice.

Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Apr 14, 2018
This commit resolves python#1907.

Specifically, it turned out that support for non-positional args in
overload was never implemented to begin with. Thankfully, it also turned
out the bulk of the logic we wanted was already implemented within
`mypy.subtypes.is_callable_subtype`. Rather then re-implementing that
code, this commit refactors that method to support any kind of check,
instead of specifically subtype checks.

This, as a side-effect, ended up making some partial progress towards
python#4159 -- this is because unlike
the existing checks, `mypy.subtypes.is_callable_subtype` *doesn't* erase
types and has better support for typevars in general.

The reason this commit does not fully remove type erasure from overload
checks is because the new implementation still calls
`mypy.meet.is_overlapping_types` which *does* perform erasure. But
fixing that seemed out-of-scope for this commit, so I stopped here.
Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Apr 14, 2018
This commit resolves python#1907.

Specifically, it turned out that support for non-positional args in
overload was never implemented to begin with. Thankfully, it also turned
out the bulk of the logic we wanted was already implemented within
`mypy.subtypes.is_callable_subtype`. Rather then re-implementing that
code, this commit refactors that method to support any kind of check,
instead of specifically subtype checks.

This, as a side-effect, ended up making some partial progress towards
python#4159 -- this is because unlike
the existing checks, `mypy.subtypes.is_callable_subtype` *doesn't* erase
types and has better support for typevars in general.

The reason this commit does not fully remove type erasure from overload
checks is because the new implementation still calls
`mypy.meet.is_overlapping_types` which *does* perform erasure. But
fixing that seemed out-of-scope for this commit, so I stopped here.
Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue Apr 23, 2018
This commit resolves python#1907.

Specifically, it turned out that support for non-positional args in
overload was never implemented to begin with. Thankfully, it also turned
out the bulk of the logic we wanted was already implemented within
`mypy.subtypes.is_callable_subtype`. Rather then re-implementing that
code, this commit refactors that method to support any kind of check,
instead of specifically subtype checks.

This, as a side-effect, ended up making some partial progress towards
python#4159 -- this is because unlike
the existing checks, `mypy.subtypes.is_callable_subtype` *doesn't* erase
types and has better support for typevars in general.

The reason this commit does not fully remove type erasure from overload
checks is because the new implementation still calls
`mypy.meet.is_overlapping_types` which *does* perform erasure. But
fixing that seemed out-of-scope for this commit, so I stopped here.
Michael0x2a added a commit to Michael0x2a/mypy that referenced this issue May 11, 2018
This commit resolves python#1907.

Specifically, it turned out that support for non-positional args in
overload was never implemented to begin with. Thankfully, it also turned
out the bulk of the logic we wanted was already implemented within
`mypy.subtypes.is_callable_subtype`. Rather then re-implementing that
code, this commit refactors that method to support any kind of check,
instead of specifically subtype checks.

This, as a side-effect, ended up making some partial progress towards
python#4159 -- this is because unlike
the existing checks, `mypy.subtypes.is_callable_subtype` *doesn't* erase
types and has better support for typevars in general.

The reason this commit does not fully remove type erasure from overload
checks is because the new implementation still calls
`mypy.meet.is_overlapping_types` which *does* perform erasure. But
fixing that seemed out-of-scope for this commit, so I stopped here.
@ilevkivskyi ilevkivskyi added false-positive mypy gave an error on correct code topic-overloads labels May 20, 2018
@ilevkivskyi
Copy link
Member

This is fixed on master now.

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

No branches or pull requests

4 participants