Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

245
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda