-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Implement module-level __getattr__ support
#19791
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b1de2f7
4a6aa3b
c398235
b5d5404
93d0082
41f7831
fb4f142
2cfb671
e1b1776
7728ceb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # Module-level `__getattr__` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought of one other case we are missing. We don't necessarily need to add support for it in this PR (though if you are up for it, that's also fine!), but I think we should at least add a test for it with TODO comment to help us keep track of the fact that it's missing. At runtime if module
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I didn't understand properly what you meant.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh! My bad, I thought I had tested this and it wasn't working, but I must have done something wrong. That's great that we actually only need to do this in one place :) The new test looks excellent, thank you! |
||
|
|
||
| ## Basic functionality | ||
|
|
||
| ```py | ||
| import module_with_getattr | ||
|
|
||
| # Should work: module `__getattr__` returns `str` | ||
| reveal_type(module_with_getattr.whatever) # revealed: str | ||
| ``` | ||
|
|
||
| `module_with_getattr.py`: | ||
|
|
||
| ```py | ||
| def __getattr__(name: str) -> str: | ||
| return "hi" | ||
| ``` | ||
|
|
||
| ## `from import` with `__getattr__` | ||
|
|
||
| At runtime, if `module` has a `__getattr__` implementation, you can do `from module import whatever` | ||
| and it will exercise the `__getattr__` when `whatever` is not found as a normal attribute. | ||
|
|
||
| ```py | ||
| from module_with_getattr import nonexistent_attr | ||
|
|
||
| reveal_type(nonexistent_attr) # revealed: int | ||
| ``` | ||
|
|
||
| `module_with_getattr.py`: | ||
|
|
||
| ```py | ||
| def __getattr__(name: str) -> int: | ||
| return 42 | ||
| ``` | ||
|
|
||
| ## Precedence: explicit attributes take priority over `__getattr__` | ||
|
|
||
| ```py | ||
| import mixed_module | ||
|
|
||
| # Explicit attribute should take precedence | ||
| reveal_type(mixed_module.explicit_attr) # revealed: Unknown | Literal["explicit"] | ||
|
|
||
| # `__getattr__` should handle unknown attributes | ||
| reveal_type(mixed_module.dynamic_attr) # revealed: str | ||
| ``` | ||
|
|
||
| `mixed_module.py`: | ||
|
|
||
| ```py | ||
| explicit_attr = "explicit" | ||
|
|
||
| def __getattr__(name: str) -> str: | ||
| return "dynamic" | ||
| ``` | ||
|
|
||
| ## Precedence: submodules vs `__getattr__` | ||
|
|
||
| If a package's `__init__.py` (e.g. `mod/__init__.py`) defines a `__getattr__` function, and there is | ||
| also a submodule file present (e.g. `mod/sub.py`), then: | ||
|
|
||
| - If you do `import mod` (without importing the submodule directly), accessing `mod.sub` will call | ||
| `mod.__getattr__('sub')`, so `reveal_type(mod.sub)` will show the return type of `__getattr__`. | ||
| - If you do `import mod.sub` (importing the submodule directly), then `mod.sub` refers to the actual | ||
| submodule, so `reveal_type(mod.sub)` will show the type of the submodule itself. | ||
|
|
||
| `mod/__init__.py`: | ||
|
|
||
| ```py | ||
| def __getattr__(name: str) -> str: | ||
| return "from_getattr" | ||
| ``` | ||
|
|
||
| `mod/sub.py`: | ||
|
|
||
| ```py | ||
| value = 42 | ||
| ``` | ||
|
|
||
| `test_import_mod.py`: | ||
|
|
||
| ```py | ||
| import mod | ||
|
|
||
| reveal_type(mod.sub) # revealed: str | ||
| ``` | ||
|
|
||
| `test_import_mod_sub.py`: | ||
|
|
||
| ```py | ||
| import mod.sub | ||
|
|
||
| reveal_type(mod.sub) # revealed: <module 'mod.sub'> | ||
| ``` | ||
Uh oh!
There was an error while loading. Please reload this page.