Skip to content

[red-knot] detect implicit instance attributes in a generic method #16928

@carljm

Description

@carljm
class C:
    def f(self):
        self.x: int = 1
    
    def g[T](self, t: T) -> T:
        self.y: str = "foo"
        return t

reveal_type(C().x)  # correctly reveals `int`
reveal_type(C().y)  # should reveal `str`, but instead errors with `unresolved-attribute`

Generic functions have an additional type-params scope in between the outer scope and the function body scope. SemanticIndexBuilder::is_method_of_class fails to account for this, so we fail to record attribute assignments that occur in a generic method.

Relatedly, we now have "is this function a method of a class" logic separately both in SemanticIndexBuilder::is_method_of_class and in TypeInferenceBuilder::infer_function_body (as part of detecting whether a function is a method of a protocol). It may be worth seeing if we can unify that logic to a single location (maybe a method on Scope?).

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingtyMulti-file analysis & type inference

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions