Skip to content

[ty] avoid unions of generic aliases of the same class in fixpoint#21909

Merged
carljm merged 2 commits intomainfrom
cjm/nounion
Dec 11, 2025
Merged

[ty] avoid unions of generic aliases of the same class in fixpoint#21909
carljm merged 2 commits intomainfrom
cjm/nounion

Conversation

@carljm
Copy link
Contributor

@carljm carljm commented Dec 10, 2025

Partially addresses astral-sh/ty#1732
Fixes astral-sh/ty#1800

Summary

At each fixpoint iteration, we union the "previous" and "current" iteration types, to ensure that the type can only widen at each iteration. This prevents oscillation and ensures convergence.

But some unions triggered by this behavior (in particular, unions of differently-specialized generic-aliases of the same class) never simplify, and cause spurious errors. Since we haven't seen examples of oscillating types involving class-literal or generic-alias types, just don't union those.

There may be more thorough/principled ways to avoid undesirable unions in fixpoint iteration, but this narrow change seems like it results in strict improvement.

Test Plan

Removes two false positive unsupported-class-base in mdtests, and several in the ecosystem, without causing other regression.

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

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

mypy_primer results

Changes were detected when running on open source projects
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 42 diagnostics
+ Found 41 diagnostics

strawberry (https://github.com/strawberry-graphql/strawberry)
- strawberry/channels/handlers/ws_handler.py:69:5: warning[unsupported-base] Unsupported class base with type `<class 'AsyncBaseHTTPView[GraphQLWSConsumer, GraphQLWSConsumer, GraphQLWSConsumer, GraphQLWSConsumer, GraphQLWSConsumer, Context@GraphQLWSConsumer, RootValue@GraphQLWSConsumer]'> | <class 'AsyncBaseHTTPView[GraphQLWSConsumer[None, None], GraphQLWSConsumer[None, None], GraphQLWSConsumer[None, None], GraphQLWSConsumer[None, None], GraphQLWSConsumer[None, None], Context@GraphQLWSConsumer, RootValue@GraphQLWSConsumer]'>`
- Found 402 diagnostics
+ Found 401 diagnostics

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

No memory usage changes detected ✅

@carljm carljm marked this pull request as ready for review December 11, 2025 00:09
@carljm
Copy link
Contributor Author

carljm commented Dec 11, 2025

@mtshiba I can't request your review, but would be happy for you to take a look at this.

@AlexWaygood
Copy link
Member

AlexWaygood commented Dec 11, 2025

Have we actually seen any issues reported due to unions of class-literal types from our fixpoint machinery? Or has it just been generic-alias types?

I'm not sure how we ever would get a spurious union of class-literal types from our fixpoint machinery. Though if we did, I agree it could cause similar issues to the ones we've seen in the ecosystem (and in issue reports) regarding generic-alias types

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Seems worth a try and, at the very least, would help us learn where our assumption is off

@carljm carljm changed the title [ty] avoid unions of class literals / generic aliases in fixpoint [ty] avoid unions of generic aliases of the same class in fixpoint Dec 11, 2025
@carljm carljm merged commit 4fdb4e8 into main Dec 11, 2025
91 of 94 checks passed
@carljm carljm deleted the cjm/nounion branch December 11, 2025 17:53
dcreager added a commit that referenced this pull request Dec 11, 2025
* origin/main: (29 commits)
  Document range suppressions, reorganize suppression docs (#21884)
  Ignore ruff:isort like ruff:noqa in new suppressions (#21922)
  [ty] Handle `Definition`s in `SemanticModel::scope` (#21919)
  [ty] Attach salsa db when running ide tests for easier debugging (#21917)
  [ty] Don't show hover for expressions with no inferred type (#21924)
  [ty] avoid unions of generic aliases of the same class in fixpoint (#21909)
  [ty] Squash false positive logs for failing to find `builtins` as a real module
  [ty] Uniformly use "not supported" in diagnostics (#21916)
  [ty] Reduce size of ty-ide snapshots (#21915)
  [ty] Adjust scope completions to use all reachable symbols
  [ty] Rename `all_members_of_scope` to `all_end_of_scope_members`
  [ty] Remove `all_` prefix from some routines on UseDefMap
  Enable `--document-private-items` for `ruff_python_formatter` (#21903)
  Remove `BackwardsTokenizer` based `parenthesized_range` references in `ruff_linter` (#21836)
  [ty] Revert "Do not infer types for invalid binary expressions in annotations" (#21914)
  Skip over trivia tokens after re-lexing (#21895)
  [ty] Avoid inferring types for invalid binary expressions in string annotations (#21911)
  [ty] Improve overload call resolution tracing (#21913)
  [ty] fix missing heap_size on Salsa query (#21912)
  [ty] Support implicit type of `cls` in signatures (#21771)
  ...
dcreager added a commit that referenced this pull request Dec 11, 2025
* origin/main: (36 commits)
  [ty] Defer all parameter and return type annotations (#21906)
  [ty] Fix workspace symbols to return members too (#21926)
  Document range suppressions, reorganize suppression docs (#21884)
  Ignore ruff:isort like ruff:noqa in new suppressions (#21922)
  [ty] Handle `Definition`s in `SemanticModel::scope` (#21919)
  [ty] Attach salsa db when running ide tests for easier debugging (#21917)
  [ty] Don't show hover for expressions with no inferred type (#21924)
  [ty] avoid unions of generic aliases of the same class in fixpoint (#21909)
  [ty] Squash false positive logs for failing to find `builtins` as a real module
  [ty] Uniformly use "not supported" in diagnostics (#21916)
  [ty] Reduce size of ty-ide snapshots (#21915)
  [ty] Adjust scope completions to use all reachable symbols
  [ty] Rename `all_members_of_scope` to `all_end_of_scope_members`
  [ty] Remove `all_` prefix from some routines on UseDefMap
  Enable `--document-private-items` for `ruff_python_formatter` (#21903)
  Remove `BackwardsTokenizer` based `parenthesized_range` references in `ruff_linter` (#21836)
  [ty] Revert "Do not infer types for invalid binary expressions in annotations" (#21914)
  Skip over trivia tokens after re-lexing (#21895)
  [ty] Avoid inferring types for invalid binary expressions in string annotations (#21911)
  [ty] Improve overload call resolution tracing (#21913)
  ...
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.

false positive unsupported-base for self-referential generic base

4 participants

Comments