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
But if we move the decorator into its own module and use a relative import, it breaks no matter how we define the decorator type:
# test_case.pyfromtypingimportCallable, Iterable, TypeVar, Union# from test_case_import import decr # <-- this worksfrom .test_case_importimportdecr# <-- this causes the false positives below classResult:
@decrdef__init__(self):
passres=Result() # ; reveal_type(res) # -> Any (should be test_case.Result)defsome_results(op: Callable[[], Union[Result, Iterable[Result]]]) ->Iterable[Result]:
result_or_results=op()
results: Iterable[Result]
ifisinstance(result_or_results, Result):
results= (result_or_results,) # ; reveal_type(result_or_results) # -> (should be test_case.Result)else:
results=result_or_results# ; reveal_type(result_or_results) # -> Union[test_case.Result, typing.Iterable[test_case.Result]] (should be typing.Iterable[test_case.Result])returnresults
# test_case_import.pyfromtypingimportCallable, TypeVar_T=TypeVar("_T")
def_identity(__: _T) ->_T:
return__decr: Callable[[_T], _T] =_identity# <-- doesn't matter how this is defined
% mypy --config-file=pyproject.toml test_case.py test_case_import.py
test_case.py:19: error: Incompatible types in assignment (expression has type"Union[Result, Iterable[Result]]", variable has type"Iterable[Result]")
Found 1 error in 1 file (checked 2 source files)
Using the Protocol approach does not salvage the second (relative import) scenario.
The text was updated successfully, but these errors were encountered:
What does note: Revealed type is "def [_T] (_T`-1) -> _T`-1" mean? (I found that when looking into the real world case that inspired the above distillations.)
It's a function with the type signature (T) -> T where T is a typevar, i.e. an identity function.
Good to know it's legit. I've never seen that before. Is it documented anywhere? I was unable to find it in the official docs. (Apologies if I missed it.)
This one's just weird. I'm not sure if this is a dup of #1927, but I figured I'd raise it anyway.
Note that if
DecoratorT
is defined as follows, things work again:But if we move the decorator into its own module and use a relative import, it breaks no matter how we define the decorator type:
Using the
Protocol
approach does not salvage the second (relative import) scenario.The text was updated successfully, but these errors were encountered: