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
Describe the bug
Pyright does not report errors, when declaring the enum within the same file where it is being used. When importing the same enum from a different file, pyright reports an error.
To Reproduce
bar.py
fromenumimportEnumfromtypingimportLiteral# similar to https://github.com/python/typeshed/pull/7127/filesclass_NoDefault(Enum):
no_default="NO_DEFAULT"no_default=_NoDefault.no_default# Sentinel indicating the default value.NoDefault=Literal[_NoDefault.no_default]
deftest(x: NoDefault=no_default):
...
pyright reports:
3:13 - information: Type of "bar.NoDefault" is "Type[Literal[_NoDefault.no_default]]"
4:13 - information: Type of "bar.no_default" is "_NoDefault"
6:29 - error: Expression of type "_NoDefault" cannot be assigned to parameter of type "NoDefault"
Expected behavior
It seems that pyright has troubles inferring bar.no_default, it should be bar._NoDefault.no_default and not bar._NoDefault.
The text was updated successfully, but these errors were encountered:
Yes, this behavior is intended. The variable no_default has no type annotation, so its type is inferred. In this case, it has an inferred type of _NoDefault. Locally, its type is narrowed to the type Literal[_NoDefault.no_default] because of the assignment. When you import that variable from another module, the local type narrowing is not applied, so its type is simply NoDefault. If your intent is for no_default to have the type Literal[_NoDefault.no_default], you should annotate it as such.
Even better, if your intent is for this variable to be constant, you should annotate it as Final. When inferring the type of a Final variable, pyright retains the literal type.
Describe the bug
Pyright does not report errors, when declaring the enum within the same file where it is being used. When importing the same enum from a different file, pyright reports an error.
To Reproduce
bar.py
pyright reports no errors for bar.py
foo.py
pyright reports:
3:13 - information: Type of "bar.NoDefault" is "Type[Literal[_NoDefault.no_default]]"
4:13 - information: Type of "bar.no_default" is "_NoDefault"
6:29 - error: Expression of type "_NoDefault" cannot be assigned to parameter of type "NoDefault"
Expected behavior
It seems that pyright has troubles inferring
bar.no_default
, it should bebar._NoDefault.no_default
and notbar._NoDefault
.The text was updated successfully, but these errors were encountered: