Skip to content

Conversation

@dcreager
Copy link
Member

This PR updates the constraint implication type relationship to work on compound types as well. (A compound type is a non-atomic type, like list[T].)

The goal of constraint implication is to check whether the requirements of a constraint imply that a particular subtyping relationship holds. Before, we were only checking atomic typevars. That would let us verify that the constraint set T ≤ bool implies that T is always a subtype of int. (In this case, the lhs of the subtyping check, T, is an atomic typevar.)

But we weren't recursing into compound types, to look for nested occurrences of typevars. That means that we weren't able to see that T ≤ bool implies that Covariant[T] is always a subtype of Covariant[int].

Doing this recursion means that we have to carry the constraint set along with us as we recurse into types as part of has_relation_to, by adding constraint implication as a new TypeRelation variant. (Before it was just a method on ConstraintSet.)

@dcreager dcreager added the internal An internal refactor or improvement label Nov 10, 2025
@dcreager dcreager added the ty Multi-file analysis & type inference label Nov 10, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 10, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 10, 2025

mypy_primer results

No ecosystem changes detected ✅

No memory usage changes detected ✅

@dcreager dcreager force-pushed the dcreager/deep-comparison branch 2 times, most recently from f3b001d to b39afa7 Compare November 10, 2025 17:45
@AlexWaygood
Copy link
Member

Are the primer hits here expected?

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 11, 2025

ecosystem-analyzer results

No diagnostic changes detected ✅
Full report with detailed diff (timing results)

Base automatically changed from dcreager/pep-self to main November 11, 2025 00:46
@dcreager
Copy link
Member Author

Are the primer hits here expected?

They were not! Latest patch disappears them

@sharkdp sharkdp self-assigned this Nov 11, 2025
@sharkdp
Copy link
Contributor

sharkdp commented Nov 11, 2025

I am planning to review this, but postponing to tomorrow, since Doug is out today anyway.

@carljm carljm removed their request for review November 11, 2025 19:55
@dcreager
Copy link
Member Author

I am planning to review this, but postponing to tomorrow, since Doug is out today anyway.

Thank you! No rush

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Comment on lines +1449 to +1460
// From here on out we know that both constraints constrain the same typevar. The
// clause above will propagate all that we know about the current typevar relative to
// other typevars, producing constraints on this typevar that have concrete lower/upper
// bounds. That means we can skip the simplifications below if any bound is another
// typevar.
if left_constraint.lower(db).is_type_var()
|| left_constraint.upper(db).is_type_var()
|| right_constraint.lower(db).is_type_var()
|| right_constraint.upper(db).is_type_var()
{
continue;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like an optimization, but seems to be required for correctness?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, as described (and clarified/fixed) in much more detail in #21463

Co-authored-by: David Peter <sharkdp@users.noreply.github.com>
* origin/main: (59 commits)
  [ty] Improve diagnostic range for `non-subscriptable` diagnostics (#21461)
  [ty] Improve literal promotion heuristics (#21439)
  [ty] Further improve details around which expressions should be deferred in stub files (#21456)
  [ty] Improve generic class constructor inference (#21442)
  [ty] Propagate type context through conditional expressions (#21443)
  [ty] Suppress completions when introducing names with `as`
  [ty] Add panic-by-default await methods to `TestServer` (#21451)
  [ty] name is parameter and global is a syntax error (#21312)
  [ty] Fixup a few details around version-specific dataclass features (#21453)
  [ty] Support attribute-expression `TYPE_CHECKING` conditionals (#21449)
  [ty] Support stringified annotations in value-position `Annotated` instances (#21447)
  [ty] Type inference for genererator expressions (#21437)
  [ty] Make `__getattr__` available for `ModuleType` instances (#21450)
  [ty] Increase default receive timeout in tests to 10s (#21448)
  [ty] Add synthetic members to completions on dataclasses (#21446)
  [ty] Support legacy `typing` special forms in implicit type aliases (#21433)
  Bump 0.14.5 (#21435)
  [ty] Support `type[…]` and `Type[…]` in implicit type aliases (#21421)
  [ty] Respect notebook cell boundaries when adding an auto import (#21322)
  Update PyCharm setup instructions (#21409)
  ...
@dcreager dcreager merged commit 698231a into main Nov 14, 2025
41 checks passed
@dcreager dcreager deleted the dcreager/deep-comparison branch November 14, 2025 23:43
dcreager added a commit that referenced this pull request Nov 14, 2025
* origin/main:
  [ty] Implement constraint implication for compound types (#21366)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ecosystem-analyzer internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants