Registry Mirror
Skip to content

Conversation

@headtr1ck

@headtr1ck headtr1ck commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Follow-up to #11649 (based on it, so it includes its commits until that is merged), using typing features of Python 3.12:

  • Enable the ruff rules UP040, UP046 and UP047 and use PEP 695 type parameters and type statements instead of TypeVar, Generic and TypeAlias.
  • Define NestedSequence and NestedDict as recursive type aliases instead of a Protocol and a quoted forward reference.
  • Mark methods that override a method of one of xarray's own classes with @typing.override (PEP 698), so type checkers report it when the overridden method is renamed or removed.
  • Allow __type_params__ in the stubtest allowlist: classes with type parameters have it at runtime, and stubtest reports it as missing from the stub.

Exceptions:

  • NamedArray keeps Generic with its covariant TypeVars: PEP 695 infers variance, and its mutable data would make it invariant.
  • ResampleCompatible stays a TypeAlias, as it is used in isinstance checks.
  • The shared TypeVars in xarray.core.types are kept, since they are still used by functions and downstream packages.
  • No @override for overrides of builtins and the collections.abc/typing ABCs, in generated files and tests, and for properties with a setter, which mypy cannot check yet.

mypy and stubtest report no new errors, and the tests pass on Python 3.12 and 3.14.

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
      Tools: Claude Code (Claude Opus 5.5)

🤖 Generated with Claude Code

headtr1ck and others added 3 commits September 30, 2026 20:51
Python 3.12 is now the minimum, in line with the minimum dependency policy.

- Bump requires-python, the classifiers (also adding 3.14), the binder, asv
  and bug report environments and the installation docs.
- Rename the py311 pixi feature and the test-py311* environments to py312 and
  run the minimum version environments on Python 3.12.
- Remove the version checks for Python < 3.12 in NodePath and
  test_namedarray.
- Reformat an f-string that ruff now formats with PEP 701 quoting.
- Ignore the PEP 695 ruff rules (UP040, UP046, UP047) that apply now; the
  migration from TypeVar and TypeAlias is left for a separate change.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Now that Python 3.12 is the minimum, enable the ruff rules UP040, UP046 and
UP047 and use type parameters and type statements instead of TypeVar,
Generic and TypeAlias.

- Classes using the TypeVars shared through xarray.core.types restate their
  bounds or constraints; the shared TypeVars are kept, as many functions and
  downstream packages still use them.
- NamedArray keeps Generic with its covariant TypeVars: PEP 695 infers
  variance, and its mutable data makes it invariant.
- ResampleCompatible stays a TypeAlias, as it is used in isinstance checks.
- Remove the TypeVars that are no longer used, and quotes and Union in bounds
  and type statements, which are evaluated lazily.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added topic-backends topic-indexing topic-CF conventions topic-groupby topic-plotting topic-zarr Related to zarr storage library topic-rolling Automation Github bots, testing workflows, release automation topic-DataTree Related to the implementation of a DataTree class topic-typing io topic-NamedArray Lightweight version of Variable labels Sep 30, 2026
headtr1ck and others added 3 commits September 30, 2026 21:40
With type statements, aliases can refer to themselves directly, so
NestedSequence no longer needs the Protocol copied from numpy, and
NestedDict no longer needs a quoted forward reference, which also makes it
properly generic at runtime.

The stricter NestedSequence showed that _infer_tile_ids_from_nested_list
was called with leaves as well; it now only recurses into sequences.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Add typing.override (Python >= 3.12) to the methods that override a method
of one of xarray's own classes, e.g. of Index, the backend stores and
entrypoints, the coders, the cftime offsets, the indexing adapters and
Coordinates. Type checkers then report it if the overridden method is
renamed or removed.

Not covered: overrides of builtins and of the collections.abc and typing
ABCs, the generated files, the tests, and properties with a setter, which
mypy cannot check yet.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Classes with type parameters have a __type_params__ attribute at runtime,
which stubtest reports as missing from the stub, e.g. for
xarray.core.dataarray._LocIndexer.

Co-authored-by: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@headtr1ck
headtr1ck enabled auto-merge (squash) October 1, 2026 08:04
Co-authored-by: Claude <noreply@anthropic.com>
@headtr1ck
headtr1ck merged commit e20d3f7 into pydata:main Oct 1, 2026
45 checks passed
@headtr1ck
headtr1ck deleted the pep695-type-syntax branch October 1, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Automation Github bots, testing workflows, release automation io topic-backends topic-CF conventions topic-cftime topic-DataTree Related to the implementation of a DataTree class topic-groupby topic-indexing topic-NamedArray Lightweight version of Variable topic-plotting topic-rolling topic-typing topic-zarr Related to zarr storage library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants