Skip to content

[v4.0] Update use of type annotations#1017

Merged
anoto-moniz merged 6 commits intorelease/v4.0from
use-new-python-features
Feb 19, 2026
Merged

[v4.0] Update use of type annotations#1017
anoto-moniz merged 6 commits intorelease/v4.0from
use-new-python-features

Conversation

@anoto-moniz
Copy link
Collaborator

A handful of updates and improvements came into type annotations in Python 3.9 and 3.10. The ones most relevant to us are the introduction of | for union types, the ability to use builtin classes as annotations (i.e. list), and the deprecation of a handful of types in typing in favor of referencing them in collections.abc.

We could also replace our usages of Optional[T] with T | None, but the latter syntax doesn't seem as clear to me.

We're also clear to start using match statements and the dictionary update operator, it's just harder to identify opportunities for them without knowing where to look.

Note: a couple classes (namely DataConceptsCollection and PredictorEvaluationCollection) still have methods that return List. It seems the list method defined in Collection is conflicting with the built-in in those cases. I'm not sure why, but will be investigating in a follow-up.

PR Type:

  • Breaking change (fix or feature that would cause existing functionality to change)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Maintenance (non-breaking change to assist developers)

Adherence to team decisions

  • I have added tests for 100% coverage
  • I have written Numpy-style docstrings for every method and class.
  • I have communicated the downstream consequences of the PR to others.
  • I have bumped the version in __version__.py

As of python 3.9, the builtin classes can be used as type annotations,
so there's no need to use the separate "typing" versions (which are also
deprecated).

Note that data_objects and predictor_evaluation retain List for now, as
it conflicts with their definition of a method called "list".
As of python 3.10, unions can be expressed with the pipe (|) operator.
Clean up all the unused imports created by the previous two changes. It
was easier to handle them all at once rather than as I went.
Many types introduced as type annotations in typing are now available
for use from collections.abc. The typing versions are deprecated, so we
switch to these newer versions. For us, this impacts Sequence, Iterator,
Iterable, Callable, and (in one place) Collection.
@anoto-moniz anoto-moniz marked this pull request as ready for review February 17, 2026 20:35
@anoto-moniz anoto-moniz requested a review from a team as a code owner February 17, 2026 20:35
Copy link
Collaborator

@kroenlein kroenlein left a comment

Choose a reason for hiding this comment

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

I transformed a lot of Optional[x] to x | None, and there are a lot more left. Seems like it's the better pattern.

Copy link
Collaborator

@kroenlein kroenlein left a comment

Choose a reason for hiding this comment

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

I transformed a lot of Optional[x] to x | None, and there are a lot more left. Seems like it's the better pattern.

Copy link
Collaborator

@kroenlein kroenlein left a comment

Choose a reason for hiding this comment

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

Additional minor clerical fixes.

if not isinstance(element_types, Iterable):
raise ValueError("element types must be iterable: {}".format(element_types))
self.element_types: typing.List[Property, ...] = \
self.element_types: list[Property, ...] = \
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self.element_types: list[Property, ...] = \
self.element_types: list[Property] = \

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tacked on to #1018

if not isinstance(element_types, list):
raise ValueError("element types must be a list: {}".format(element_types))
self.element_types: typing.List[Property, ...] = \
self.element_types: list[Property, ...] = \
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
self.element_types: list[Property, ...] = \
self.element_types: list[Property] = \

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tacked on to #1018

@anoto-moniz
Copy link
Collaborator Author

Additional minor clerical fixes.

From the description of the PR above:

Note: a couple classes (namely DataConceptsCollection and PredictorEvaluationCollection) still have methods that return List. It seems the list method defined in Collection is conflicting with the built-in in those cases. I'm not sure why, but will be investigating in a follow-up.

@anoto-moniz anoto-moniz merged commit 2a511a4 into release/v4.0 Feb 19, 2026
5 checks passed
@anoto-moniz anoto-moniz deleted the use-new-python-features branch February 19, 2026 14:01
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

Comments