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
Using a typevar bound to a class that has a self returning method leads to an error when using in a function that operates on the typevar and returns the typevar.
Using constraint does not present the issue but looses the generic on the class.
To Reproduce
# pyright: strictfromtypingimportAny, Generic, TypeVarfromtyping_extensionsimportSelf, reveal_typeT=TypeVar("T")
classX(Generic[T]):
defm(self, v: int) ->Self:
returnselfclassY:
defm(self, v: int) ->Self:
returnself# using bound: tb_self errors with Incompatible return value typeTB=TypeVar("TB", bound=X[Any] |Y)
deftb_self(value: TB) ->TB:
returnvalue.m(42)
deftb(value: TB) ->TB:
returnvaluereveal_type(tb_self(X[int]()))
reveal_type(tb(X[int]()))
# using constraint: return type is X[Any] in both casesTC=TypeVar("TC", X[Any], Y)
deftc_self(value: TC) ->TC:
returnvalue.m(42)
deftc(value: TC) ->TC:
returnvaluereveal_type(tc_self(X[int]()))
reveal_type(tc(X[int]()))
file.py:20:12: error: Incompatible return value type (got "X[Any] | Y", expected "TB") [return-value]
file.py:25:13: note: Revealed type is "try_.X[builtins.int]"
file.py:26:13: note: Revealed type is "try_.X[builtins.int]"
file.py:37:13: note: Revealed type is "try_.X[Any]"
file.py:38:13: note: Revealed type is "try_.X[Any]"
Found 1 error in 1 file (checked 1 source file)
For reference pyright on the same input reports no error
Expected Behavior
The is no error.
Actual Behavior
Mypy reports an error when the using a self returning method
Your Environment
Mypy version used: 1.9.0, 1.7.1
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): -
Python version used: 3.10, 3.12
The text was updated successfully, but these errors were encountered:
Bug Report
Using a typevar bound to a class that has a self returning method leads to an error when using in a function that operates on the typevar and returns the typevar.
Using constraint does not present the issue but looses the generic on the class.
To Reproduce
https://gist.github.com/mypy-play/3f911f7de9e2282fb6463a7146ffae47
The output is:
For reference pyright on the same input reports no error
Expected Behavior
The is no error.
Actual Behavior
Mypy reports an error when the using a self returning method
Your Environment
mypy.ini
(and other config files): -The text was updated successfully, but these errors were encountered: