Skip to content

Commit 0c77760

Browse files
committed
Cleanup
1 parent 311730a commit 0c77760

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

crates/ty_python_semantic/resources/mdtest/public_types.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ if flag():
189189

190190
## Mixed declarations and bindings
191191

192-
Since we currently treat all public uses not just as definitely-bound but also as
193-
definitely-declared, we do consider the the `A: str` declaration to be definite, and do not consider
194-
the `A = None` binding in the type of `A`:
192+
When a declaration only appears in one branch, we also consider types of bindings:
195193

196194
```py
197195
def flag() -> bool:
@@ -208,8 +206,8 @@ def _():
208206
reveal_type(A) # revealed: str | None
209207
```
210208

211-
This pattern appears frequently with conditional imports. Here, the import is treated as both a
212-
declaration and a binding, and therefore shadows the `None` binding.
209+
This pattern appears frequently with conditional imports. The `import` statement is both a
210+
declaration and a binding, but we still add `None` to the public type union:
213211

214212
```py
215213
try:
@@ -418,5 +416,7 @@ reveal_type(f) # revealed: (Overload[(x: int) -> int, (x: str) -> str]) | (Over
418416

419417
def _():
420418
# TODO: ideally, this should be the same union type as above.
421-
reveal_type(f) # revealed: Overload[(x: int) -> int, (x: bytes) -> bytes]
419+
reveal_type(
420+
f
421+
) # revealed: (def f(x: int) -> int) | (Overload[(x: int) -> int, (x: str) -> str]) | (Overload[(x: int) -> int, (x: bytes) -> bytes])
422422
```

crates/ty_python_semantic/src/place.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,6 @@ fn place_from_bindings_impl<'db>(
840840
}) if binding.is_undefined_or(is_non_exported) => Some(*reachability_constraint),
841841
_ => None,
842842
};
843-
let mut all_bindings_definitely_reachable = true;
844843
let mut deleted_reachability = Truthiness::AlwaysFalse;
845844

846845
// Evaluate this lazily because we don't always need it (for example, if there are no visible
@@ -933,8 +932,6 @@ fn place_from_bindings_impl<'db>(
933932
}
934933

935934
let binding_ty = binding_type(db, binding);
936-
all_bindings_definitely_reachable =
937-
all_bindings_definitely_reachable && static_reachability.is_always_true();
938935
Some(narrowing_constraint.narrow(db, binding_ty, binding.place(db)))
939936
},
940937
);
@@ -947,13 +944,7 @@ fn place_from_bindings_impl<'db>(
947944
};
948945

949946
let boundness = match boundness_analysis {
950-
BoundnessAnalysis::AlwaysBound => {
951-
if all_bindings_definitely_reachable {
952-
Boundness::Bound
953-
} else {
954-
Boundness::PossiblyUnbound
955-
}
956-
}
947+
BoundnessAnalysis::AlwaysBound => Boundness::Bound,
957948
BoundnessAnalysis::BasedOnUnboundVisibility => match unbound_visibility() {
958949
Some(Truthiness::AlwaysTrue) => {
959950
unreachable!(
@@ -1135,6 +1126,10 @@ fn place_from_declarations_impl<'db>(
11351126
if all_declarations_definitely_reachable {
11361127
Boundness::Bound
11371128
} else {
1129+
// For declarations, it is important to consider the possibility that they might only
1130+
// be bound in one control flow path, while the other path contains a binding. In order
1131+
// to even consider the bindings as well in `place_by_id`, we return `PossiblyUnbound`
1132+
// here.
11381133
Boundness::PossiblyUnbound
11391134
}
11401135
}

0 commit comments

Comments
 (0)