Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
Python, numpy and type hints: a tale of woe

I'm trying to write some basic Python code using numpy, and as someone who is pretty committed to the concept of strict type checking, I'm finding it a bit challenging.

I'm using VS Code with pylance, and my environment is somewhat version and library constrained.

One simple example:

flags = chi_squared > .5          # chi_squared is type np.ndarray
indices = chi_squared.argsort()

The first line is fine; no type warnings, the types of everything are known (even if flags is "Any"). The second line, however, gives me a type error because "the type of argsort() is partially unknown".

That's odd... but when I tracked down the numpy type stubs, I find that argsort is in fact partially undefined:

def argsort(self, axis=..., kind=..., order=...) -> typing.Any:
        'a.argsort(axis=-1, kind=None, order=None)\n\n    Returns the indices that would sort this array.\n\n    Refer to `numpy.argsort` for full documentation.\n\n    See Also\n    --------\n    numpy.argsort : equivalent function'
        ...

axis, kind, and order have no types (and returning Any is kind of lame as well... this function can really return any type?)

As a result, about half the lines in my code have cascading type errors like this, so my code is filled with # type: ignore directives.

I get that types in Python are a sort of bolt-on afterthought, and that libraries like numpy do some magical things, so I don't expect perfection, but I'm left wondering if there is a sane way to get the benefits of at least some type checking while using numpy, without littering my code with ignore statements.

What is the best way forward?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!