DetachHead / basedpyright

pyright fork with various type checking improvements, improved vscode support and pylance features built into the language server
https://docs.basedpyright.com
Other
1.19k stars 21 forks source link

No type inference on empty collections, giving unnecessary reportUnknownMemberType #192

Open meee-low opened 7 months ago

meee-low commented 7 months ago

Basedpyright is giving me errors when I initialize an empty collection without annotations for the item generic and then try to append to it.

Minimum reproducible example:

a = []
a.append(1)  # ERROR: Type of "append" is partially unknown [reportUnknownMemberType]

Other typecheckers seem to be ok with this kind of pattern. mypy --strict doesn't find any problems with the example above. It correctly infers it as list[int] when I run reveal_type(). However if I try to append a string to a after appending 1, it correctly complains that they don't match:

# mypy: 
from typing_extensions import reveal_type
a = []
a.append(1)  # fine for mypy
reveal_type(a)  # mypy gives list[int], while basedpyright gives list[Unknown]
a.append("oops")  # mypy complains that this doesn't match list[int]

Initializing an empty collection is a common pattern, so it would make sense to support it (infer) without giving an error.

DetachHead commented 7 months ago

this is an upstream pyright issue that's been rejected (https://github.com/microsoft/pyright/issues/5766), but i agree it should be fixed

asukaminato0721 commented 1 week ago

another upstream issue https://github.com/microsoft/pyright/issues/6710