-
Notifications
You must be signed in to change notification settings - Fork 127
Description
Summary
Ty emits "possibly-unbound-attribute" diagnostic on setting the attribute.
from typing import Self
class Foo:
def __init(self: Self, flag: bool):
if flag:
self.x = 1 # error possibly-unbound-attribute
https://play.ty.dev/617d2c31-fe1b-4c8a-9787-7b7baf1f886d
Currently in a class body if an instance attribute is conditionally defined or undefined, we emit a diagnostic on setting the attribute.
Instead ty should report when accessing the attribute. In the previous code example if we only accessed the self.x
when it's undefined the diagnostic made sense.
Another example of where this diagnostic makes sense.
class C:
if flag:
x: int
C().x # error: [possibly-unbound-attribute]
comments from @carljm:
I think that in the case of a possibly-unbound implicit instance attribute, we should never emit a diagnostic on that, whether inside or outside the class body, and whether setting or accessing the attribute
comments from @sharkdp on implementation:
I agree that we shouldn't even attempt to emit that diagnostic in case that it refers to an instance attribute. For other attribute accesses, like on class bodies or modules, it probably still makes sense, but might be similarly problematic to possibly-unresolved-reference, which we've recently degraded to ignored-by-default.
Fixing that would require a richer return type for Type::member, I think. We are planning to do that anyway for better error messages, but I guess in the non-error case, we could also return additional metadata (what kind of attribute is this, is the returned type the result of a descriptor get method, etc.)