Skip to content

Commit dac4e35

Browse files
authored
[ty] Use all reachable bindings for instance attributes and deferred lookups (#18955)
## Summary Remove a hack in control flow modeling that was treating `return` statements at the end of function bodies in a special way (basically considering the state *just before* the `return` statement as the end-of-scope state). This is not needed anymore now that #18750 has been merged. In order to make this work, we now use *all reachable bindings* for purposes of finding implicit instance attribute assignments as well as for deferred lookups of symbols. Both would otherwise be affected by this change: ```py def C: def f(self): self.x = 1 # a reachable binding that is not visible at the end of the scope return ``` ```py def f(): class X: ... # a reachable binding that is not visible at the end of the scope x: "X" = X() # deferred use of `X` return ``` Implicit instance attributes also required another change. We previously kept track of possibly-unbound instance attributes in some cases, but we now give up on that completely and always consider *implicit* instance attributes to be bound if we see a reachable binding in a reachable method. The previous behavior was somewhat inconsistent anyway because we also do not consider attributes possibly-unbound in other scenarios: we do not (and can not) keep track of whether or not methods are called that define these attributes. closes astral-sh/ty#711 ## Ecosystem analysis I think this looks very positive! * We see an unsurprising drop in `possibly-unbound-attribute` diagnostics (599), mostly for classes that define attributes in `try … except` blocks, `for` loops, or `if … else: raise …` constructs. There might obviously also be true positives that got removed, but the vast majority should be false positives. * There is also a drop in `possibly-unresolved-reference` / `unresolved-reference` diagnostics (279+13) from the change to deferred lookups. * Some `invalid-type-form` false positives got resolved (13), because we can now properly look up the names in the annotations. * There are some new *true* positives in `attrs`, since we understand the `Attribute` annotation that was previously inferred as `Unknown` because of a re-assignment after the class definition. ## Test Plan The existing attributes.md test suite has sufficient coverage here.
1 parent ebf59e2 commit dac4e35

File tree

7 files changed

+30
-70
lines changed

7 files changed

+30
-70
lines changed

crates/ty_python_semantic/resources/mdtest/attributes.md

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ reveal_type(c_instance.declared_only) # revealed: Unknown
4343

4444
reveal_type(c_instance.declared_and_bound) # revealed: bool
4545

46-
# error: [possibly-unbound-attribute]
4746
reveal_type(c_instance.possibly_undeclared_unbound) # revealed: str
4847

4948
# This assignment is fine, as we infer `Unknown | Literal[1, "a"]` for `inferred_from_value`.
@@ -265,7 +264,7 @@ class C:
265264

266265
# TODO: Mypy and pyright do not support this, but it would be great if we could
267266
# infer `Unknown | str` here (`Weird` is not a possible type for the `w` attribute).
268-
reveal_type(C().w) # revealed: Unknown
267+
reveal_type(C().w) # revealed: Unknown | Weird
269268
```
270269

271270
#### Attributes defined in tuple unpackings
@@ -342,10 +341,7 @@ class C:
342341
for self.z in NonIterable():
343342
pass
344343

345-
# Iterable might be empty
346-
# error: [possibly-unbound-attribute]
347344
reveal_type(C().x) # revealed: Unknown | int
348-
# error: [possibly-unbound-attribute]
349345
reveal_type(C().y) # revealed: Unknown | str
350346
```
351347

@@ -453,8 +449,8 @@ reveal_type(c_instance.g) # revealed: Unknown
453449

454450
#### Conditionally declared / bound attributes
455451

456-
Attributes are possibly unbound if they, or the method to which they are added are conditionally
457-
declared / bound.
452+
We currently treat implicit instance attributes to be bound, even if they are only conditionally
453+
defined:
458454

459455
```py
460456
def flag() -> bool:
@@ -472,13 +468,9 @@ class C:
472468

473469
c_instance = C()
474470

475-
# error: [possibly-unbound-attribute]
476471
reveal_type(c_instance.a1) # revealed: str | None
477-
# error: [possibly-unbound-attribute]
478472
reveal_type(c_instance.a2) # revealed: str | None
479-
# error: [possibly-unbound-attribute]
480473
reveal_type(c_instance.b1) # revealed: Unknown | Literal[1]
481-
# error: [possibly-unbound-attribute]
482474
reveal_type(c_instance.b2) # revealed: Unknown | Literal[1]
483475
```
484476

@@ -620,8 +612,10 @@ reveal_type(C(True).a) # revealed: Unknown | Literal[1]
620612
# error: [unresolved-attribute]
621613
reveal_type(C(True).b) # revealed: Unknown
622614
reveal_type(C(True).c) # revealed: Unknown | Literal[3] | str
623-
# TODO: this attribute is possibly unbound
624-
reveal_type(C(True).d) # revealed: Unknown | Literal[5]
615+
# Ideally, this would just be `Unknown | Literal[5]`, but we currently do not
616+
# attempt to analyze control flow within methods more closely. All reachable
617+
# attribute assignments are considered, so `self.x = 4` is also included:
618+
reveal_type(C(True).d) # revealed: Unknown | Literal[4, 5]
625619
# error: [unresolved-attribute]
626620
reveal_type(C(True).e) # revealed: Unknown
627621
```
@@ -1289,6 +1283,10 @@ def _(flag: bool):
12891283

12901284
### Possibly unbound/undeclared instance attribute
12911285

1286+
We currently treat implicit instance attributes to be bound, even if they are only conditionally
1287+
defined within a method. If the class-level definition or the whole method is only conditionally
1288+
available, we emit a `possibly-unbound-attribute` diagnostic.
1289+
12921290
#### Possibly unbound and undeclared
12931291

12941292
```py
@@ -1320,10 +1318,8 @@ def _(flag: bool):
13201318
else:
13211319
self.y = "b"
13221320

1323-
# error: [possibly-unbound-attribute]
13241321
reveal_type(Foo().x) # revealed: Unknown | Literal[1]
13251322

1326-
# error: [possibly-unbound-attribute]
13271323
Foo().x = 2
13281324

13291325
reveal_type(Foo().y) # revealed: Unknown | Literal["a", "b"]

crates/ty_python_semantic/src/place.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ impl<'db> Place<'db> {
6262
Place::Type(ty.into(), Boundness::Bound)
6363
}
6464

65-
pub(crate) fn possibly_unbound(ty: impl Into<Type<'db>>) -> Self {
66-
Place::Type(ty.into(), Boundness::PossiblyUnbound)
67-
}
68-
6965
/// Constructor that creates a [`Place`] with a [`crate::types::TodoType`] type
7066
/// and boundness [`Boundness::Bound`].
7167
#[allow(unused_variables)] // Only unused in release builds

crates/ty_python_semantic/src/semantic_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub(crate) fn attribute_assignments<'db, 's>(
118118
let place = place_table.place_id_by_instance_attribute_name(name)?;
119119
let use_def = &index.use_def_maps[function_scope_id];
120120
Some((
121-
use_def.inner.end_of_scope_bindings(place),
121+
use_def.inner.all_reachable_bindings(place),
122122
function_scope_id,
123123
))
124124
})

crates/ty_python_semantic/src/semantic_index/builder.rs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,29 +1122,7 @@ impl<'ast> Visitor<'ast> for SemanticIndexBuilder<'_, 'ast> {
11221122
&mut first_parameter_name,
11231123
);
11241124

1125-
// TODO: Fix how we determine the public types of symbols in a
1126-
// function-like scope: https://github.com/astral-sh/ruff/issues/15777
1127-
//
1128-
// In the meantime, visit the function body, but treat the last statement
1129-
// specially if it is a return. If it is, this would cause all definitions
1130-
// in the function to be marked as non-visible with our current treatment
1131-
// of terminal statements. Since we currently model the externally visible
1132-
// definitions in a function scope as the set of bindings that are visible
1133-
// at the end of the body, we then consider this function to have no
1134-
// externally visible definitions. To get around this, we take a flow
1135-
// snapshot just before processing the return statement, and use _that_ as
1136-
// the "end-of-body" state that we resolve external references against.
1137-
if let Some((last_stmt, first_stmts)) = body.split_last() {
1138-
builder.visit_body(first_stmts);
1139-
let pre_return_state = matches!(last_stmt, ast::Stmt::Return(_))
1140-
.then(|| builder.flow_snapshot());
1141-
builder.visit_stmt(last_stmt);
1142-
let reachability = builder.current_use_def_map().reachability;
1143-
if let Some(pre_return_state) = pre_return_state {
1144-
builder.flow_restore(pre_return_state);
1145-
builder.current_use_def_map_mut().reachability = reachability;
1146-
}
1147-
}
1125+
builder.visit_body(body);
11481126

11491127
builder.current_first_parameter_name = first_parameter_name;
11501128
builder.pop_scope()

crates/ty_python_semantic/src/types/class.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,7 +1740,7 @@ impl<'db> ClassLiteral<'db> {
17401740
// attribute might be externally modified.
17411741
let mut union_of_inferred_types = UnionBuilder::new(db).add(Type::unknown());
17421742

1743-
let mut is_attribute_bound = Truthiness::AlwaysFalse;
1743+
let mut is_attribute_bound = false;
17441744

17451745
let file = class_body_scope.file(db);
17461746
let module = parsed_module(db, file).load(db);
@@ -1771,7 +1771,7 @@ impl<'db> ClassLiteral<'db> {
17711771
let method = index.expect_single_definition(method_def);
17721772
let method_place = class_table.place_id_by_name(&method_def.name).unwrap();
17731773
class_map
1774-
.end_of_scope_bindings(method_place)
1774+
.all_reachable_bindings(method_place)
17751775
.find_map(|bind| {
17761776
(bind.binding.is_defined_and(|def| def == method))
17771777
.then(|| class_map.is_binding_reachable(db, &bind))
@@ -1806,13 +1806,8 @@ impl<'db> ClassLiteral<'db> {
18061806
.is_binding_reachable(db, &attribute_assignment)
18071807
.and(is_method_reachable)
18081808
{
1809-
Truthiness::AlwaysTrue => {
1810-
is_attribute_bound = Truthiness::AlwaysTrue;
1811-
}
1812-
Truthiness::Ambiguous => {
1813-
if is_attribute_bound.is_always_false() {
1814-
is_attribute_bound = Truthiness::Ambiguous;
1815-
}
1809+
Truthiness::AlwaysTrue | Truthiness::Ambiguous => {
1810+
is_attribute_bound = true;
18161811
}
18171812
Truthiness::AlwaysFalse => {
18181813
continue;
@@ -1832,7 +1827,7 @@ impl<'db> ClassLiteral<'db> {
18321827
.and(is_method_reachable)
18331828
.is_always_true()
18341829
{
1835-
is_attribute_bound = Truthiness::AlwaysTrue;
1830+
is_attribute_bound = true;
18361831
}
18371832

18381833
match binding.kind(db) {
@@ -1849,17 +1844,12 @@ impl<'db> ClassLiteral<'db> {
18491844
);
18501845

18511846
// TODO: check if there are conflicting declarations
1852-
match is_attribute_bound {
1853-
Truthiness::AlwaysTrue => {
1854-
return Place::bound(annotation_ty);
1855-
}
1856-
Truthiness::Ambiguous => {
1857-
return Place::possibly_unbound(annotation_ty);
1858-
}
1859-
Truthiness::AlwaysFalse => unreachable!(
1860-
"If the attribute assignments are all invisible, inference of their types should be skipped"
1861-
),
1847+
if is_attribute_bound {
1848+
return Place::bound(annotation_ty);
18621849
}
1850+
unreachable!(
1851+
"If the attribute assignments are all invisible, inference of their types should be skipped"
1852+
);
18631853
}
18641854
DefinitionKind::Assignment(assign) => {
18651855
match assign.target_kind() {
@@ -1995,10 +1985,10 @@ impl<'db> ClassLiteral<'db> {
19951985
}
19961986
}
19971987

1998-
match is_attribute_bound {
1999-
Truthiness::AlwaysTrue => Place::bound(union_of_inferred_types.build()),
2000-
Truthiness::Ambiguous => Place::possibly_unbound(union_of_inferred_types.build()),
2001-
Truthiness::AlwaysFalse => Place::Unbound,
1988+
if is_attribute_bound {
1989+
Place::bound(union_of_inferred_types.build())
1990+
} else {
1991+
Place::Unbound
20021992
}
20031993
}
20041994

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5665,7 +5665,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
56655665
// If we're inferring types of deferred expressions, always treat them as public symbols
56665666
if self.is_deferred() {
56675667
let place = if let Some(place_id) = place_table.place_id_by_expr(expr) {
5668-
place_from_bindings(db, use_def.end_of_scope_bindings(place_id))
5668+
place_from_bindings(db, use_def.all_reachable_bindings(place_id))
56695669
} else {
56705670
assert!(
56715671
self.deferred_state.in_string_annotation(),

crates/ty_python_semantic/src/types/signatures.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,8 +1650,8 @@ mod tests {
16501650
panic!("expected one positional-or-keyword parameter");
16511651
};
16521652
assert_eq!(name, "a");
1653-
// Parameter resolution deferred; we should see B
1654-
assert_eq!(annotated_type.unwrap().display(&db).to_string(), "B");
1653+
// Parameter resolution deferred:
1654+
assert_eq!(annotated_type.unwrap().display(&db).to_string(), "A | B");
16551655
}
16561656

16571657
#[test]

0 commit comments

Comments
 (0)