[ty] Evaluate reachability of non-definitely-bound to Ambiguous#19579
[ty] Evaluate reachability of non-definitely-bound to Ambiguous#19579sharkdp merged 17 commits intoastral-sh:mainfrom
Conversation
|
CodSpeed WallTime Performance ReportMerging #19579 will not alter performanceComparing Summary
|
CodSpeed Instrumentation Performance ReportMerging #19579 will not alter performanceComparing Summary
|
This comment was marked as resolved.
This comment was marked as resolved.
d5dafc5 to
3430eea
Compare
This comment was marked as resolved.
This comment was marked as resolved.
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
d383152 to
b05868f
Compare
b05868f to
9be1e82
Compare
9be1e82 to
feca174
Compare
This comment was marked as resolved.
This comment was marked as resolved.
feca174 to
590ea0d
Compare
aa650e9 to
a84b8ce
Compare
…s-not-definitely-bound
|
I'm looking into making the changes described above. |
4c1da8c to
2218db4
Compare
94d1897 to
71bd443
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unresolved-attribute |
20 | 0 | 0 |
possibly-unbound-attribute |
7 | 0 | 0 |
possibly-unresolved-reference |
2 | 0 | 0 |
| Total | 29 | 0 | 0 |
…tions (#20114) ## Summary Properly preserve type qualifiers when accessing attributes on unions and intersections. This is a prerequisite for #19579. Also fix a completely wrong implementation of `map_with_boundness_and_qualifiers`. It now closely follows `map_with_boundness` (just above). ## Test Plan I thought about it, but didn't find any easy way to test this. This only affected `Type::member`. Things like validation of attribute writes (where type qualifiers like `ClassVar` and `Final` are important) were already handling things correctly.
…s-not-definitely-bound
966553c to
a18a7ce
Compare
|
I tried various other solutions here, and non of them worked as well as what @Glyphack implemented in the first place. The main problem is that we can only build unions/intersections of types. We can't build unions/intersections of I also tried various completely different approaches of solving this problem, and none of them hit the right granularity. Either they are not able to solve the So I am returning this PR to a previous state (with a couple of smaller cleanups, renamings, and additional tests). I am still not very satisfied with the fact that we track boundness information in a |
crates/ty_python_semantic/resources/mdtest/statically_known_branches.md
Outdated
Show resolved
Hide resolved
* main: Fix mdtest ignore python code blocks (#20139) [ty] add support for cyclic legacy generic protocols (#20125) [ty] add cycle detection for find_legacy_typevars (#20124) Use new diff rendering format in tests (#20101) [ty] Fix 'too many cycle iterations' for unions of literals (#20137) [ty] No boundness analysis for implicit instance attributes (#20128) Bump 0.12.11 (#20136) [ty] Benchmarks for problematic implicit instance attributes cases (#20133) [`pyflakes`] Fix `allowed-unused-imports` matching for top-level modules (`F401`) (#20115) Move GitLab output rendering to `ruff_db` (#20117) [ty] Evaluate reachability of non-definitely-bound to Ambiguous (#19579) [ty] Introduce a representation for the top/bottom materialization of an invariant generic (#20076) [`flake8-async`] Implement `blocking-http-call-httpx` (`ASYNC212`) (#20091) [ty] print diagnostics with fully qualified name to disambiguate some cases (#19850) [`ruff`] Preserve relative whitespace in multi-line expressions (`RUF033`) (#19647)
…tions (astral-sh#20114) ## Summary Properly preserve type qualifiers when accessing attributes on unions and intersections. This is a prerequisite for astral-sh#19579. Also fix a completely wrong implementation of `map_with_boundness_and_qualifiers`. It now closely follows `map_with_boundness` (just above). ## Test Plan I thought about it, but didn't find any easy way to test this. This only affected `Type::member`. Things like validation of attribute writes (where type qualifiers like `ClassVar` and `Final` are important) were already handling things correctly.
…al-sh#19579) ## Summary closes astral-sh/ty#692 If the expression (or any child expressions) is not definitely bound the reachability constraint evaluation is determined as ambiguous. This fixes the infinite cycles panic in the following code: ```py from typing import Literal class Toggle: def __init__(self: "Toggle"): if not self.x: self.x: Literal[True] = True ``` Credit of this solution is for David. ## Test Plan - Added a test case with too many cycle iterations panic. - Previous tests. --------- Co-authored-by: David Peter <[email protected]>
…nalysis (#22971) ## Summary cf: #19579, #20566 (comment) Currently, we use the query `static_expression_truthiness` to determine the truthiness of an expression. This always determines the truthinesses of non-definitely-bound places as `Ambiguous`, preventing cycles that occur when their reachabilities depend on themselves. However, this can lead to inaccurate analysis of code like the following. ```python def _(flag: bool): if flag: ALWAYS_TRUE_IF_BOUND = True # error: [possibly-unresolved-reference] "Name `ALWAYS_TRUE_IF_BOUND` used when possibly not defined" if ALWAYS_TRUE_IF_BOUND: x = 1 else: x = 2 # If `ALWAYS_TRUE_IF_BOUND` were not defined, an error would occur, and therefore the `x = 2` branch would never be executed. reveal_type(x) # expected: Literal[1], but: Literal[1, 2] ``` Since #20566, we can determine the `Place` by looking at the previous cycle result. Therefore, we can now remove `static_expression_truthiness`, improving reachability analysis of the above code. ## Test Plan mdtest updated
Summary
closes astral-sh/ty#692
If the expression (or any child expressions) is not definitely bound the reachability constraint evaluation is determined as ambiguous.
This fixes the infinite cycles panic in the following code:
Credit of this solution is for David.
Test Plan