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
isinstance(x, np.uint64) gives error: Second argument to "isinstance" must be a class or tuple of classes, TypeVar or generic type with type arguments not allowed
#3830
Closed
ischurov opened this issue
Jan 12, 2023
· 2 comments
The following error is given regarding the expression `isinstance(x, np.uint64)`:
Second argument to "isinstance" must be a class or tuple of classes
TypeVar or generic type with type arguments not allowed
Additional context
There is a similar fixed issue #2128. I am not sure that it is actually a bug, as np.uint64 is a type alias, but I believe there should be a way to run this `isinstance` without having type error. `mypy` finds no issues in this snippet.
The text was updated successfully, but these errors were encountered:
The problem here is that the numpy type stubs are "lying" to the type checker about how the underlying classes are modeled in numpy. The stubs define uint64 as a specialized version of the generic class unsighed integer.
uint64=unsignedinteger[_64Bit]
I would expect this to generate a runtime TypeError if used with isinstance, but it doesn't. That's because the runtime implementation doesn't use a generic class. Instead, the runtime implementation of uint64 is a proper subclass of unsignedinteger.
Pyright (the type checker upon which pylance is built) relies on accurate static type information from the library. In this case, the static type information provided by numpy is unfortunately not correctly modeling the runtime types, which results in this false positive error.
You could file a bug with the numpy maintainers, but I think it's unlikely they would want to make a change here.
You may need to resort to a # type: ignore or a typing.cast call to suppress the static type error.
Environment data
Code Snippet
Repro Steps
Type-check the given snippet.Expected behavior
No errors found.
Actual behavior
The following error is given regarding the expression `isinstance(x, np.uint64)`:Additional context
There is a similar fixed issue #2128. I am not sure that it is actually a bug, as np.uint64 is a type alias, but I believe there should be a way to run this `isinstance` without having type error. `mypy` finds no issues in this snippet.The text was updated successfully, but these errors were encountered: