You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mypy chooses the wrong overload of a generic class method in the below code. str | Sequence[int]would match Sequence[ItemT] if ItemT was not bounded or constrained, but since it is constrained to non-str the overload for str | Sequence[ItemT] should be matched instead. The issue only affects generic class methods and not regular functions.
To Reproduce
fromcollections.abcimportSequencefromtypingimportGeneric, TypeVar, overloadfromtyping_extensionsimportassert_typeT=TypeVar("T", covariant=True)
ItemT=TypeVar("ItemT", bound=int) # OR TypeVar("ItemT", int, float)classMyGeneric(Generic[T]):
@overloaddeftransform(self: "MyGeneric[str]") ->"MyGeneric[str]": ...
@overloaddeftransform(self: "MyGeneric[Sequence[ItemT]]") ->"MyGeneric[Sequence[ItemT]]": ...
@overloaddeftransform(self: "MyGeneric[str | Sequence[ItemT]]") ->"MyGeneric[str | Sequence[ItemT]]": ...
deftransform(self: "MyGeneric[str | Sequence[ItemT]]") ->"MyGeneric[str | Sequence[ItemT]]": ... # type: ignoreassert_type(MyGeneric[str]().transform(), MyGeneric[str])
assert_type(MyGeneric[Sequence[int]]().transform(), MyGeneric[Sequence[int]])
assert_type(MyGeneric[str|Sequence[int]]().transform(), MyGeneric[str|Sequence[int]])
# error: Expression is of type "MyGeneric[Sequence[object]]", not "MyGeneric[str | Sequence[int]]"# ---# The equivalent functions do not have the same issue@overloaddeftransform(x: "MyGeneric[str]") ->"MyGeneric[str]": ...
@overloaddeftransform(x: "MyGeneric[Sequence[ItemT]]") ->"MyGeneric[Sequence[ItemT]]": ...
@overloaddeftransform(x: "MyGeneric[str | Sequence[ItemT]]") ->"MyGeneric[str | Sequence[ItemT]]": ...
deftransform(x: "MyGeneric[str | Sequence[ItemT]]") ->"MyGeneric[str | Sequence[ItemT]]": ... # type: ignoreassert_type(transform(MyGeneric[str]()), MyGeneric[str])
assert_type(transform(MyGeneric[Sequence[int]]()), MyGeneric[Sequence[int]])
assert_type(transform(MyGeneric[str|Sequence[int]]()), MyGeneric[str|Sequence[int]])
Your Environment
Mypy version used: 1.14.1
Mypy command-line flags:
Mypy configuration options from mypy.ini (and other config files):
Python version used: 3.13.0
The text was updated successfully, but these errors were encountered:
This could be the same issue and is a simpler reproduction, and rules out overload as being the cause. The typevar StrIntT is constrained to str or int, so the Literal input should become a str output, but it remains Literal.
fromtypingimportGeneric, Literal, TypeVarfromtyping_extensionsimportassert_typeT=TypeVar("T")
StrIntT=TypeVar("StrIntT", str, int)
classMyGeneric(Generic[T]):
deftransform(self: "MyGeneric[StrIntT]") ->"MyGeneric[StrIntT]":
returnselfassert_type(MyGeneric[str]().transform(), MyGeneric[str])
assert_type(MyGeneric[Literal["x"]]().transform(), MyGeneric[str])
# error: Expression is of type "MyGeneric[Literal['x']]", not "MyGeneric[str]"# ---# The equivalent functions do not have the same issuedeftransform(x: MyGeneric[StrIntT]) ->MyGeneric[StrIntT]: ... # type: ignore[empty-body]assert_type(transform(MyGeneric[str]()), MyGeneric[str])
Bug Report
Mypy chooses the wrong overload of a generic class method in the below code.
str | Sequence[int]
would matchSequence[ItemT]
ifItemT
was not bounded or constrained, but since it is constrained to non-str the overload forstr | Sequence[ItemT]
should be matched instead. The issue only affects generic class methods and not regular functions.To Reproduce
Your Environment
mypy.ini
(and other config files):The text was updated successfully, but these errors were encountered: