Skip to content

chore: use if TYPE_CHECKING: blocks#2866

Open
mayeut wants to merge 1 commit into
pypa:mainfrom
mayeut:type-checking
Open

chore: use if TYPE_CHECKING: blocks#2866
mayeut wants to merge 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.

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