Skip to content

Update typing aliases to use collections.abc instead#1677

Open
micpap25 wants to merge 20 commits into
quantumlib:mainfrom
micpap25:typings-update
Open

Update typing aliases to use collections.abc instead#1677
micpap25 wants to merge 20 commits into
quantumlib:mainfrom
micpap25:typings-update

Conversation

@micpap25

@micpap25 micpap25 commented Jul 16, 2025

Copy link
Copy Markdown
Contributor

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.10 python versions, but Qualtran isn't intended to target those anymore.

@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@mhucka

mhucka commented May 17, 2026

Copy link
Copy Markdown
Contributor

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 mhucka left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 Union types can be rewritten to simpler forms. There are two cases I see:

    • A type declaration of the form Union[X, None] can be rewritten as X | None, but it is safest to do this only if the X is not a quoted type name. For example, the case of

      call_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 to Optional[X], and this is safer to use this variant if the X is 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 at qualtran/bloqs/data_loading/qroam_clean.py but are probably others. Basically, things like cls: Type['QROAMCleanAdjoint'] can be cls: type['QROAMCleanAdjoint'] and the import of Type from typing can be dropped.

That's all I have for now.

mhucka added a commit that referenced this pull request Jun 15, 2026
…ections` (#1851)

Partial on #1653 
As suggested by @mhucka isolate the changes from #1677 to smaller
subsets of the codebase so that the review process is easier.
This PR removes compatibility with pre-3.10 python versions, but
Qualtran isn't intended to target those anymore.

---------

Co-authored-by: Michael Hucka <mhucka@google.com>
@micpap25

Copy link
Copy Markdown
Contributor Author

Hi @mhucka Thank you for the detailed feedback, just to confirm that this is in addition to the changes to typing.Set and annotations that were discussed in the smaller PR?

@mhucka

mhucka commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Hi @mhucka Thank you for the detailed feedback, just to confirm that this is in addition to the changes to typing.Set and annotations that were discussed in the smaller PR?

Hi @micpap25 – thanks for coming back to this. Yes, I think that, basically, if there are cases of upper case Set, it should be possible to replace them with the lower-case variant. The tricky cases are what we discussed before about quoted type names.

@mhucka mhucka self-assigned this Jun 19, 2026
@micpap25

Copy link
Copy Markdown
Contributor Author

@mhucka What about using collections.abc.Set in place of typing.Set? Does that work in the trickier cases?

@micpap25

Copy link
Copy Markdown
Contributor Author

@mhucka It seems there are some issues with the testing, in particular Pylint seems to have issues with the changes to Union and Optional you described.

Once we resolve this, I'm happy to continue the work on Union and Optional though I think it might constitute a new PR.

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.

replace deprecated typing aliases with those from collections

2 participants