Skip to content

Fix metaclass dispatch ordering for class-object operators - #22444

Open
Johnx-w wants to merge 2 commits into
python:masterfrom
Johnx-w:fix/class-object-operator-dispatch
Open

Johnx-w wants to merge 2 commits into
python:masterfrom
Johnx-w:fix/class-object-operator-dispatch

Conversation

@Johnx-w

@Johnx-w Johnx-w commented Nov 12, 2026 •

Copy link
Copy Markdown

Fixes #22377

Summary

Use the runtime metaclasses of class-object operands when deciding reflected-operator priority, while preserving the original operand types for checking the method calls. This also covers operands annotated with type[...] and bounded type variables.

 check_op_reversible
- order class-object operators using constructor types or TypeType wrappers
+ order class-object operators using their runtime metaclasses

The regression test covers overridden and inherited reflected methods, unrelated and identical metaclasses, class objects, and typed parameters.

Evidence

On master 332915e, the new regression test fails; with this change, it passes.

  • Related class, expression, overload, generic, and protocol suites: 1502 passed, 4 skipped, 1 xfailed.
  • Self-check: no issues in 349 source files.
  • Black, Ruff, and git diff --check: passed.

Merge Danger

Door: two-way. Blast radius: inferred binary-operator results for class-object operands.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

binary operator on class objects ignores the right operand's reflected method when its metaclass subclasses the left operand's metaclass

1 participant