Skip to content

Commit 649610c

Browse files
[red-knot] Support super (#17174)
## Summary closes #16615 This PR includes: - Introduces a new type: `Type::BoundSuper` - Implements member lookup for `Type::BoundSuper`, resolving attributes by traversing the MRO starting from the specified class - Adds support for inferring appropriate arguments (`pivot_class` and `owner`) for `super()` when it is used without arguments When `super(..)` appears in code, it can be inferred into one of the following: - `Type::Unknown`: when a runtime error would occur (e.g. calling `super()` out of method scope, or when parameter validation inside `super` fails) - `KnownClass::Super::to_instance()`: when the result is an *unbound super object* or when a dynamic type is used as parameters (MRO traversing is meaningless) - `Type::BoundSuper`: the common case, representing a properly constructed `super` instance that is ready for MRO traversal and attribute resolution ### Terminology Python defines the terms *bound super object* and *unbound super object*. An **unbound super object** is created when `super` is called with only one argument (e.g. `super(A)`). This object may later be bound via the `super.__get__` method. However, this form is rarely used in practice. A **bound super object** is created either by calling `super(pivot_class, owner)` or by using the implicit form `super()`, where both arguments are inferred from the context. This is the most common usage. ### Follow-ups - Add diagnostics for `super()` calls that would result in runtime errors (marked as TODO) - Add property tests for `Type::BoundSuper` ## Test Plan - Added `mdtest/class/super.md` --------- Co-authored-by: Carl Meyer <[email protected]>
1 parent 1a79722 commit 649610c

File tree

10 files changed

+1219
-150
lines changed

10 files changed

+1219
-150
lines changed

crates/red_knot_python_semantic/resources/mdtest/attributes.md

-14
Original file line numberDiff line numberDiff line change
@@ -1870,20 +1870,6 @@ reveal_type(Foo.BAR.value) # revealed: @Todo(Attribute access on enum classes)
18701870
reveal_type(Foo.__members__) # revealed: @Todo(Attribute access on enum classes)
18711871
```
18721872

1873-
## `super()`
1874-
1875-
`super()` is not supported yet, but we do not emit false positives on `super()` calls.
1876-
1877-
```py
1878-
class Foo:
1879-
def bar(self) -> int:
1880-
return 42
1881-
1882-
class Bar(Foo):
1883-
def bar(self) -> int:
1884-
return super().bar()
1885-
```
1886-
18871873
## References
18881874

18891875
Some of the tests in the *Class and instance variables* section draw inspiration from

0 commit comments

Comments
 (0)