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
{{ message }}
Repository navigation
Inference for Literal arguments not handled #11003
literalinf.py:10: error: Argument 1 to "myfun" has incompatible type "str"; expected "Union[Literal['a'], Literal['b'], Literal['c']]"
literalinf.py:18: error: Argument 1 to "myfun2" has incompatible type "int"; expected "Union[Literal[1], Literal[2]]"
literalinf.py:21: error: Argument 1 to "myfun2" has incompatible type "int"; expected "Union[Literal[1], Literal[2]]"
Expected Behavior
No error. The values can be inferred to be of the Literal value
Actual Behavior
See above.
Your Environment
Mypy version used: 0.910
Mypy command-line flags: None
Mypy configuration options from mypy.ini (and other config files): None
The problem is if the variable is re-assigned to, then it could be incorrect to pass it to these functions. The solution for your scenario is to type these assignments with Final, like a: Final = "a".
Activity
emmatyping commentedon Aug 20, 2021
The problem is if the variable is re-assigned to, then it could be incorrect to pass it to these functions. The solution for your scenario is to type these assignments with Final, like
a: Final = "a".erictraut commentedon Aug 20, 2021
It shouldn't be necessary to mark them as Final. If a variable is reassigned, the type checker knows it's reassigned and can flag it as an error.
There is no type violation, so these errors are false positives.
The above code type checks without errors in pyright. I don't see why it shouldn't work with mypy.
Dr-Irv commentedon Aug 21, 2021
I agree. @ethanhs based on @erictraut 's comment, can you reopen this?
Zac-HD commentedon Dec 10, 2021
I get a related error when setting the default argument to a function:
hauntsaninja commentedon Dec 10, 2021
Hello @Zac-HD ! I enjoyed your recent paper :-) The default arguments issue is fixed on master, in #11236
Zac-HD commentedon Dec 10, 2021
Thanks, and thanks! I'm really looking forward to 0.920 and the release after 😁