Skip to content

Work around concurrent import bug on free-threaded Python - #22120

Merged
ilevkivskyi merged 1 commit into
python:masterfrom
p-sawicki:nativeparse-import-freethreaded-workaround
Oct 6, 2026
Merged

ilevkivskyi merged 1 commit into
python:masterfrom
p-sawicki:nativeparse-import-freethreaded-workaround

Conversation

@p-sawicki

Copy link
Copy Markdown
Collaborator

CI sometimes fails when accessing mypy.nativeparse after importing it. This appears to be caused by a race condition in importlib where a child module can be initialized without being placed in its parent's __dict__. More details in this comment.

Change the imports to from mypy import nativeparse as a workaround. This way importlib falls back to sys.modules when it can't find the child attribute in parent's __dict__.

It also turns out that our command line tests in the wheel build CI launch interpreted mypy because PYTHONPATH is set to the source directory but the wheel build does not place the compiled extensions there. Change PYTHONPATH to the parent directory of the mypy package to use the compiled version when available.

@p-sawicki
p-sawicki requested a review from ilevkivskyi October 6, 2026 11:12
@github-actions

github-actions Bot commented Oct 6, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@ilevkivskyi
ilevkivskyi merged commit e576c20 into python:master Oct 6, 2026
25 checks passed
@p-sawicki
p-sawicki deleted the nativeparse-import-freethreaded-workaround branch October 6, 2026 12:20
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