astropy: Typing module error: "str" is not assignable to type "Literal"

Simple example code:

import numpy as np
from astropy.stats import calculate_bin_edges

def func(bin_method: str = "knuth"): # Same error for the other methods
    calculate_bin_edges(np.array([]), bins=bin_method)

Pyright returns:

$pyright --pythonpath=/home/gabriel/miniconda3/envs/astropy-dev/bin/python test.py
/home/gabriel/Github/test.py
  /home/gabriel/Github/test.py:6:44 - error: Argument of type "str" cannot be assigned to parameter "bins" of type "int | list[float] | Literal['blocks', 'knuth', 'scott', 'freedman'] | None" in function "calculate_bin_edges"
    Type "str" is not assignable to type "int | list[float] | Literal['blocks', 'knuth', 'scott', 'freedman'] | None"
      "str" is not assignable to "int"
      "str" is not assignable to "list[float]"
      "str" is not assignable to "None"
      "str" is not assignable to type "Literal['blocks']"
      "str" is not assignable to type "Literal['knuth']"
      "str" is not assignable to type "Literal['scott']"
      "str" is not assignable to type "Literal['freedman']" (reportArgumentType)
1 error, 0 warnings, 0 informations 

I guess the more general issue is: since the typing module has been on a deprecation path since 3.9, should astropy start phasing out its usage?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 27 (27 by maintainers)

Most upvoted comments

Perhaps my message came out more harsh than I intended.

There will always be disagreements on how to develop such a large project. In such cases, with no clear majority vote in place, the opinion of an active developer like @neutrinoceros should definitely have more weight than that of a casual contributor such as myself.

I’m happy with the discussion and its outcome even if it didn’t go my way.

Cheers

You have not convinced me and I still believe astropy does a disservice to its users by not simplifying the type checking, but I agree that I have not gathered any type of support so closing is the reasonable thing to do.

Cheers

@neutrinoceros has already explained how using Literal in the annotation is better for type checking and how it is better for IDE integration. I will attempt to write an answer informative for someone that just wants the error to go away.

At least in numpy 2 histogram_bin_edges() is annotated using Literal, not str (see my previous post for the link to the annotation). Our calculate_bin_edges() calls histogram_bin_edges(), so replacing the Literal with str might avoid explicit type checking errors in some dowstream code, but then we would be getting those type checking errors instead. Why would we agree to that?

In any case Literal is from the Python standard library, so it is always available, and we do not require downstream to use it at runtime.

The claim that numpy does not use Literal might have been true with older versions, but it’s certainly not true anymore: https://github.com/numpy/numpy/blob/26d2c0ecace996c9d75d86720151f3f701603aa3/numpy/lib/_histograms_impl.pyi

In any case our use of Literal doesn’t matter at runtime, so nobody downstream is forced to import it if they don’t want to.

It is reasonable to assume that the people who care about typing would appreciate more detailed types and the people who don’t care about typing will not bother running a type checker.