Skip to content

Constructing a FieldArray from a list of ndarray objects fails typechecking #603

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

Closed
maksverver opened this issue Apr 30, 2025 · 3 comments
Closed

Comments

@maksverver
Copy link

Consider the following example:

import galois
import numpy as np

GF = galois.GF(16)

a: galois.FieldArray = GF([[1,2,3]])            # OK
b: galois.FieldArray = GF(np.array([[1,2,3]]))  # OK
c: galois.FieldArray = GF([np.array([1,2,3])])  # ERROR (see below)

print(a)  # [[1, 2, 3]]
print(b)  # [[1, 2, 3]]
print(c)  # [[1, 2, 3]]

This runs just fine, but typechecking with pyright fails only for the third assignment:

test.py:8:28 - error: Argument of type "list[NDArray[Any]]" cannot be assigned to parameter "x" of type "ElementLike | ArrayLike" in function "__new__"
    Type "NDArray[Any]" is not assignable to type "IterableLike"
      "ndarray[_Shape, dtype[Any]]" is not assignable to "Sequence[ElementLike]"
      "ndarray[_Shape, dtype[Any]]" is not assignable to "Sequence[IterableLike]" (reportArgumentType)
test.py:8:28 - error: Argument of type "list[NDArray[Any]]" cannot be assigned to parameter "x" of type "ElementLike | ArrayLike" in function "__init__"
    Type "NDArray[Any]" is not assignable to type "IterableLike"
      "ndarray[_Shape, dtype[Any]]" is not assignable to "Sequence[ElementLike]"
      "ndarray[_Shape, dtype[Any]]" is not assignable to "Sequence[IterableLike]" (reportArgumentType)
2 errors, 0 warnings, 0 informations 

(The two errors are exactly the same, except one is about __new__ and the other about __init__.)

So the question is: why is an NDArray considered not IterableLike? An ndarray instance is definitely iterable. Is this a bug in the type definitions for galois?

For reference: I'm using Python 3.13.3, galois 0.4.5, pyright 1.1.400.

@mhostetter
Copy link
Owner

I suppose we could add NDArray to IterableLike.

The typing in NumPy, and consequently this package, have been challenging to say the least. See #486.

@maksverver
Copy link
Author

I suppose we could add NDArray to IterableLike.

That would fix my problem.

It also seems like the right thing to do since the initializer already passes the “iterable” off to np.array() here, so in practice the FieldArray constructor already accepts anything that's acceptable by np.array(), at least so long as the values can be converted to field elements.

mhostetter added a commit that referenced this issue Apr 30, 2025
mhostetter added a commit that referenced this issue Apr 30, 2025
mhostetter added a commit that referenced this issue Apr 30, 2025
@mhostetter
Copy link
Owner

Released in v0.4.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants