Skip to content

dev-tools replace deprecated typing aliases with those from collections#1851

Open
micpap25 wants to merge 11 commits into
quantumlib:mainfrom
micpap25:typing-update-dev-tools
Open

dev-tools replace deprecated typing aliases with those from collections#1851
micpap25 wants to merge 11 commits into
quantumlib:mainfrom
micpap25:typing-update-dev-tools

Conversation

@micpap25
Copy link
Copy Markdown
Contributor

@micpap25 micpap25 commented May 8, 2026

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.

Remove pre-3.10 python version targeting, and for the dev-tools folder, update typing classes to modern standards.
Copy link
Copy Markdown
Contributor

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

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 updates type annotations across the codebase to use built-in collection types and collections.abc equivalents, replacing the older typing module generics. It also simplifies imports in protocol buffer interface files by removing conditional typing_extensions logic in favor of the standard typing module. I have no feedback to provide as there were no review comments.

Copy link
Copy Markdown
Contributor

@mhucka mhucka left a comment

Choose a reason for hiding this comment

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

Thanks for this work! It's quite thorough.

I found one place where there's a subtle bug, and a few suggestions for minor tweaks elsewhere, but otherwise this is almost done!

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.

Just double-checking: @micpap25 how did you (re)generate the .pyi files?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure why it doesn't show the diffs because they are quite straightforward, but I do just manually change them to remove typing and python 3.8 support like any other file.

Copy link
Copy Markdown
Contributor

@mhucka mhucka May 17, 2026

Choose a reason for hiding this comment

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

Ah. _pb2.pyi files are usually generated by a program, not written manually, and I guess GitHub doesn't make diffs for them by default.

I think this project uses the script in dev_tools/build-protos.sh to generate them, but I've never done it myself, so have to defer to @mpharrigan or @tanujkhattar about the exact procedure that is supposed to be used.

(The hand-edited changes look reasonable to my eyes, but in principle, there's a risk that the other type changes elsewhere will affect what dev_tools/build-protos.sh will produce. I would rather err on the side of caution than introduce some subtle change that might not be picked up by existing unit test cases yet might affect things like the Qualtran web service.)

if annot['ssa'] != 'SympySymbolAllocator':
print(f"{bc}.build_call_graph `ssa: 'SympySymbolAllocator'`")
if annot['return'] != Set[ForwardRef('BloqCountT')]: # type: ignore[misc]
if annot['return'] != set[ForwardRef('BloqCountT')]: # type: ignore[misc]
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.

This one is incorrect. The typing.Set and the built-in set are not considered equal when used in type annotations, even if they contain the same arguments. Here is a small test program to demonstrate:

from typing import ForwardRef, Set

# Original annotation using typing.Set
original = Set[ForwardRef('BloqCountT')]

# PEP 585 annotation using built-in set
pep585 = set[ForwardRef('BloqCountT')]

print(f"Is {original} == {pep585}?")
print(f"Result: {original == pep585}")

In the typing module, parameterizing a type with a string automatically wraps that string in a ForwardRef object. That meant that typing.Set['BloqCountT'] was identical to typing.Set[typing.ForwardRef('BloqCountT')] in the previous version of the code. However, PEP 585's built-in collections do not auto-convert strings to ForwardRef objects. When it evaluates set['BloqCountT'], Python directly creates a types.GenericAlias containing the literal string 'BloqCountT'.

The fix in this particular case turns out to be easy: get rid of the ForwardRef completely and do:

    if annot['return'] != set['BloqCountT']:  # type: ignore[misc]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good to know, because I replace typing.Set with set throughout the other parts of the code; I could go and fix that as well / remove the ForwardRef usage there too? I will make the suggested fix, though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Regarding this bug, it seems that making the change you recommended causes an issue in the dev-tools CI about BloqCountT being undefined. It seems fixing this for every type typing.Set is changed to the built-in set will require a lot of TYPE_CHECKING imports?

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.

Huh. Let me look at it over the weekend.

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.

Regarding this bug, it seems that making the change you recommended causes an issue in the dev-tools CI about BloqCountT being undefined. It seems fixing this for every type typing.Set is changed to the built-in set will require a lot of TYPE_CHECKING imports?

I don't have an answer to this yet, but I see all the other comments have been resolved (thank you), so what's left is this question and the .pyi files question above.

Comment thread dev_tools/qualtran_dev_tools/reference_docs.py Outdated
Comment thread dev_tools/qualtran_dev_tools/bloq_report_card.py Outdated
Comment thread dev_tools/qualtran_dev_tools/bloq_report_card.py Outdated
Comment thread dev_tools/qualtran_dev_tools/make_reference_docs/_components/render_docstring.py Outdated
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