Update typing aliases to use collections.abc instead#1677
Conversation
|
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request performs a large-scale refactoring of type annotations across the codebase to align with modern Python standards (PEP 585). Specifically, it replaces deprecated typing module aliases like List, Dict, Tuple, Set, and Type with their built-in lowercase counterparts. Additionally, it migrates abstract collection types such as Iterable, Sequence, Callable, Iterator, and Mapping from typing to collections.abc. These changes are applied consistently across Python source files, development tools, and Jupyter notebooks, resulting in cleaner code and reduced reliance on the typing module. I have no feedback to provide.
mhucka
left a comment
There was a problem hiding this comment.
Thanks for doing this!
This is a monster PR and it's hard to do individual line-oriented comments, so I'll have to summarize change requests and ask you to find the individual instances. Overall it looks good! There are just a few repeated patterns of changes that can be improved:
-
Some of the new
Uniontypes can be rewritten to simpler forms. There are two cases I see:-
A type declaration of the form
Union[X, None]can be rewritten asX | None, but it is safest to do this only if theXis not a quoted type name. For example, the case ofcall_graph_example: Union[BloqExample, None] = field()
can be rewritten as
call_graph_example: BloqExample | None = field()
-
Unions of the form
Union[X, None]are also equivalent toOptional[X], and this is safer to use this variant if theXis a quoted type name. So for example,Union['cirq.Operation', None]
can be converted to
Optional['cirq.Operation']
-
-
Some combinations of Optional and Union can be further simplified. Here is an example:
target_bitsizes: Optional[Union[SymbolicInt, tuple[SymbolicInt, ...]]] = None
can be
target_bitsizes: SymbolicInt | tuple[SymbolicInt, ...] | None = None
-
There are some remaining uses of
typing.Type. I'm looking atqualtran/bloqs/data_loading/qroam_clean.pybut are probably others. Basically, things likecls: Type['QROAMCleanAdjoint']can becls: type['QROAMCleanAdjoint']and the import ofTypefromtypingcan be dropped.
That's all I have for now.
Fixes #1653
following the recommendations described in the issue, replace all instances of deprecated aliases with the correct ones / built-ins.
This removes compatibility with pre-
3.10python versions, but Qualtran isn't intended to target those anymore.