ruff/crates/ty_python_semantic/resources/mdtest
Douglas Creager 45842cc034
[ty] Fix non-determinism in ConstraintSet.specialize_constrained (#21744)
This fixes a non-determinism that we were seeing in the constraint set
tests in https://github.com/astral-sh/ruff/pull/21715.

In this test, we create the following constraint set, and then try to
create a specialization from it:

```
(T@constrained_by_gradual_list = list[Base])
  ∨
(Bottom[list[Any]] ≤ T@constrained_by_gradual_list ≤ Top[list[Any]])
```

That is, `T` is either specifically `list[Base]`, or it's any `list`.
Our current heuristics say that, absent other restrictions, we should
specialize `T` to the more specific type (`list[Base]`).

In the correct test output, we end up creating a BDD that looks like
this:

```
(T@constrained_by_gradual_list = list[Base])
┡━₁ always
└─₀ (Bottom[list[Any]] ≤ T@constrained_by_gradual_list ≤ Top[list[Any]])
    ┡━₁ always
    └─₀ never
```

In the incorrect output, the BDD looks like this:

```
(Bottom[list[Any]] ≤ T@constrained_by_gradual_list ≤ Top[list[Any]])
┡━₁ always
└─₀ never
```

The difference is the ordering of the two individual constraints. Both
constraints appear in the first BDD, but the second BDD only contains `T
is any list`. If we were to force the second BDD to contain both
constraints, it would look like this:

```
(Bottom[list[Any]] ≤ T@constrained_by_gradual_list ≤ Top[list[Any]])
┡━₁ always
└─₀ (T@constrained_by_gradual_list = list[Base])
    ┡━₁ always
    └─₀ never
```

This is the standard shape for an OR of two constraints. However! Those
two constraints are not independent of each other! If `T` is
specifically `list[Base]`, then it's definitely also "any `list`". From
that, we can infer the contrapositive: that if `T` is not any list, then
it cannot be `list[Base]` specifically. When we encounter impossible
situations like that, we prune that path in the BDD, and treat it as
`false`. That rewrites the second BDD to the following:

```
(Bottom[list[Any]] ≤ T@constrained_by_gradual_list ≤ Top[list[Any]])
┡━₁ always
└─₀ (T@constrained_by_gradual_list = list[Base])
    ┡━₁ never   <-- IMPOSSIBLE, rewritten to never
    └─₀ never
```

We then would see that that BDD node is redundant, since both of its
outgoing edges point at the `never` node. Our BDDs are _reduced_, which
means we have to remove that redundant node, resulting in the BDD we saw
above:

```
(Bottom[list[Any]] ≤ T@constrained_by_gradual_list ≤ Top[list[Any]])
┡━₁ always
└─₀ never       <-- redundant node removed
```

The end result is that we were "forgetting" about the `T = list[Base]`
constraint, but only for some BDD variable orderings.

To fix this, I'm leaning in to the fact that our BDDs really do need to
"remember" all of the constraints that they were created with. Some
combinations might not be possible, but we now have the sequent map,
which is quite good at detecting and pruning those.

So now our BDDs are _quasi-reduced_, which just means that redundant
nodes are allowed. (At first I was worried that allowing redundant nodes
would be an unsound "fix the glitch". But it turns out they're real!
[This](https://ieeexplore.ieee.org/abstract/document/130209) is the
paper that introduces them, though it's very difficult to read. Knuth
mentions them in §7.1.4 of
[TAOCP](https://course.khoury.northeastern.edu/csu690/ssl/bdd-knuth.pdf),
and [this paper](https://par.nsf.gov/servlets/purl/10128966) has a nice
short summary of them in §2.)

While we're here, I've added a bunch of `debug` and `trace` level log
messages to the constraint set implementation. I was getting tired of
having to add these by hands over and over. To enable them, just set
`TY_LOG` in your environment, e.g.

```sh
env TY_LOG=ty_python_semantic::types::constraints::SequentMap=trace ty check ...
```

[Note, this has an `internal` label because are still not using
`specialize_constrained` in anything user-facing yet.]
2025-12-03 10:19:39 -05:00
..
annotations [ty] Don't confuse multiple occurrences of typing.Self when binding bound methods (#21754) 2025-12-02 13:15:09 -05:00
assignment [ty] Avoid expression reinference for diagnostics (#21267) 2025-11-25 09:24:00 -08:00
binary [ty] Improve diagnostics for unsupported comparison operations (#21737) 2025-12-02 19:58:45 +00:00
boolean
boundness_declaredness [ty] Reformulation of public symbol inference test suite (#20667) 2025-10-01 14:26:17 +02:00
call [ty] Avoid expression reinference for diagnostics (#21267) 2025-11-25 09:24:00 -08:00
class [ty] Support type[T] with type variables (#21650) 2025-11-28 09:20:24 +01:00
comparison [ty] Improve diagnostics for unsupported comparison operations (#21737) 2025-12-02 19:58:45 +00:00
comprehensions [ty] fix global symbol lookup from eager scopes (#21317) 2025-11-12 10:15:51 -08:00
conditional
dataclasses [ty] Suppress false positives when dataclasses.dataclass(...)(cls) is called imperatively (#21729) 2025-12-03 08:05:25 +00:00
declaration
diagnostics Fix syntax error false positives for await outside functions (#21763) 2025-12-02 21:02:02 +00:00
directives [ty] Custom concise diagnostic messages (#21498) 2025-11-18 09:35:40 +01:00
doc
exception [ty] Improve diagnostics for invalid exceptions (#21475) 2025-11-15 22:12:00 +00:00
expression [ty] Check method definitions on subclasses for Liskov violations (#21436) 2025-11-23 18:08:15 +00:00
function [ty] Sync vendored typeshed stubs (#21466) 2025-11-15 17:12:32 +00:00
generics [ty] Fix non-determinism in ConstraintSet.specialize_constrained (#21744) 2025-12-03 10:19:39 -05:00
ide_support [ty] Support type[T] with type variables (#21650) 2025-11-28 09:20:24 +01:00
import [ty] add tests for workspaces (#21741) 2025-12-02 06:43:41 -05:00
libraries [ty] Generic types aliases (implicit and PEP 613) (#21553) 2025-11-28 20:38:24 +01:00
literal [ty] Type inference for genererator expressions (#21437) 2025-11-14 13:04:11 +00:00
loops [ty] Fix bug where ty would think all types had an __mro__ attribute (#20995) 2025-10-27 11:19:12 +00:00
narrow [ty] Eagerly evaluate types.UnionType elements as type expressions (#21531) 2025-11-20 17:28:48 +01:00
regression [ty] handle recursive type inference properly (#20566) 2025-11-26 08:50:26 -08:00
scopes [ty] Make __getattr__ available for ModuleType instances (#21450) 2025-11-14 13:59:14 +01:00
shadowing
snapshots [ty] Improve @override, @final and Liskov checks in cases where there are multiple reachable definitions (#21767) 2025-12-03 12:51:36 +00:00
stubs [ty] Better invalid-assignment diagnostics (#21476) 2025-11-18 14:31:04 +01:00
subscript [ty] support type[tuple[...]] (#21652) 2025-12-01 11:49:26 +01:00
suppressions [ty] Add code action to ignore diagnostic on the current line (#21595) 2025-11-29 15:41:54 +01:00
type_compendium [ty] Improve literal promotion heuristics (#21439) 2025-11-14 16:13:56 -05:00
type_of [ty] type[T] is assignable to an inferable typevar (#21766) 2025-12-02 18:25:09 -05:00
type_properties [ty] Stop testing the (brittle) constraint set display implementation (#21743) 2025-12-02 09:17:29 +01:00
type_qualifiers [ty] Fix false positive for Final attribute assignment in __init__ (#21158) 2025-11-11 12:54:05 -08:00
unary
with [ty] Use typing.Self for the first parameter of instance methods (#20517) 2025-09-29 21:08:08 +02:00
.mdformat.toml
async.md [ty] Generic types aliases (implicit and PEP 613) (#21553) 2025-11-28 20:38:24 +01:00
attributes.md [ty] Support type[T] with type variables (#21650) 2025-11-28 09:20:24 +01:00
bidirectional.md [ty] Improve diagnostics for unsupported comparison operations (#21737) 2025-12-02 19:58:45 +00:00
classes.md [ty] Fix bug where ty would think all types had an __mro__ attribute (#20995) 2025-10-27 11:19:12 +00:00
cycle.md [ty] remove the visitor parameter in the recursive_type_normalized_impl method (#21701) 2025-12-01 08:48:43 +01:00
decorators.md
del.md [ty] No union with Unknown for module-global symbols (#20664) 2025-10-01 16:40:30 +02:00
deprecated.md
descriptor_protocol.md [ty] Use the return type of __get__ for descriptor lookups even when __get__ is called with incorrect arguments (#21424) 2025-11-13 12:05:10 +00:00
enums.md [ty] Support type[T] with type variables (#21650) 2025-11-28 09:20:24 +01:00
exhaustiveness_checking.md [ty] Exhaustiveness checking for generic classes (#21726) 2025-12-01 13:52:36 +01:00
final.md [ty] Improve @override, @final and Liskov checks in cases where there are multiple reachable definitions (#21767) 2025-12-03 12:51:36 +00:00
implicit_type_aliases.md [ty] Default-specialization of generic type aliases (#21765) 2025-12-03 09:10:45 +01:00
instance_layout_conflict.md
intersection_types.md [ty] Use "cannot" consistently over "can not" (#21255) 2025-11-03 10:38:20 -05:00
invalid_syntax.md [ty] Implicit type aliases: Add support for typing.Union (#21363) 2025-11-12 12:59:14 +01:00
known_constants.md
liskov.md [ty] Improve @override, @final and Liskov checks in cases where there are multiple reachable definitions (#21767) 2025-12-03 12:51:36 +00:00
literal_promotion.md [ty] Narrow type context during literal promotion in generic class constructors (#21574) 2025-11-21 17:05:32 -05:00
mdtest_config.md
mdtest_custom_typeshed.md
metaclass.md
mro.md [ty] Sync vendored typeshed stubs (#21466) 2025-11-15 17:12:32 +00:00
named_tuple.md [ty] Improve @override, @final and Liskov checks in cases where there are multiple reachable definitions (#21767) 2025-12-03 12:51:36 +00:00
overloads.md [ty] Infer type of self for decorated methods and properties (#21123) 2025-10-29 21:22:38 +00:00
override.md [ty] Improve @override, @final and Liskov checks in cases where there are multiple reachable definitions (#21767) 2025-12-03 12:51:36 +00:00
pep613_type_aliases.md [ty] Support typevar-specialized dynamic types in generic type aliases (#21730) 2025-12-03 10:00:02 +01:00
pep695_type_aliases.md [ty] Support typevar-specialized dynamic types in generic type aliases (#21730) 2025-12-03 10:00:02 +01:00
properties.md [ty] Use the return type of __get__ for descriptor lookups even when __get__ is called with incorrect arguments (#21424) 2025-11-13 12:05:10 +00:00
protocols.md [ty] Support type[T] with type variables (#21650) 2025-11-28 09:20:24 +01:00
public_types.md [ty] Disambiguate classes that live in different modules but have the same fully qualified names (#20756) 2025-10-08 18:27:40 +01:00
statically_known_branches.md [ty] Rename "possibly unbound" diagnostics to "possibly missing" (#20492) 2025-09-23 14:26:55 +00:00
sys_platform.md
sys_version_info.md [ty] support PEP 613 type aliases (#21394) 2025-11-20 17:59:35 -08:00
t_strings.md
terminal_statements.md
ty_extensions.md [ty] Stop testing the (brittle) constraint set display implementation (#21743) 2025-12-02 09:17:29 +01:00
typed_dict.md [ty] Preserve quoting style when autofixing TypedDict keys (#21682) 2025-11-28 18:40:34 +00:00
union_types.md [ty] Introduce TypeRelation::Redundancy (#20602) 2025-10-03 18:35:30 +01:00
unpacking.md
unreachable.md [ty] Add subdiagnostic hint if a variable with type Never is used in a type expression (#21660) 2025-11-27 12:48:18 +00:00