Related: can someone point to a somewhat definitive help article on the current state of typing for ndarrays? I am interested in this too.
Of late my experience has been trying to straddle the web and scientific computing worlds (I have been writing a scientific/engineering based web app), and while typing is incredibly useful for the web (fastapi and pydantic, I LOVE YOU), it has been an endless source of frustration in scientific computing. It would be great to be pointed to something current that will help gain a thorough understanding of what we can do with typing in numpy these days. --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler On Tue, Jan 18, 2022 at 7:26 AM bas van beek <bas.vanb...@hotmail.com> wrote: > 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. > > > -- > > Vincent Schut > > Remote Sensing Software Engineer > > +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: ri...@teachey.org >
_______________________________________________ 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