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 seems to bind the TypeVar of a Generic decorator incorrectly when a variable is annotated as a Union.
To Reproduce
This example is based on this untyped cython code.
fromtypingimportCallable, Generic, Optional, TypeVar, Union, overloadF=TypeVar("F")
G=TypeVar("G")
classcache_readonly(Generic[F, G]):
def__init__(self, func: Callable[[F], G]) ->None:
self.func=funcself.name=func.__name__self.__doc__=getattr(func, "__doc__", None)
@overloaddef__get__(self, obj: F, typ) ->G:
...
@overloaddef__get__(self, obj: None, typ) ->"cache_readonly[F, G]":
...
def__get__(self, obj: Optional[F], typ) ->Union[G, "cache_readonly[F, G]"]:
ifobjisNone:
# accessed on the class, not the instancereturnself# Get the cache or set a default one if neededcache=getattr(obj, "_cache", None)
ifcacheisNone:
cache=obj._cache= {} # type: ignoreifself.nameincache:
val=cache[self.name]
else:
val=self.func(obj)
cache[self.name] =valreturnvaldef__set__(self, obj: F, value: G) ->None:
raiseAttributeError("Can't set attribute")
classTestA:
@cache_readonlydefx(self) ->str:
return"A"classTestB:
@cache_readonlydefx(self) ->str:
return"B"deftest(obj: Union[TestA, TestB]) ->str:
returnobj.x
Expected Behavior
Mypy and pyright are happy with the code :)
Actual Behavior
mypy
test.py:55: error: Argument 1 to "__get__" of "cache_readonly" has incompatible type "Union[TestA, TestB]"; expected "TestA"
test.py:55: error: Argument 1 to "__get__" of "cache_readonly" has incompatible type "Union[TestA, TestB]"; expected "TestB"
Pyright doesn't complain (except for the __doc__ line).
Your Environment
Mypy version used: 0.910
Mypy command-line flags: -
Mypy configuration options from mypy.ini (and other config files): -
Python version used: 3.9.6
Operating system and version: CentOS 3.10.0-693.5.2.el7.x86_64
The text was updated successfully, but these errors were encountered:
Bug Report
Mypy seems to bind the TypeVar of a Generic decorator incorrectly when a variable is annotated as a Union.
To Reproduce
This example is based on this untyped cython code.
Expected Behavior
Mypy and pyright are happy with the code :)
Actual Behavior
mypy
Pyright doesn't complain (except for the
__doc__
line).Your Environment
mypy.ini
(and other config files): -The text was updated successfully, but these errors were encountered: