-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[ty] Add support for __qualname__ and other implicit class attributes
#21966
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
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,120 @@ | ||
| # Implicit class body attributes | ||
|
|
||
| ## Class body implicit attributes | ||
|
|
||
| Python makes certain names available implicitly inside class body scopes. These are `__qualname__`, | ||
| `__module__`, and `__doc__`, as documented at | ||
| <https://docs.python.org/3/reference/datamodel.html#creating-the-class-object>. | ||
|
|
||
| ```py | ||
| class Foo: | ||
| reveal_type(__qualname__) # revealed: str | ||
| reveal_type(__module__) # revealed: str | ||
| reveal_type(__doc__) # revealed: str | None | ||
| ``` | ||
|
|
||
| ## `__firstlineno__` (Python 3.13+) | ||
|
|
||
| Python 3.13 added `__firstlineno__` to the class body namespace. | ||
|
|
||
| ### Available in Python 3.13+ | ||
|
|
||
| ```toml | ||
| [environment] | ||
| python-version = "3.13" | ||
| ``` | ||
|
|
||
| ```py | ||
| class Foo: | ||
| reveal_type(__firstlineno__) # revealed: int | ||
| ``` | ||
|
|
||
| ### Not available in Python 3.12 and earlier | ||
|
|
||
| ```toml | ||
| [environment] | ||
| python-version = "3.12" | ||
| ``` | ||
|
|
||
| ```py | ||
| class Foo: | ||
| # error: [unresolved-reference] | ||
| __firstlineno__ | ||
| ``` | ||
|
|
||
| ## Nested classes | ||
|
|
||
| These implicit attributes are also available in nested classes, and refer to the nested class: | ||
|
|
||
| ```py | ||
| class Outer: | ||
| class Inner: | ||
| reveal_type(__qualname__) # revealed: str | ||
| reveal_type(__module__) # revealed: str | ||
| ``` | ||
|
|
||
| ## Class body implicit attributes have priority over globals | ||
|
|
||
| If a global variable with the same name exists, the class body implicit attribute takes priority | ||
| within the class body: | ||
|
|
||
| ```py | ||
| __qualname__ = 42 | ||
| __module__ = 42 | ||
|
|
||
| class Foo: | ||
| # Inside the class body, these are the implicit class attributes | ||
| reveal_type(__qualname__) # revealed: str | ||
| reveal_type(__module__) # revealed: str | ||
|
|
||
| # Outside the class, the globals are visible | ||
| reveal_type(__qualname__) # revealed: Literal[42] | ||
| reveal_type(__module__) # revealed: Literal[42] | ||
| ``` | ||
|
|
||
| ## `__firstlineno__` has priority over globals (Python 3.13+) | ||
|
|
||
| The same applies to `__firstlineno__` on Python 3.13+: | ||
|
|
||
| ```toml | ||
| [environment] | ||
| python-version = "3.13" | ||
| ``` | ||
|
|
||
| ```py | ||
| __firstlineno__ = "not an int" | ||
|
|
||
| class Foo: | ||
| reveal_type(__firstlineno__) # revealed: int | ||
|
|
||
| reveal_type(__firstlineno__) # revealed: Literal["not an int"] | ||
| ``` | ||
|
|
||
| ## Class body implicit attributes are not visible in methods | ||
|
|
||
| The implicit class body attributes are only available directly in the class body, not in nested | ||
| function scopes (methods): | ||
|
|
||
| ```py | ||
| class Foo: | ||
| # Available directly in the class body | ||
| x = __qualname__ | ||
| reveal_type(x) # revealed: str | ||
|
|
||
| def method(self): | ||
| # Not available in methods - falls back to builtins/globals | ||
| # error: [unresolved-reference] | ||
| __qualname__ | ||
| ``` | ||
|
|
||
| ## Real-world use case: logging | ||
|
|
||
| A common use case is defining a logger with the class name: | ||
|
|
||
| ```py | ||
| import logging | ||
|
|
||
| class MyClass: | ||
| logger = logging.getLogger(__qualname__) | ||
| reveal_type(logger) # revealed: Logger | ||
| ``` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| use ruff_db::files::File; | ||
| use ruff_python_ast::PythonVersion; | ||
|
|
||
| use crate::dunder_all::dunder_all_names; | ||
| use crate::module_resolver::{KnownModule, file_to_module, resolve_module_confident}; | ||
|
|
@@ -1633,6 +1634,35 @@ mod implicit_globals { | |
| } | ||
| } | ||
|
|
||
| /// Looks up the type of an "implicit class body symbol". Returns [`Place::Undefined`] if | ||
| /// `name` is not present as an implicit symbol in class bodies. | ||
| /// | ||
| /// Implicit class body symbols are symbols such as `__qualname__`, `__module__`, `__doc__`, | ||
| /// and `__firstlineno__` that Python implicitly makes available inside a class body during | ||
| /// class creation. | ||
| /// | ||
| /// See <https://docs.python.org/3/reference/datamodel.html#creating-the-class-object> | ||
| pub(crate) fn class_body_implicit_symbol<'db>( | ||
| db: &'db dyn Db, | ||
| name: &str, | ||
| ) -> PlaceAndQualifiers<'db> { | ||
| match name { | ||
| "__qualname__" => Place::bound(KnownClass::Str.to_instance(db)).into(), | ||
| "__module__" => Place::bound(KnownClass::Str.to_instance(db)).into(), | ||
| // __doc__ is `str` if there's a docstring, `None` if there isn't | ||
| "__doc__" => Place::bound(UnionType::from_elements( | ||
| db, | ||
| [KnownClass::Str.to_instance(db), Type::none(db)], | ||
|
Member
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. I think we could detect if a docstring exists and narrow this to
Member
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. we could do, but I'm not sure it's worth it. I'd vote for keeping it simple for now and adding it if someone asks for it |
||
| )) | ||
| .into(), | ||
| // __firstlineno__ was added in Python 3.13 | ||
| "__firstlineno__" if Program::get(db).python_version(db) >= PythonVersion::PY313 => { | ||
| Place::bound(KnownClass::Int.to_instance(db)).into() | ||
| } | ||
| _ => Place::Undefined.into(), | ||
| } | ||
| } | ||
|
|
||
| #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)] | ||
| pub(crate) enum RequiresExplicitReExport { | ||
| Yes, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.