-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Unable to track Literal type conditionally imported from third party library #11568
Comments
I'm trying to reproduce this: import sys
if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal
Direction = Literal['request', 'response']
x: Direction
reveal_type(x) # N: Revealed type is "Union[Literal['request'], Literal['response']]" Works fine. Let's try something else. Putting this into a different file also works for the latest Can you please add |
Without
I too saw that when all put into a single file, everything worked fine. My suspicion has been that because I played around with modifying the third party library (adding |
Yes, this is what I was missing 🙂 It won't work in Repro: # a.py
import sys
if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal
# b.py
from b import Literal
Direction = Literal['request', 'response']
# c.py
from a import Direction
x: Direction
reveal_type(x) # Any Solutionimport sys
if sys.version_info >= (3, 8):
from typing import Literal as Literal
else:
from typing_extensions import Literal as Literal After this change even with I am not sure this is fixable outside of 3rd party lib. The only thing you can do is to disable |
@sobolevn Shouldn't another (better?) fix be to set I'm also curios why |
Yes,
Any other names won't work. Only matching names count. |
Weird, when I change if sys.version_info >= (3, 8):
from typing import Final as Final, Literal as Literal, _TypedDictMeta as _TypedDictMeta
else:
from typing_extensions import (
Final as Final, Literal as Literal, _TypedDictMeta as _TypedDictMeta,
) I still see the |
Thanks sobolevn! I think the remaining thing that can be done here is #13965, so closing as a duplicate of that issue |
Bug Report
mypy
seems unable to track aLiteral
type when theLiteral
is conditionally imported from a third party library.To Reproduce
Run:
Expected Behavior
No type error because
mypy
can detect this is exhaustive.Actual Behavior
Error printed above
Your Environment
--strict
mypy.ini
(and other config files): N/AThird party code of interest:
I tried modifying the third party package locally to remove some of the conditional imports, but I could never get the
mypy
error to go away. The only way I was able to get the error to stop was to redeclare (duplicate) the type locally:The text was updated successfully, but these errors were encountered: