mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-22 15:54:18 +08:00
micha/flat-python-ast
16
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
d8c6e237e7 |
[ty] Split mismatched-type-name by type constructor (#29957)
## Summary
This PR splits `mismatched-type-name` into eight independently
configurable warnings for `NewType`, functional enums, `TypeVar`,
`ParamSpec`, `TypeVarTuple`, `namedtuple`/`NamedTuple`, `TypedDict`, and
`TypeAliasType`.
~~This PR is actually quite a bit less breaking for users than other
rule splits we've done over the last few days. Because
`mismatched-type-name` is no longer the canonical name for _any_ rule on
this branch, we're able to make it a deprecated alias for _all_ the new
rules, meaning that it will continue to work on the CLI, in ty
configuration files, and ty suppression comments (we'll just emit a
warning diagnostic telling users to upgrade their configuration
file/suppression comment).~~
~~A more specific rule takes precedence over the deprecated name if both
are given within the same configuration table. On the command line, the
last matching flag wins (e.g. `--error=mismatched-enum-name
--ignore=mismatched-type-name` will suppress diagnostics for mismatched
enum names).~~
(After review from @MichaReiser, I backed out these changes. It's now a
somewhat breaking change, because the rule is now marked as "removed"
rather than "deprecated".)
As well as splitting the rule into eight separate rules, it also cleans
up our error recovery for several of these type factories. For example,
in the following snippet, we fail to recognize `T` as a `TypeVar` due to
bad error recovery from the non-literal name. But we don't really need a
nonliteral name in this case to determine that it's a validly
constructed TypeVar. It's probably a mistake, but there's no _soundness_
issue here:
```py
from typing import TypeVar
def f(x: str):
# main emits an error: `invalid-typevar`
# this PR emits a warning: `mismatched-typevar-name`
T = TypeVar(x)
def g(x: T) -> T:
return x
reveal_type(g(42)) # main: Todo. This PR: `Literal[42]`.
```
## Motivation
The main motivation here is that having a namedtuple or enum with the
"wrong name", for example, just seems much less "obviously wrong" than
having a typevar or paramspec with the "wrong name". The [ecosystem
report](https://github.com/astral-sh/ruff/pull/29957#issuecomment-6603964612)
is evidence of this. namedtuples and enums define runtime classes that
can be used in runtime contexts and instantiated; these constructs
aren't _just_ for type checkers.
TypeVars/ParamSpecs/TypeVarTuples/NewTypes/TypedDicts are all _only_ for
type checkers and there's no plausible reason why you'd want to give the
type a different name to the variable it's assigned to.
`mismatched-namedtuple-name` and `mismatched-enum-name` are therefore
now disabled by default, matching the default severity we already
settled on for `mistmatched-sentinel-name` in
https://github.com/astral-sh/ruff/pull/27206. `mismatched-typevar-name`,
`mismatched-paramspec-name`, `mismatched-typevartuple-name`,
`mismatched-enum-name`, `mismatched-namedtuple-name` and
`mismatched-newtype-name` all remain enabled by default with `warning`
severity.
|
||
|
|
22e77392fb |
[ty] Split invalid-return-type into 3 focussed rules; rename empty-body and unsound-return (#29947)
Co-authored-by: Micha Reiser <micha@reiser.io> |
||
|
|
4c51cc646d |
[ty] Give abstract class instantiation its own diagnostic rule (#29948)
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com> |
||
|
|
ce0077c3e5 |
[ty] Distinguish attributes missing from some union members (#29944)
## Summary
Users can now suppress an attribute error that affects only some union
members and still catch names that are missing from every member. For
example, both lines below previously reported `unresolved-attribute`:
```python
def clean(value: str | int) -> str:
value.stirp() # missing-attribute: neither str nor int has stirp
return value.strip() # missing-union-attribute: str has strip, int does not
```
The motivation is that a project adopting ty may want `value.strip()` on
`str | None` to only warn while a typo remains an error-severity
diagnostic.
Both rules default to errors. The same distinction applies to optional
values such as `str | None` and to attribute deletion. Attributes
defined only conditionally on a class continue to use
`possibly-missing-attribute`.
### Comparison to other type checkers
Pyright distinguishes between `reportOptionalMemberAccess` (which is
specifically for unions with `None`) and `reportAttributeAccessIssue`,
which is for all other unions. mypy, meanwhile, distinguishes between
`union-attr` (which is an error code for all attribute-access errors on
union types) and `attr-defined` (which is an error code for all
attribute-access errors on non-union types). So this new split doesn't
_exactly_ align with either precedent -- but I _do_ think it's probably
the most useful split we can do, and it's the one that makes the most
sense _a priori_ in my opinion.
### Compatibility
`unresolved-attribute` remains an alias for `missing-attribute` in
configuration and inline ignores. To preserve a setting such as
`unresolved-attribute = "warn"` for both cases, also set
`missing-union-attribute = "warn"`. On an access that fails for only
some union members, replace `# ty: ignore[unresolved-attribute]` with `#
ty: ignore[missing-union-attribute]`. Tools that consume diagnostic
codes should recognize both new names.
Fixes astral-sh/ty#2839 and fixes astral-sh/ty#2471.
## Test Plan
A comparison with ty 0.0.96 on typeshed-stats, docstring-adder,
sphinx-lint, and typing_extensions found the same messages, locations,
and severities. Eight diagnostics received `missing-union-attribute`
across those four projects (two in sphinx-lint and six in
typing_extensions).
---------
Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
|
||
|
|
45d1a3a4eb |
[ty] Solve generic call type context through constraint sets (#26680)
When inferring a generic call, there are two types of constraints: the
constraints inferred from the arguments to the call, and the constraints
inferred from the declared type of the call expression. We currently
infer these two sets of constraints separately, and manually inject
"preferred" types from the declared type into the solution through a
legacy type-mapping. After this PR, we now combine both declared and
inferred constraints into a single constraint set, allowing us to take
full advantage of the constraint solver for more complex generic calls.
Note that we still retain the legacy path for calls involving
`ParamSpec` or `TypeVarTuple`, but eventually we will be able to migrate
generic calls entirely to the new solver.
The most prominent pattern fixed by this change is that we now solve a
generic call directly to the declared type when no inferred evidence is
present, even if the declared type is covariant over its type parameter:
```py
def example() -> frozenset[int]:
return reveal_type(frozenset()) # frozenset[int]
```
We previously inferred `frozenset[Unknown]` for such a call, leading to
a number of `unsound-return-statement` being removed throughout the
ecosystem.
Much of the complexity of this PR is due to solver heuristics around
declared type preference. Constraints originating from the declared type
are tagged with `ConstraintProvenance::Declared`, a third variant of
constraint provenance, which this PR also changes to be represented as a
bit-set. We now also apply the mutual-assignability heuristic when
choosing the solution to a given type variable in a generic call: if the
inferred and declared solutions are mutually-assignable, we prefer the
declared solution, otherwise, we prefer the inferred solution.
```py
from typing import Any
def identity[T](value: T) -> T:
return value
def _(dynamic: Any, number: int):
reveal_type(identity(number)) # revealed: int
reveal_type(identity(dynamic)) # revealed: Any
x1: Any = reveal_type(identity(number)) # revealed: Any
reveal_type(x1) # revealed: Any
x2: int = reveal_type(identity(dynamic)) # revealed: int
reveal_type(x2) # revealed: int
```
Note that this heuristic applies independently to each type variable in
the generic call, meaning that the declared type may be preferred for
one type variable, why the inferred type is preferred for another:
```py
def pair[T, U](first: T, second: U) -> tuple[list[T], list[U]]:
return [first], [second]
def _(first: int, second: str):
# revealed: tuple[list[Any], list[str]]
result: tuple[Sequence[Any], Sequence[object]] = reveal_type(pair(first, second))
```
The primary limitation of this PR is that we do not extend the same
approach to collection-literal inference, which still build a set of
legacy type-mappings. However, collection literals form a much simpler
constraint solver problem than generic calls in general, and so that
should be a straightforward follow up after this PR lands.
Resolves https://github.com/astral-sh/ty/issues/3559.
Resolves https://github.com/astral-sh/ty/issues/3236.
Resolves https://github.com/astral-sh/ty/issues/2615.
Resolves https://github.com/astral-sh/ty/issues/4016.
Resolves https://github.com/astral-sh/ty/issues/4294.
Resolves https://github.com/astral-sh/ty/issues/4311.
Resolves https://github.com/astral-sh/ty/issues/4736.
|
||
|
|
b9a10a087d |
[ty] Apply a cycle iteration cutoff across type inference (#30010)
A recursive assignment can keep nesting a type argument until ty panics. For example, if `Box[T].next` has type `Box[tuple[T]]`, each update `self.value = other.value.next` adds another tuple layer. Apply a 16-iteration budget when normalizing types across cycle iterations. Once the budget is reached, preserve types whose normalized value has stabilized and return `Unknown` for types that continue to change. Keep that fallback on subsequent iterations so a concrete initializer cannot restart the growth. This covers both member lookup and expression inference. This should generally prevent too-many-iterations panics resulting from infinitely-growing types. This broad cutoff can lose precision in a cycle that would eventually converge. We could increase the budget to reduce the real-world impact of this. A limit of 16 generally keeps things snappy. I tested 64, and that can lead to very slow type checking in some of the infinitely-growing cases. - Fixes astral-sh/ty#4609 - Fixes astral-sh/ty#4611 - Fixes astral-sh/ty#4612 - Fixes astral-sh/ty#4618 - Fixes astral-sh/ty#4905 ## Test plan Ran the linked issue reproducers on the PR build with the reported Python versions and Linux platform settings. Both the tuple and list variants of astral-sh/ty#4905 passed. The reduced and regenerated inputs for the linked fuzzer reports completed with ordinary diagnostics or a clean check. |
||
|
|
0471c14d1f |
[ty] Rename unresolved-import to import-not-found (#29960)
## Summary ty currently reports `unresolved-import` for both a missing module and a missing member in a `from` import. But the word "unresolved" is heavily overloaded in Python, and has been reported by users as being confusing (see https://github.com/astral-sh/ty/issues/2471). This PR renames the rule to `import-not-found`, which describes the lookup failure in either case. Configuration, command-line flags, and ignore comments accept either spelling. If several flags set this rule’s severity using different spellings, the last flag wins. Existing baseline entries still match, and rewriting a baseline uses the new name. Links to the old rule heading still work. |
||
|
|
ea1865842b |
[ty] Explain invalid aliases at their definitions (#29654)
Builds on #29200. When an invalid implicit alias is used as a type, the error now explains why its original right-hand side is invalid and points to that expression. For example: ```py Called = type(1) # snapshot: invalid-type-form def inspect(called: Called): reveal_type(called) # revealed: Unknown ``` ```text error[invalid-type-form]: Variable is not allowed in parameter annotations --> src/mdtest_snippet.py:14:21 | 14 | def inspect(called: Called): | ^^^^^^ info: Function calls are not allowed in type alias values --> src/mdtest_snippet.py:11:10 | 11 | Called = type(1) | ^^^^^^^ info: See the following page for a reference on valid type expressions: info: https://typing.python.org/en/latest/spec/annotations.html#type-and-annotation-expressions ``` No other type checker provides this kind of detailed origin explanation when a value is not usable as a type alias. Follow imports and forwarding assignments to the original definition. Collect these explanations independently of diagnostics in the defining file, so unchecked dependencies and local suppressions do not hide them. Ordinary assignments remain valid at runtime; the error is reported only where the value is used as a type. |
||
|
|
ef23c9c39e |
Update default and latest Python versions for 3.15 (#28792)
Summary -- This PR updates our default Python version to 3.11, in line with 3.10 reaching [EOL] next month, and updates our latest Python version to 3.15. I followed last year's PR (#20725) to find everywhere that needed to change. Fortunately there were not as many test changes needed as last year's deferred annotation issues. I also updated the language classifier in our pyproject.toml. [EOL]: https://devguide.python.org/versions/#supported-versions Test Plan -- Existing tests with updated snapshots. Most of the updates were to snapshots for TC001, TC002, and TC003, which now default to the lazy-import fix. After updating the snapshots, I tried to make sure we still had coverage for whatever changed. That meant adding a few more mdtests for the TC rules, and one small mdtest for RUF017 to restore coverage for the `functools.reduce` fix. |
||
|
|
28ad9e5d8d |
[ty] Avoid recursive lambda class decorator panics (#28984)
## Summary
We avoid a cycle-iteration panic when a lambda used as a class decorator
refers back to the decorated name:
```python
make = lambda cls: result
try:
raise Exception
except Exception:
@make
class result: ...
result = make
finally:
from unknown_module import member as result
```
Previously, we treated a provisional `Divergent` decorator result as
preserving the original class. Replacing that result with the class
discarded the cycle marker, allowing the lambda's return type to gain
another callable layer on each inference iteration. We now retain both
the current binding and the cycle marker until inference resolves the
decorator result. Keeping the current binding also lets decorators whose
annotations depend on the decorated class resolve without losing the
class type or its constructor diagnostics.
Closes https://github.com/astral-sh/ty/issues/4616.
|
||
|
|
d238c46e67 |
[ty] Fix stack overflow when inferring dynamic class metaclasses (#28917)
## Summary
We avoid a stack overflow when a dynamically created class is assigned
in a loop whose condition depends on that assignment:
```python
value = 0
while value:
value = type("C", (value,), {})
```
Determining whether the loop body is reachable can require the class's
metaclass, while the class's base type depends on that reachability. We
now make dynamic-class metaclass inference cycle-aware and recover with
an unknown metaclass, as we do for static classes. This also fixes the
same cycle with `types.new_class`.
|
||
|
|
160ffbf2cf |
[ty] Introduce Recursive types and fully support recursive implicit type aliases (#28425)
## Summary Closes https://github.com/astral-sh/ty/issues/1738 (While this PR also resolves the error in the code provided in https://github.com/astral-sh/ty/issues/1846, I consider the broader issue under discussion to still be unresolved) To fully support implicit type aliases, this PR introduces `Recursive` types. These represent *structural recursive types*. In other words, they are anonymous recursive types implicitly constructed through type inference, rather than a nominally defined recursive type like PEP 695 recursive type aliases. This is what was being discussed in https://github.com/astral-sh/ruff/pull/24252#issuecomment-4172655350. We are not introducing recursive types solely for implicit type aliases; this is the first step toward completely replacing the current `Divergent`-based approximate recursive type inference with a full implementation. Initially, I planned to completely remove the `Divergent` type alongside the introduction of `Recursive` types, but because the scope of the impact was far too large, I decided to replace it incrementally. Both will coexist until the replacement is complete, but eventually, everything should be replaced by `Recursive` types. Following this PR, I plan to use the newly introduced `Recursive` types to replace various forms of recursive type inference. ## Design Currently, we approximate and treat recursive type structures generated by recursive type inference as gradual types containing `Divergent`. For example, a subscript on `x: Divergent` results in `x[n]: Divergent`. Consequently, we get: ``` x: list[Divergent] x[n]: Divergent x[n][n]: Divergent ``` As a result, the type after subscripting loses the information that `x` was a recursive list type. We solve this problem by introducing recursive types. `RecursiveType` is a binder for a body type that contains a recursive variable. In other words, it roughly has the following structure: ```rust struct RecursiveType<'db> { id: salsa::Id, body: Type<'db>, arguments: Option<Specialization<'db>>, } struct RecursiveVar<'db> { id: salsa::Id, arguments: Option<Specialization<'db>>, } enum Type<'db> { Recursive(RecursiveType<'db>), RecursiveVar(RecursiveVar<'db>), ... } ``` For example, for the following implicit type alias: ```python Tree = tuple[T, "Tree[list[T]] | None"] # = μF. λT. tuple[T, F[list[T]] | None] ``` the constructed `RecursiveType` looks like this: ```rust RecursiveType { id, body: tuple[T, RecursiveVar { id, arguments: [list[T]] } | None], arguments: [T] } ``` Unlike `Divergent`, the recursive variable `RecursiveVar` has no type properties on its own. Attempting to access this variable immediately unfolds the recursive type, so questioning the typing properties of the variable itself is meaningless. Therefore, if any type operation on `RecursiveVar` appears, it should be a bug. To infer the container subscript `x[1]` for `x: Tree[int]`, first unfold `x`'s type. Unfolding replaces references to the recursive binder with the recursive type itself. Writing `B[a := R]` for capture-avoiding substitution of `R` for `a` in `B`: ```text unfold(μa. B) = B[a := μa. B] ``` For `Tree[int]`, substitute the constructor for `F`, then apply `T := int`: ```text unfold((μF. λT. tuple[T, F[list[T]] | None])[int]) = tuple[int, (μF. λT. tuple[T, F[list[T]] | None])[list[int]] | None] (= tuple[int, Tree[list[int]] | None]) ``` Tuple subscripting then selects the element at index 1: `x[1]: Tree[list[int]] | None`. ## Test Plan mdtest updated --------- Co-authored-by: Carl Meyer <carl@astral.sh> |
||
|
|
7ae3af9e7d |
[ty] Preserve tuple shapes during concatenation (#28617)
Tuple concatenation currently discards element positions and tuple length, so adding two fixed-length tuples produces a variable-length tuple. Preserve the concatenated shape, including known prefixes and suffixes around variable-length portions. As with tuple slicing and iteration, this (unsoundly) assumes values annotated as `tuple` behave like ordinary tuples, even though a subclass could override `__add__` and/or `__radd__` incompatibly. Explicitly known subclasses continue to use their operator methods. Keep inference bounded by approximating large tuple combinations and widening tuple lengths when their alternatives change between cycle iterations. Stable tuple unions, including those supplied by collection type context, retain their shapes. Some cycles with finite but changing tuple lengths can still lose precision to ensure convergence. Fixes https://github.com/astral-sh/ty/issues/3413. ## Ecosystem impact - Removes tuple-shape false positives in Hydra-zen, Trio, Spark, and SymPy, and two unsoundness warnings in pandas. - Most other changes make existing diagnostics more precise. New errors expose overly narrow tuple attributes and collection element types in zope.interface and SymPy, including invariant dictionary key types. - One new xarray false positive exposes existing dictionary-key inference that incorrectly restricts keys to strings. This limitation is left for separate work. Cycle widening also loses finite tuple alternatives in Apprise, consolidating existing errors without changing which operations are rejected. [Full ecosystem analysis](https://github.com/astral-sh/ruff/pull/28617#issuecomment-5687202176). ## Performance The `many_tuple_assignments` microbenchmark increases from 141.1 ms to 159.7 ms in simulation and from 6.4 MB to 6.9 MB in memory. This is an expected cost of preserving tuple shapes; the tradeoff is acceptable for this change. See the [benchmark report](https://github.com/astral-sh/ruff/pull/28617#issuecomment-5684963921). ## Test plan - Mdtests cover fixed and empty tuples, variable-length prefixes and suffixes, aliases, union operands, subclass overrides, and `TypeVarTuple` concatenation with both typing syntaxes. - Exercise ordinary and augmented concatenation, large unions with custom operators, repeated doubling, and recursively growing local variables and instance attributes. - Preserve finite alternating tuple shapes and fixed-length tuple unions supplied by an empty list's return context. - Verify incompatible augmented assignment to `tuple[Any, Any]` and instance-layout conflicts detected through concatenated `__slots__`. - Unit tests cover tuple widening across cycle results, including equal lengths, changing lengths, discarded alternatives, avoiding relation queries, and `Never` elements. |
||
|
|
689a33fd36 |
[ty] Add rules to detect always-truthy and always-falsy conditions (#28034)
## Summary Builds on #28032. Add two rules for boolean tests whose truthiness is statically known: - `redundant-condition`, enabled by default at warning severity, catches cases such as an uncalled function or method, an unawaited coroutine, a generator tested for emptiness, or a tuple whose length makes its truthiness constant. - `redundant-condition-strict`, disabled by default, covers boolean/integer conditions, conditions whose outcome is fixed only by short-circuit evaluation, and conditions containing walrus expressions. These can expose real bugs, but are also much more likely to be intentional. The checks reuse ty's truthiness inference and apply to `if`/`elif`, `while`, assertions, match guards, conditional expressions, comprehension filters, and unary `not`. The operands of `and`/`or` are checked when the expression is used as a condition. The main design question is which statically redundant tests are likely to represent mistakes. The implementation deliberately prefers missing some redundant conditions to warning about common defensive or compatibility patterns. ## Avoiding false positives ### Separate likely mistakes from more opinionated checks Types assignable to `int`, including `bool`, use the strict rule. So do conditions whose outcome is fixed by short-circuit evaluation even though their inferred value type has ambiguous truthiness, and tests containing walrus expressions. Other values with fixed truthiness use the default rule. This keeps checks of ordinary boolean/integer flags quiet by default, because these are extremely common and it is difficult to distinguish deliberately constant flags from mistakes: ```python DEBUG = False if DEBUG: # Only reported by redundant-condition-strict. print("debugging") ``` The strict rule is a sibling of the default rule: it handles a separate set of expressions rather than reporting the same expressions more aggressively. ### Distinguish a condition's outcome from its value type Short-circuit evaluation can establish a fixed outcome even when the expression's value type does not: ```python def check(value: object): if value and False: # Always false; reported by redundant-condition-strict. print("unreachable") ``` The condition fails whether `value` is truthy or falsy. However, saving `value and False` to a variable and testing that variable later is different: the saved value could be the original object, whose `__bool__` result may change between calls. The redundant-condition checks use short-circuit truthiness APIs introduced in https://github.com/astral-sh/ruff/pull/28082 for direct conditions, and value-type truthiness for tests of computed values. This also handles chained comparisons whose intermediate results are objects with mutable truthiness. ### Recognize explicitly constant tests Both rules exempt direct boolean, integer and `None` literals in the AST, such as `if False:`, `if 0:`, `while True:`, `while 1:`, and `assert None`. These spellings are strong evidence that the constant truthiness is deliberate. A name _inferred_ as `Literal[False]` is different: its value may not be apparent where it is tested, so it remains eligible for the strict rule. ### Treat walrus expressions as evidence of intentional side effects An always-truthy operand can be useful because evaluating it assigns a value: ```python if should_continue() and (message := "continuing") and is_ready(): print(message) ``` The middle operand uses the strict rule, so it does not warn by default. We does not attempt to classify arbitrary side effects. For example, `if items.append(value) or fallback:` can still trigger the default rule because `append` returns `None`. An `and`/`or` expression used to compute a value (rather than one that is used directly in an `if` test or similar), such as `result = items.append(value) or fallback`, is not flagged by either rule. ### Follow the origins of environment-dependent conditions Both rules exempt compatibility checks involving `sys.version_info`, `sys.platform`, `os.name`, and `TYPE_CHECKING`. A condition being constant for the configured environment does not make it redundant across the environments supported by the project. The exemption also follows names and attributes through their definitions, including aliases and imports across modules: ```python import sys IS_WINDOWS = sys.platform == "win32" LINE_PREFIX = "\n" if IS_WINDOWS else "" if LINE_PREFIX: # Exempt even though its truthiness is known on this platform. print(LINE_PREFIX) ``` The traversal follows assignment values, augmented assignments, walrus bindings, loop and comprehension iterables, context-manager expressions, and the subjects of captured `match` patterns. This lookup is deliberately not flow-sensitive: if any resolved definition depends on one of these symbols, the use is exempt. This is so that we err on the side of avoiding false positives. The shared definition-resolution machinery from #28032 allows this traversal during inference without requesting completed inference of the same scope. Name lookups are cached per scope and name; attribute lookups are cached per receiver type and attribute name. Definition traversal has its own Salsa query boundary and handles cyclic aliases, so repeated conditions can share the provenance analysis. ### Preserve defensive assertions and exhaustive dispatch Defensive boolean/integer assertions such as `assert isinstance(value, int)` remain useful at runtime even when `value` is annotated as `int`, since the runtime does not enforce type annotations. These are therefore exempt from both rules. Non-boolean mistakes such as `assert function` (where the user meant `assert function()`) are still reported by the default rule. A complete assertion whose value type has ambiguous truthiness but whose outcome is fixed by short-circuit evaluation remains eligible for the strict rule. For `if` and `elif` statements, the strict rule also recognizes defensive exits. An always-false condition is exempt when its body ends in a `raise`, an assertion that might fail, a call returning `Never`, or a return assignable to `NotImplementedType`: ```python def increment(value: int) -> int: if not isinstance(value, int): raise TypeError("expected an int") return value + 1 ``` For an always-true condition, the same heuristic examines an immediately following `else` suite, or the remainder of the surrounding suite when there are no later clauses. This accommodates exhaustive dispatch followed by `assert_never`, and early-return dispatch followed by a defensive exception. These defensive-exit exemptions apply to boolean/integer conditions and conditions requiring short-circuit analysis. An uncalled function is still suspicious even when an otherwise unreachable branch raises. ### Check compound conditions once In `result = flag and function`, Python does not test the truthiness of the final operand: it simply returns that value if evaluation reaches it. The rule therefore does not flag `function`, even though it is an always-truthy object in a boolean expression. In `if flag and predicate:`, the surrounding `if` does test the truthiness of `predicate`, so the rule reports the uncalled function even though the complete condition has ambiguous truthiness. Unlike `and`/`or` expressions used to compute values, `not` explicitly tests its operand's truthiness even when it used to computer a value, so `not` expressions are checked even outside the context of `if`/`while`/`elif`/`assert` tests. For compound conditions, the redundant-condition checks visit operands before deciding whether to report the complete expression. Reporting an uncalled function in `if not function:` suppresses a second duplicate diagnostic on the `not function` expression as a whole. Boolean/integer operands and operands requiring short-circuit analysis are instead suppressed so that the complete condition can be reported once. This applies consistently to statement conditions, conditional expressions, and comprehension filters. Boolean tests inside value expressions, such as `if consume(not function):`, are checked independently. Tests in nested scopes are checked when those scopes are inferred. Neither rule runs in stub files or files excluded from checking. Diagnostic and provenance analysis is skipped entirely when both rules are disabled. ## Diagnostics and fixes Special-case diagnostics are provided to help explain some of the more common errors we see in the ecosystem: an uncalled function or method being always truthy, a fixed-length tuple being always truthy or always falsy, a `TypedDict` always being truthy due to the presence of `Required` fields, a `@final` class without `__bool__` or `__len__` being always truthy, or an unevaluated generator being always truthy. For simple function and method references, the diagnostic carries a call fix. A parameterless signature gets an unsafe `()` fix; signatures with parameters get a display-only `(...)` suggestion. Generator diagnostics suggest `any()` and carry a display-only wrapping fix when the standard `any` builtin is known not to be shadowed, including by a custom `__builtins__.pyi`. These fixes are deliberately not classified as safe: calling a function or consuming a generator can change runtime behavior, and the intended correction may be different. ### Default-rule diagnostics <details> <summary>Uncalled function</summary>  </details> <details> <summary>Uncalled method</summary>  </details> <details> <summary>Generator: suggest <code>any()</code></summary>  </details> <details> <summary>Tuple with a fixed length</summary>  </details> <details> <summary>Tuple with a minimum length</summary>  </details> <details> <summary>Empty tuple</summary>  </details> <details> <summary>Nonempty string literals</summary>  </details> <details> <summary>Empty string</summary>  </details> <details> <summary><code>None</code></summary>  </details> <details> <summary><code>TypedDict</code>: required fields and their declarations</summary>  </details> <details> <summary>Final class without <code>__bool__</code> or <code>__len__</code></summary>  </details> <details> <summary>Enum: implicit finality</summary>  </details> ### Strict-rule diagnostics These examples enable `redundant-condition-strict` explicitly. <details> <summary>Indexing <code>bytes</code> returns <code>int</code></summary>  </details> <details> <summary>Comparing <code>str</code> with <code>bytes</code></summary>  </details> <details> <summary>Length comparison: explain the fixed tuple length</summary>  </details> <details> <summary>Comparison: omit an obvious literal's type annotation</summary>  </details> ## Implementation The semantic index records the outermost boolean tests in each scope, so one check owns the diagnostics for a complete condition and its subexpressions. A shared checker separates classification, context exemptions, and duplicate suppression from diagnostic rendering. The main implementation is in `types/infer/builder/redundant_conditions/mod.rs`; its `diagnostic.rs` submodule constructs messages and fixes, and `exemptions.rs` handles environment provenance and defensive exits. |
||
|
|
6885154772 |
[ty] Recover cycles when constructing known class instances (#28289)
String member lookup can panic when a first-party `typing.py` introduces an inference cycle through class and metaclass resolution. Add cycle recovery to `known_class_to_instance` so inference can converge and checking reports the ordinary diagnostics. Fixes astral-sh/ty#4456. ## Test plan Add mdtests with a local `typing.py` whose function call requires resolving an unknown metaclass before a `Sequence` binding. Check the consumer both before and after the shadowing module. Both scenarios verify that `str.encode()` retains its `bytes` return type and that the unresolved metaclass produces the expected diagnostic. |
||
|
|
2136803aa7 |
[ty] Make cyclic intersection simplification deterministic (#28156)
## Summary
Previously, checking a guarded implicit attribute could reject a valid
bound-method assignment or suppress an unrelated error depending on
whether its defining class or a subclass was checked first:
```python
# base.py
class Base:
def __init__(self):
if not hasattr(self, "x"):
self.x = self.__str__
self.missing # Always report unresolved-attribute.
# child.py
from base import Base
class Child(Base):
x = Base.__str__
```
We now retain both intersection elements when simplifying them would
depend on an inference cycle. This prevents an assignment from making
its own guard unreachable based on unfinished type inference. Named and
synthesized protocols use the same rule.
For protocol reads typed as `object`, an existing class attribute
establishes presence without inferring a shadowing instance assignment.
This preserves narrowing when the class already provides the attribute.
Closes https://github.com/astral-sh/ty/issues/4076.
|