-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[ty] Override __file__ to str when applicable on imported modules
#22333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AlexWaygood
merged 14 commits into
astral-sh:main
from
sinon:type-module-dunder-file-str
Jan 15, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
59fa481
[ty] Override `__file__` to str on imported modules
sinon bfb30ae
fix mdtest formatting, fix? failing tests
sinon 6777713
code should actually match what comment states
sinon 3572bcc
pyi __file__ shouldnt assume str
sinon c93b7b6
clippy
sinon a4b801a
respect type of __file__ when explictly defined
sinon de0a646
remove duplicate place
sinon dbc2588
code review
sinon b132319
add missing renamed md test file
sinon de0b9ad
improve wording
sinon 955f0f1
fixups
AlexWaygood df9e3f1
fixups
AlexWaygood 0d161ba
fixups
AlexWaygood 45c8ad4
fixups
AlexWaygood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
crates/ty_python_semantic/resources/mdtest/import/dunder_file_attribute.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # The `__file__` attribute on imported modules | ||
|
|
||
| ## Module successfully resolved | ||
|
|
||
| ```py | ||
| from b import __file__ as module_path | ||
| from stub import __file__ as stub_path | ||
| from override import __file__ as overidden_path | ||
|
|
||
| reveal_type(__file__) # revealed: str | ||
| reveal_type(module_path) # revealed: str | ||
| reveal_type(stub_path) # revealed: str | ||
| reveal_type(overidden_path) # revealed: None | ||
|
|
||
| # NOTE: This import fails at runtime as this is a C Extension | ||
| # with no `__file__` global. It's hard for us to determine this | ||
| # right now, however (all we know is it comes from a stub file), | ||
| # and if it did exist then it would be of type `str` since `sys` | ||
| # is not a namespace packages. This behaviour also matches other | ||
| # type checkers. | ||
| from sys import __file__ as no_path | ||
|
|
||
| reveal_type(no_path) # revealed: str | ||
| ``` | ||
|
|
||
| `b.py`: | ||
|
|
||
| ```py | ||
|
|
||
| ``` | ||
|
|
||
| `override.py`: | ||
|
|
||
| ```py | ||
| __file__ = None | ||
| ``` | ||
|
|
||
| `stub.pyi`: | ||
|
|
||
| ```pyi | ||
| ``` | ||
|
|
||
| ## Module resolution failed | ||
|
|
||
| ```py | ||
| from bar import __file__ as module_path # error: "Cannot resolve imported module `bar`" | ||
|
|
||
| reveal_type(module_path) # revealed: Unknown | ||
| ``` | ||
|
|
||
| ## Non-namespace packages have `__file__` available | ||
|
|
||
| `a/__init__.py`: | ||
|
|
||
| ```py | ||
| ``` | ||
|
|
||
| `a/b.py`: | ||
|
|
||
| ```py | ||
| ``` | ||
|
|
||
| ```py | ||
| import a | ||
|
|
||
| reveal_type(a.__file__) # revealed: str | ||
| ``` | ||
|
|
||
| ## `__file__` is set to `None` for namespace packages | ||
|
|
||
| `namespace/c.py`: | ||
|
|
||
| ```py | ||
| ``` | ||
|
|
||
| ```py | ||
| import namespace | ||
|
|
||
| # TODO: `__file__` does exist on namespace packages but is set to `None`; | ||
| # this is a false positive | ||
| # | ||
| # error: [unresolved-attribute] "Module `namespace` has no member `__file__`" | ||
| reveal_type(namespace.__file__) # revealed: Unknown | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.