8000 Incompatible return type for overlapping signatures with regards to __new__ · Issue #10313 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Incompatible return type for overlapping signatures with regards to __new__ #10313

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

Closed
hatal175 opened this issue Apr 12, 2021 · 2 comments
Closed
Labels
bug mypy got something wrong

Comments

@hatal175
Copy link
Contributor
hatal175 commented Apr 12, 2021

Bug Report

I came up with this when trying to fix itertools.combinations in typeshed to be a class instead of function.
It seems like mypy doesn't consider Tuple[T,T] to not overlap with Tuple[T, ...] with regards to return type.

I'm not sure if this is a bug or policy, but I would think that a variable length tuple is basically a union of all set length tuples and therefore should accept this.

To Reproduce

See the following code:
`
from typing import Generic, Literal, Tuple, overload,TypeVar

_T = TypeVar("_T")

class A(Generic[_T]):
@overload
def new(cls, a: Literal[1]) -> A[Tuple[int]]:
...
@overload
def new(cls, a: int) -> A[Tuple[int, ...]]:
...
def new(cls, a: int) -> A[Tuple[int, ...]]:
return super().new(cls)
`
mypy returns the following error:
error: Overloaded function signatures 1 and 2 overlap with incompatible return types

(also some error for the implementation return type, but it's irrelevant).

Your Environment

latest mypy, python 3.8.

Tried also on mypy playground with latest mypy and 3.9.

@hatal175 hatal175 added the bug mypy got something wrong label Apr 12, 2021
@hatal175 hatal175 changed the title Variable length tuples incompatible with set length tuples with regards to return type Incompatible return type for overlapping signatures with regards to __new__ Apr 12, 2021
@hatal175
Copy link
Contributor Author

OK, my bad, I haven't researched this enough.
The issue here is that A[Tuple[int]] and A[Tuple[int,...]] are not subtypes, which they probably aren't. However, I do want to return a single tuple when a is specifically 1 - see https://github.com/python/typeshed/blob/2ae49e130751b9404e6703abe90fe869d63ef065/stdlib/itertools.pyi#L170

@hatal175
Copy link
Contributor Author

I think I made a mistake here with return types and covariant TypeVars. My mistake!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

1 participant
0