Skip to content

TypeAliasType aliases (jax.py/numpy.py/torch.py/cupy.py) break pyright under Python 3.10/3.11 despite requires-python >=3.10 #9

Description

@nstarman

Summary

bearshape's predefined array-type aliases (Shaped, Bool, I8C128, Int, Integer, Float, etc., and the *Like variants) are defined via typing.TypeAliasType(...) inside if tp.TYPE_CHECKING: blocks in bearshape/jax.py, numpy.py, torch.py, and cupy.py (e.g. jax.py:234-265):

if tp.TYPE_CHECKING:
  Shaped = tp.TypeAliasType("Shaped", JaxArray, type_params=(_Dims,))
  ...

typing.TypeAliasType (PEP 695) was only added to the stdlib in Python 3.12. bearshape declares requires-python = ">=3.10", so this is inconsistent with the package's own supported floor: any static type checker resolving Python 3.10 or 3.11 against these modules breaks.

Reproduction

With bearshape==0.0.1 installed under Python 3.11:

pyright --pythonversion 3.11 path/to/file_using_bearshape_types.py

produces, on every line that subscripts one of these types (e.g. Shaped[Array, "N"]):

error: Variable not allowed in type expression (reportInvalidTypeForm)

Re-running with --pythonversion 3.13 produces 0 errors for the same file — confirming this is purely a Python-version resolution issue in how these aliases are declared, not a usage error on the consumer's side.

This can silently escape a project's own CI if its pyright pre-commit hook (or any pinned pyright/mypy invocation) resolves a newer interpreter than the project's declared floor — the gap only surfaces when a downstream project explicitly checks at its own minimum supported Python version.

Suggested fix

Use the standard typing_extensions backport instead of typing.TypeAliasType when targeting <3.12, e.g.:

import typing_extensions as tpe

if tp.TYPE_CHECKING:
  Shaped = tpe.TypeAliasType("Shaped", JaxArray, type_params=(_Dims,))

across jax.py, numpy.py, torch.py, and cupy.py. This would need adding typing_extensions as a runtime dependency (only exercised under TYPE_CHECKING, so effectively type-checker-only, but still needs to resolve for tools like pyright/mypy) — currently bearshape only declares beartype>=0.20,<0.23.

For reference, quax hit the identical issue for a local TypeAliasType alias and fixed it the same way (swap typing.TypeAliasTypetyping_extensions.TypeAliasType), which is what prompted this report.

Happy to open a PR with this change across the four modules if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions