Skip to content

chore: use if TYPE_CHECKING: blocks#2866

Merged
henryiii merged 1 commit into
pypa:mainfrom
mayeut:type-checking
May 27, 2026
Merged

chore: use if TYPE_CHECKING: blocks#2866
henryiii merged 1 commit into
pypa:mainfrom
mayeut:type-checking

Conversation

@mayeut
Copy link
Copy Markdown
Member

@mayeut mayeut commented May 23, 2026

This will become the default in Python 3.14 in any case given the current lint rules.
It ensures typing only imports do not slow down execution (admittedly only useful in edge use cases, such as querying help).

If #2797 makes it, it will further speed-up this one:

hyperfine --warmup 10 \
     -n "main"                --prepare "git checkout main"               ".nox/tests-3-15/bin/python -m cibuildwheel --help" \
     -n "Henry's PR"          --prepare "git checkout pr/2797"            ".nox/tests-3-15/bin/python -m cibuildwheel --help" \
     -n "type-checking"       --prepare "git checkout type-checking"      ".nox/tests-3-15/bin/python -m cibuildwheel --help" \
     -n "type-checking-lazy"  --prepare "git checkout type-checking-lazy" ".nox/tests-3-15/bin/python -m cibuildwheel --help"
Benchmark 1: main
  Time (mean ± σ):     308.7 ms ±  16.7 ms    [User: 194.7 ms, System: 44.1 ms]
  Range (min … max):   294.3 ms … 349.3 ms    10 runs
 
Benchmark 2: Henry's PR
  Time (mean ± σ):     106.2 ms ±   9.4 ms    [User: 62.7 ms, System: 17.6 ms]
  Range (min … max):    88.7 ms … 118.2 ms    19 runs
 
Benchmark 3: type-checking (this PR)
  Time (mean ± σ):     306.9 ms ±  15.5 ms    [User: 194.2 ms, System: 43.2 ms]
  Range (min … max):   290.2 ms … 342.8 ms    10 runs
 
Benchmark 4: type-checking-lazy
  Time (mean ± σ):      77.5 ms ±   6.1 ms    [User: 45.6 ms, System: 13.7 ms]
  Range (min … max):    68.9 ms …  86.3 ms    22 runs
 
Summary
  type-checking-lazy ran
    1.37 ± 0.16 times faster than Henry's PR
    3.96 ± 0.37 times faster than type-checking (this PR)
    3.98 ± 0.38 times faster than main

Copy link
Copy Markdown
Contributor

@henryiii henryiii left a comment

Choose a reason for hiding this comment

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

Sounds good to me.

Copy link
Copy Markdown
Contributor

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

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

All makes sense to me

@mayeut
Copy link
Copy Markdown
Member Author

mayeut commented May 25, 2026

After further thoughts, we might want to:

  • re-allow typing.TYPE_CHECKING (use forbidden in chore: avoid importing TYPE_CHECKING #2864) as typing is always imported by cibuildwheel. If PEP 781 makes it, we can forbid typing.TYPE_CHECKING once dropping the Python version before its introduction. Only the fact that there are TYPE_CHECKING blocks is important for fix: faster CLI on Python 3.15 #2797. Whether we use TYPE_CHECKING = False or typing.TYPE_CHECKING doesn't change the fact that typing is always imported.
  • disable TC checks for tests where there's no added value in timings at all I think

Any thoughts ?

@mayeut mayeut marked this pull request as draft May 25, 2026 09:01
@mayeut
Copy link
Copy Markdown
Member Author

mayeut commented May 25, 2026

I've implemented the proposal from the previous comment in #2869
Changed the status to draft until there's a consensus on which PR to keep if any.

@henryiii
Copy link
Copy Markdown
Contributor

I like this version fine - TYPE_CHECKING = False is accepted by all the type checkers (including the new Rust ones), and is the only proper workaround that works with lazy imports too until PEP 781 makes it. I'd be fine to see it everywhere, might help get PEP 781 in. :)

In timings, typing forces an re import, which is quite expensive.

I'm fine to keep tests consistent, though I don't really care, we don't have to.

@joerick
Copy link
Copy Markdown
Contributor

joerick commented May 26, 2026

I don't have a strong opinion, but the TYPE_CHECKING = False thing is fine by me!

@mayeut mayeut marked this pull request as ready for review May 26, 2026 18:23
@mayeut
Copy link
Copy Markdown
Member Author

mayeut commented May 26, 2026

In timings, typing forces an re import, which is quite expensive.

Just as a note, typing is always imported by cibuildwheel - at least for now.

I closed the other PR and kept this one as I don't have a strong opinion either.

@henryiii henryiii merged commit e3e7cc9 into pypa:main May 27, 2026
46 checks passed
@mayeut mayeut deleted the type-checking branch May 29, 2026 18:56
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.

5 participants