Skip to content

Support --package-root with native parser#21321

Merged
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:fix-package-root-native
Apr 25, 2026
Merged

Support --package-root with native parser#21321
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:fix-package-root-native

Conversation

@ilevkivskyi
Copy link
Copy Markdown
Member

When --package-root is used, fscache creates some fake __init__.py files on-the-fly (that don't actually exist). These obviously can't be handed by the native parser, but fscache.exists() returns True for them, causing a crash. One possible fix is to simply use os.path.isfile() everywhere. But I think fscache can give us a little perf boost, so I instead add a real_only flag to fscache.exists().

cc @JukkaL

@hauntsaninja hauntsaninja changed the title Suport --package-root with native parser Support --package-root with native parser Apr 24, 2026
@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

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

The way the fake package cache interacts with the native parser is pretty sketchy and we may want to do it differently eventually, but this is fine as a quick fix.

Comment thread mypy/fscache.py Outdated
if st is None:
return False
if real_only:
dirname, _ = os.path.split(path)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

os.path.dirname(path) may be faster, as it may construct one fewer substring (but haven't verified).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OoC I checked and it is indeed ~20% faster.

@ilevkivskyi
Copy link
Copy Markdown
Member Author

The way the fake package cache interacts with the native parser is pretty sketchy and we may want to do it differently eventually, but this is fine as a quick fix.

Yeah, --package-root is one of those dark corners I didn't risk touching. But also we will hopefully have string parsing support in native parser soon, see mypyc/ast_serialize#54. When we will have that, and the old parser will be deleted, we will be able to simplify a bunch of things.

@github-actions
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 merged commit bbe68d3 into python:master Apr 25, 2026
24 checks passed
@ilevkivskyi ilevkivskyi deleted the fix-package-root-native branch April 25, 2026 15:05
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.

3 participants