Skip to content

Tighten numpy.linalg.qr annotation #28997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

NeilGirdhar
Copy link
Contributor

No description provided.

@NeilGirdhar
Copy link
Contributor Author

@jakevdp (Sorry if this is too many annotation PRs)

@dfm dfm self-assigned this May 25, 2025
Copy link
Contributor

@dfm dfm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok to me, but it's not totally obvious to me that there's a huge benefit to including all the supported literals in the type as demonstrated by the fact that this is already inconsistent with the runtime values that are supported. What do you think?


@export
@partial(jit, static_argnames=('mode',))
def qr(a: ArrayLike, mode: str = "reduced") -> Array | QRResult:
def qr(a: ArrayLike,
mode: Literal["reduced", "complete", "raw", "r"] = "reduced",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like "full" is also supported. I wonder if we should add that too, but this also means that we need to keep this annotation in sync with runtime values supported. Is it possible to annotate "any string except the literal "r""?

@jakevdp
Copy link
Collaborator

jakevdp commented May 27, 2025

I've typically avoided Literal in type annotations outside specific overloads, because then if you do something like this:

mode: str = "reduced"
qr(..., mode=mode)

it will result in a type error, becuase str is not a valid type for mode. In my experience this kind of pattern is used often enough that changes like this lead to many downstream failures, and the effort required to land them outweighs the benefit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants