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
0.920 Regression: literal unions aren't inferred from conditionals anymore #11782
$ mypy test.py
1.py:10:17: note: Revealed type is "builtins.str"
1.py:11:47: error: Argument "byteorder" to "from_bytes" of "int" has incompatible type "str"; expected "Union[Literal['little'], Literal['big']]"
With mypy-0.920 and later. 0.910 correctly inferred byteorder as Literal['little'] | Literal['big']
I don't think mypy ever inferred the type of byteorder as Literal['little', 'big']. I think we always inferred str but 0.920 changed the type signature of int.from_bytes to take a Literal instead of a str, turning this into an error. (That change was added in python/typeshed#5715 and added to mypy in #10862, which was included in 0.920).
You can silence the error by adding an explicit Literal["little", "big"] type for byteorder.
@pranavrajpal, you're right, 0.910 also doesn't infer byteorder as Literal. I somehow convinced myself that it did. Not an inference regression then. Thanks for the explanation!
Activity
pranavrajpal commentedon Dec 18, 2021
I don't think mypy ever inferred the type of
byteorderasLiteral['little', 'big']. I think we always inferredstrbut 0.920 changed the type signature ofint.from_bytesto take aLiteralinstead of astr, turning this into an error. (That change was added in python/typeshed#5715 and added to mypy in #10862, which was included in 0.920).You can silence the error by adding an explicit
Literal["little", "big"]type forbyteorder.hauntsaninja commentedon Dec 18, 2021
TBH I find several of these typeshed Literal changes to be quite problematic, e.g. here's one I recently reverted python/typeshed#6566
elprans commentedon Dec 18, 2021
@pranavrajpal, you're right, 0.910 also doesn't infer
byteorderasLiteral. I somehow convinced myself that it did. Not an inference regression then. Thanks for the explanation!