Skip to content
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

TYP: Type annotations overhaul, episode 2 #288

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

jorenham
Copy link

This is a WIP in progress, but feel free to say whatever you feel like saying about the stuff I've already touched at this point.


  • _internal
  • common/*
  • numpy
    • __init__
    • _aliases
    • _info
    • _typing
    • fft
    • linalg
  • torch.*
  • dask.array.*

Because cupy has no py.typed, and is too dynamic for pyright to analyse, it's pretty much impossible to annotate cupy.* at the moment.

Comment on lines +7 to +14
from typing import TYPE_CHECKING

def get_xp(xp):
__all__ = ["get_xp"]

if TYPE_CHECKING:
from collections.abc import Callable
from types import ModuleType
from typing import TypeVar
Copy link
Contributor

Choose a reason for hiding this comment

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

I much prefer avoiding if TYPE_CHECKING blocks when possible, and use them only when necessary (circular imports and runtime support for obsolete versions of python).
The performance implications of importing from typing at runtime are absolutely negligible.

_T = TypeVar("_T")


def get_xp(xp: "ModuleType") -> "Callable[[Callable[..., _T]], Callable[..., _T]]":
Copy link
Contributor

@crusaderky crusaderky Mar 28, 2025

Choose a reason for hiding this comment

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

please use bare annotations.
Where it would break things, because you either have a circular dependency or a if TYPE_CHECKING block, please add from __future__ import annotations to the top of the module.

from ._typing import Array, Device, DType, Namespace

if TYPE_CHECKING:
from typing_extensions import TypeIs
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
from typing_extensions import TypeIs
# TODO: import from typing (requires Python >=3.13)
from typing_extensions import TypeIs

these comments make upgrading minimum python version so much easier.

Copy link
Author

Choose a reason for hiding this comment

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

Yea makes sense.

pyupgrade (part of ruff) can also help a lot with that, fyi

@jorenham
Copy link
Author

I'm not really sure what's causing these test failures here 🤔

@crusaderky
Copy link
Contributor

I'm not really sure what's causing these test failures here 🤔

They're in main too

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.

2 participants