Skip to content

[mypyc] Make compilation order with multiple files consistent#21419

Merged
p-sawicki merged 2 commits intopython:masterfrom
p-sawicki:mypyc-consistent-compilation-order
May 7, 2026
Merged

[mypyc] Make compilation order with multiple files consistent#21419
p-sawicki merged 2 commits intopython:masterfrom
p-sawicki:mypyc-consistent-compilation-order

Conversation

@p-sawicki
Copy link
Copy Markdown
Collaborator

The mypyc plugin sets all modules inside a group as dependent on each other so they are part of a single SCC. When we compile the SCC, we compile its modules according to the order in scc.mod_ids. mod_ids is a set so the order is non-deterministic.

This might result in random failures when multiple files are compiled together and we have a bug in mypyc caused by relying on data that is dependent on the compilation order. For example if we are compiling two files a.py and b.py each with a single class and an attribute of ClassIR representing the class A that is not set during the preparation phase is used when compiling the class B, the result will depend on whether a.py was compiled before b.py.

To fix, make the order deterministic based on module names by sorting them first. Also sort the source files and groups passed to mypycify so that invoking mypyc a.py b.py and mypyc b.py a.py produces identical results.

@p-sawicki p-sawicki force-pushed the mypyc-consistent-compilation-order branch from af4d296 to 656cd4f Compare May 6, 2026 13:33
@p-sawicki p-sawicki merged commit e80db52 into python:master May 7, 2026
17 checks passed
@p-sawicki p-sawicki deleted the mypyc-consistent-compilation-order branch May 7, 2026 10:18
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