Fix sys.path ordering: single-file module from earlier root beats package from later root#3962
Fix sys.path ordering: single-file module from earlier root beats package from later root#3962kain88-de wants to merge 1 commit into
Conversation
…kage from later root Python's sys.path semantics are first-match-wins regardless of whether the match is a .py file or a package directory. When widget.py exists in root0 and widget/__init__.py exists in root1, root0's module should win. The bug was in FindResult::best_result, which unconditionally placed RegularPackage and LegacyNamespacePackage at the top priority tier. This meant a package from any fallback root would override a single-file module found earlier in the search path. The fix adds an explicit match arm before the RegularPackage tier: when a is a SingleFilePyModule or CompiledModule and b is a package type, a wins. This preserves the existing behavior where .pyi stubs from later roots still beat .py implementations (handled by the subsequent SingleFilePyiModule arm).
|
Hi @kain88-de! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Summary
Python's sys.path semantics are first-match-wins regardless of whether the match is a .py file or a package directory. When widget.py exists in root0 and widget/init.py exists in root1, root0's module should win.
The bug was in FindResult::best_result, which unconditionally placed RegularPackage and LegacyNamespacePackage at the top priority tier. This meant a package from any fallback root would override a single-file module found earlier in the search path.
The fix adds an explicit match arm before the RegularPackage tier: when a is a SingleFilePyModule or CompiledModule and b is a package type, a wins. This preserves the existing behavior where .pyi stubs from later roots still beat .py implementations (handled by the subsequent SingleFilePyiModule arm).
Fixes #3407
Test Plan
Two new tests covering the bug directly:
test on find_module
type error when importing from the correctly resolved module