-
Notifications
You must be signed in to change notification settings - Fork 765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pylance infers Literal[] type where it is not intended #4633
Comments
This behavior is correct from a type checking perspective. An x: Integral = 0 # Type error Not surprisingly, mypy generates the same error in this case. The abstract numeric types defined in The recommended workaround is to avoid the use of abstract numeric types and use the standard builtin types like (Note: This has nothing to do with the other bug that was referenced above. They're completely unrelated. It also has nothing to do with type inference or literals, as the title implies.) |
Another workaround, if you really want to use class MyInt(int, Integral):
pass
class MyEnum(Enum):
A = MyInt(0x1)
B = MyInt(0x2) |
thanks for the quick response. The first and recommended workaround works fine. |
Are you receiving the "Cannot instanciate abstract class" error from the Python interpreter or VS Code? It runs fine for me. Here's the full sample: from enum import Enum
from numbers import Integral
class MyInt(int, Integral):
pass
class MyEnum(Enum):
A = MyInt(0x1)
B = MyInt(0x2)
def myfunc(a: Integral):
pass
myfunc(MyEnum.A.value)
The types in the |
Thanks for the good explanation. The abstract class error is reported by Pylance in VS Code:
|
Looks like you're using strict-mode type checking with the latest production version of Pylance. If you use the latest prerelease version of Pylance, this issue doesn't occur. That's because the prerelease version of Pylance contains a newer version of the typeshed stubs, and a small change was made recently in these stubs to include |
Closing as this is fixed in prerelease version 2023.7.31. |
When I try to pass an Enum-value e.g.
to a function parameter type-hinted as
Integral
e.g.pylance infers
Literal[]
for the Enum-value and complains about incompatibility.Originally posted by @tthommes in #697 (comment)
There has been a bug, fixed three years ago, that seems to be the same issue. When I back-port my Pylance-Version to that one, the here described issue is gone. So I suppose the bug was somehow "resurrected".
The text was updated successfully, but these errors were encountered: