Skip to content

[ty] normalize typevar bounds/constraints in cycles#21800

Merged
carljm merged 2 commits intomainfrom
cjm/boundcycle
Dec 4, 2025
Merged

[ty] normalize typevar bounds/constraints in cycles#21800
carljm merged 2 commits intomainfrom
cjm/boundcycle

Conversation

@carljm
Copy link
Contributor

@carljm carljm commented Dec 4, 2025

Fixes astral-sh/ty#1587

Summary

Perform cycle normalization on typevar bounds and constraints (similar to how it was already done for typevar defaults) in order to ensure convergence in cyclic cases.

There might be another fix here that could avoid the cycle in many more cases, where we don't eagerly evaluate typevar bounds/constraints on explicit specialization, but just accept the given specialization and later evaluate to see whether we need to emit a diagnostic on it. But the current fix here is sufficient to solve the problem and matches the patterns we use to ensure cycle convergence elsewhere, so it seems good for now; left a TODO for the other idea.

This fix is sufficient to make us not panic, but not sufficient to get the semantics fully correct; see the TODOs in the tests. I have ideas for fixing that as well, but it seems worth at least getting this in to fix the panic.

Test Plan

Test that previously panicked now does not.

@carljm carljm added the ty Multi-file analysis & type inference label Dec 4, 2025
@astral-sh-bot
Copy link

astral-sh-bot bot commented Dec 4, 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 Dec 4, 2025

mypy_primer results

Changes were detected when running on open source projects
beartype (https://github.com/beartype/beartype)
- beartype/claw/_package/clawpkgtrie.py:66:29: warning[unsupported-base] Unsupported class base with type `<class 'dict[str, PackagesTrieBlacklist]'> | <class 'dict[str, Divergent]'>`
- beartype/claw/_package/clawpkgtrie.py:247:29: warning[unsupported-base] Unsupported class base with type `<class 'dict[str, PackagesTrieWhitelist]'> | <class 'dict[str, Divergent]'>`
- Found 494 diagnostics
+ Found 492 diagnostics

scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/_logging.py:153:13: warning[unsupported-base] Unsupported class base with type `<class 'Mapping[str, Style]'> | <class 'Mapping[str, Divergent]'>`
- Found 39 diagnostics
+ Found 38 diagnostics

dd-trace-py (https://github.com/DataDog/dd-trace-py)
+ ddtrace/testing/internal/test_data.py:140:15: warning[unsupported-base] Unsupported class base with type `<class 'TestItem[Test, Never]'> | <class 'TestItem[Unknown, Unknown]'>`
- Found 8325 diagnostics
+ Found 8326 diagnostics

rotki (https://github.com/rotki/rotki)
- rotkehlchen/accounting/structures/processed_event.py:85:75: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- rotkehlchen/api/rest.py:1041:73: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- Found 2052 diagnostics
+ Found 2050 diagnostics

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
- pandas-stubs/_typing.pyi:1209:16: warning[unused-ignore-comment] Unused blanket `type: ignore` directive
- Found 5514 diagnostics
+ Found 5513 diagnostics

No memory usage changes detected ✅

@carljm carljm marked this pull request as ready for review December 4, 2025 22:01
Copy link
Member

@dcreager dcreager left a comment

Choose a reason for hiding this comment

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

👍 to landing this separately from any follow-on work

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
@carljm carljm merged commit a9de6b5 into main Dec 4, 2025
40 checks passed
@carljm carljm deleted the cjm/boundcycle branch December 4, 2025 23:17
dcreager added a commit that referenced this pull request Dec 5, 2025
* origin/main: (41 commits)
  [ty] Carry generic context through when converting class into `Callable` (#21798)
  [ty] Add more tests for renamings (#21810)
  [ty] Minor improvements to `assert_type` diagnostics (#21811)
  [ty] Add some attribute/method renaming test cases (#21809)
  Update mkdocs-material to 9.7.0 (Insiders now free) (#21797)
  Remove unused whitespaces in test cases (#21806)
  [ty] fix panic when instantiating a type variable with invalid constraints (#21663)
  [ty] fix build failure caused by conflicts between #21683 and #21800 (#21802)
  [ty] do nothing with `store_expression_type` if `inner_expression_inference_state` is `Get` (#21718)
  [ty] increase the limit on the number of elements in a non-recursively defined literal union (#21683)
  [ty] normalize typevar bounds/constraints in cycles (#21800)
  [ty] Update completion eval to include modules
  [ty] Add modules to auto-import
  [ty] Add support for module-only import requests
  [ty] Refactor auto-import symbol info
  [ty] Clarify the use of `SymbolKind` in auto-import
  [ty] Redact ranking of completions from e2e LSP tests
  [ty] Tweaks tests to use clearer language
  [ty] Update evaluation results
  [ty] Make auto-import ignore symbols in modules starting with a `_`
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pair of mutually recursive generic Protocol definitions

3 participants

Comments