-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: modelling various integer types as a specialized version of the generic class breaks type-checking in pyright #23007
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
Comments
Gentle nudge here. Is this a difficult change? |
Sort of. With how difficult it is to statically infer the bit sizes of returned numpy scalars & array dtypes it is currently not viable to model the various integer types as proper subclasses (rather than just parametrized subtypes). Returning the correct subclass is important as one can not freely downcast to a subclass without using heavy handed tools such as class Foo:
pass
class Bar(Foo):
pass
# Incompatible types in assignment (expression has type "Foo", variable has type "Bar") [assignment]
var: Bar = Foo() On the other hand, if the supertype is an There is one other solution I can think of here: avoid even trying to deal with bit sizes and treat the likes of int8: typing.TypeAlias = signedinteger
int16: typing.TypeAlias = signedinteger
int32: typing.TypeAlias = signedinteger
int64: typing.TypeAlias = signedinteger
# etc. |
I would be happy with that solution, for whatever that is worth. Have you considered a solution that has two type annotations for each dtype: class int8(signedInteger[_8Bit]):
...
Int8 = signedinteger[_8Bit] This requires some intelligence on behalf of the user, since they have to understand that sometimes a type that should be This second solution can also get better with time, either by adding increasingly precise overloads or in some cases by making use of new features like the |
I started the solution above here. I think it looks pretty good. There are still some test failures, but I think it's worth figuring out if there's any interest before fixing them. |
Describe the issue:
I want to make sure that some variable is an instance of
np.uint64
. To this end, I use the following:However, in that case I have the following error on that line during type-checking with
pyright
:As the developers of pyright explained, this is due to fact that type stubs model
np.uint64
as a specialized version of the generic classunsignedinteger
(uint64 = unsignedinteger[_64Bit]
), while in fact it is a proper subclass ofunsignedinteger
. As specialized versions of a class (likelist[int]
) are not allowed inisinstance
, the type-checker reports an error, making it impossible to use thisassert
at all.Are there any reasons to model these types (like
np.uint64
,np.uint32
, etc) in this way (as a specialized versions) instead of making separate types?Reproduce the code example:
Error message:
Runtime information:
1.24.1
3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:26:04) [GCC 10.4.0]
[{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_KNL',
'AVX512_KNM',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'Haswell',
'filepath': '/vol/tcm10/ischurov/.conda/envs/latsym2/lib/libopenblasp-r0.3.21.so',
'internal_api': 'openblas',
'num_threads': 1,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.21'}]
None
Context for the issue:
Initially, I had a code that looked like the following:
On the type-checking, I have an error:
Argument of type "Scalar | Unknown" cannot be assigned to parameter "x" of type "uint64" in function "foo"
. That's because the type-checker cannot infer that index ofpd.Series
has dtypeuint64
. Due to open issue in pandas, it is not yet possible to specifypd.Series.index
's dtype using type hints. To give the type-checker information thatidx
has typenp.uint64
, I want to add the following assert before runningfoo
:And that leads to type-checker error, as explained above.
The text was updated successfully, but these errors were encountered: