Hi Vincent, To answer your two questions:
* The problem with indexing is that whether you got a scalar or an array thereof is dependent on the dimensionality of the array. While shape-typing is something that's being worked on (see PEP 646), we're not quite there yet and for the mean time we're stuck with returning Any. * What is the "best" solution here is honestly a bit of an open question. With the exception of np.str_ and np.bytes_ none of numpy's scalar-types inherit from their stdlib counterpart (float64 and complex128 do inherit during runtime, but this is unfortunately not possible to express in the annotations as they're not typed as proper (complex)floating subclasses). A while ago there was some discussion about adding them as baseclasses while static type checking (numpy/numpy#17105), but the responses back then were lukewarm. This might be worthwhile to revisit at some point in the future though, be it either in the stub-files proper or via a mypy plugin. * What doesn't help here either is that the duck-typing of (numerical) scalar types is currently in a poor state. This would alleviate some/all of the headache here, but the numbers-based ABCs are downright useless for static typing in their current state. * Regards, Bas van Beek ________________________________ From: Vincent Schut <sc...@satelligence.com> Sent: 18 January 2022 12:39 To: numpy-discussion@python.org <numpy-discussion@python.org> Subject: [Numpy-discussion] Re: ndarray typing advise Sorry to bump my own issue, but I wonder if just no-one knows, or if it has been overlooked, or maybe I did not explain my questions right? Should I rather ask this on SO? Or should I just be more patient...? Thanks! Vincent. On 1/13/22 11:43, Vincent Schut wrote: Hi, I'm having a hard(ish) time adding the right type annotations to some numpy-using code. I hope someone more knowledgeable can give me some advise. Specifically, the following questions pop up: - How do I correctly type a ndarray as being an array of float64, such that mypy can infer that when I index this array, the result is actually a float (currently I consistently get "Any" after indexing)? - How can I get compatibility between np.floating and builtin.float? Currently, some numpy functions are typed to return np.floating[Any] (e.g. np.linalg.norm). When I feed this result into something that is supposed to receive a builtin.float, mypy complains. Is an explicit cast(float, <the np.floating[Any] thing>) the prefered way to gain compatibility here? versions: numpy: 1.22.0 python: 3.8.10 (unfortunately we're limited to this version) mypy: 0.931 Thanks! Vincent. -- [https://lh6.googleusercontent.com/t1GD8ftw5REdi0_kM1UjrXeX-Y-jF5xKxDqUGLsLKzftVahqw_CWQhqciHkLLYkldd9pcYmNd-pmzNnY1fc93vYpJ0Q0P-O1VC51jsTJUy-yhjF4ioK2hGq156_JYVROec9im6tW] Vincent Schut Remote Sensing Software Engineer [https://lh5.googleusercontent.com/H6PdQi9jrkkJRWy7wGQ92waInTqhh3atodO0R0CNgKm2YooJBK2vOEU2M7t5HB5FMJyOUhyFxsTqtEnqLi3zXnRbPNJEvoEyr9RyBFKKHUjT8py8Pu92ga3Urmp_zARoHqtNFY6K] +31 302272679 ~ Maliebaan 22 | 3581CP | Utrecht | Netherlands Linkedin<https://www.linkedin.com/company/satelligence/> ~ satelligence.com<http://www.satelligence.com><http://www.satelligence.com>
_______________________________________________ NumPy-Discussion mailing list -- numpy-discussion@python.org To unsubscribe send an email to numpy-discussion-le...@python.org https://mail.python.org/mailman3/lists/numpy-discussion.python.org/ Member address: arch...@mail-archive.com