9

I try to use numpy with nptyping's Array to do my typehinting.

I tried the following:

enemy_hand: Array[float, 48] = np.zeros(48)

I get an typerror:

Expected Type 'Array[float, Any]', got 'ndarray' instead

as far as I understand from this: https://pypi.org/project/nptyping/ thats how it should look.

2
  • What line gives you that error? type hinting is only for the user. Also I tried to replicate your problem but isinstance gives the expected outputs. Please update your question to include some more information about the error and how you're encountering it Jan 14, 2020 at 7:22
  • @sshashank124 as pointed out in a comment on my deleted answer, asker is trying to do Variable Annotation
    – Daniel F
    Jan 14, 2020 at 7:51

1 Answer 1

8
+50

nptyping is currently useless for static analysis. Quoting a post by the library's developer on its issue tracker,

mypy is just not supported by nptyping (yet)

I wouldn't put much hope in that "yet". NumPy's dtype and shape handling is very hard to fit into the typing/mypy static type model, and nptyping's own design decisions are a poor fit for NumPy itself. For example, it doesn't look like the dev ever considered arrays that aren't 2D, so Array[str, 3] represents a 2D array with 3 rows and unspecified columns instead of a 3-element 1D array. All the implementation is in terms of rows and columns, too.

As far as I can tell, the only real functionality nptyping has is isinstance checks, and even that's buggy.

4
  • @NameVergessen: No. Like I said, NumPy's dtype and shape handling is a very poor match for the typing/mypy type system. Jan 14, 2020 at 12:42
  • as far as i can tell the yet isn't here (yet). Feb 3, 2021 at 14:30
  • FYI, Still not yet
    – Gulzar
    Jan 18, 2022 at 9:16
  • For nptyping 2.x, mypy can detect array vs non-array but cannot detect shape/dimension label mismatch.
    – Ziyuan
    Apr 26 at 14:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.