Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda