Pyright: List[str] and list[str] results in Union, then becomes Unknown on unpacking.

Created on 30 Oct 2019  路  3Comments  路  Source: microsoft/pyright

I have a conditional in my code which apparently results in a Union[List[str], list[str]]. I would expect the signature to simply be list[str]. Additionally, this union information is lost when used in a comprehension, resulting in a list[Unknown] rather than a list[str].

pairs = [
    s.split(':') if ':' in s else [s, 'null']
    for s in ['foo:bar', 'baz']
]
foo = [p[0] for p in pairs]
bar = [a for [a, b] in pairs]

# (variable) pairs: list[Union[List[str], list[str]]]
# (variable) foo: list[str]
# (variable) bar: list[Unknown]

pyright version: 1.0.81.
VS Code version: 1.39.2.

addressed in next version bug

All 3 comments

Follow-up: I noticed that

bar = [a for a, b in pairs]

correctly flags bar as a list[str]. So maybe I'm just unpacking wrong?

Thanks for the bug report. Both of these issues will be fixed in the next published version of pyright.

This is now fixed in 1.0.82, which I just published.

Was this page helpful?
0 / 5 - 0 ratings