Repository navigation
Conversation
This comment has been minimized.
This comment has been minimized.
chrikrah
left a comment
There was a problem hiding this comment.
Mergeable from my side; reverting the fix fails the new test.
- non-blocking:
type[A] * type[B]still infersint, runtimestr; TypeType isn't unwrapped.
$ python -m mypy probe.py # A*B, A*C, A*D, SubA*SubB, A*SubB, A*RightMeta(...); f(x: type[A], y: type[B]) reveals x*y
base 55472c0: Found 4 errors in 1 file (Expression is of type "int", not "str"); probe.py:26: note: Revealed type is "int"
head 1903600: Success: no issues found in 1 source file; probe.py:26: note: Revealed type is "int"
$ python probe.py
A*B, A*C, A*D: str int int
f(A, B): str
$ pytest -n 4 -q -k testReverseOperatorOrderingForClassObjects mypy/test/testcheck.py
head: 1 passed checkexpr.py reverted to base: 1 failed (main:20: Expression is of type "int", not "str")
$ pytest -n 4 -q mypy/test/testcheck.py -k "check-classes or check-expressions or check-overloading or check-generics or check-protocols"
1502 passed, 4 skipped, 1 xfailed
Here or in a follow-up?
This comment has been minimized.
This comment has been minimized.
|
Thanks for the review! I addressed the type[A] / type[B] case in this PR in 29c567e, with regression tests for typed parameters and bounded type variables. I also corrected dispatch for operands sharing a metaclass. All CI checks are green. |
chrikrah
left a comment
There was a problem hiding this comment.
29c567e covers type[A] * type[B]; still mergeable from my side.
$ python -m mypy probe.py # a: type[A], b: type[B], sub: type[SubA], a: type[T bound A]
head 29c567e: a*b "str", sub*b "int", T-bound a*b "str" # runtime: str, int, str
checkexpr.py from 1903600: "int", "int", "int"
$ pytest -q -n 0 mypy/test/testcheck.py -k testReverseOperatorOrderingForClassObjects
head: 1 passed checkexpr.py from 1903600: 1 failed (main:25, 29, 31, 39)
$ pytest -q -n 0 mypy/test/testcheck.py -k "check-classes.test or check-expressions.test or check-generics.test"
1019 passed, 2 skipped, 1 xfailed
|
Diff from mypy_primer, showing the effect of this PR on open source code: pywin32 (https://github.com/mhammond/pywin32)
./pythonwin/pywin/test/_dbgscript.py: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
- version: 2.5.0+dev.1e2ea5350dd8fa4d44a1d381b1ae0097d880ed2d
+ version: 2.5.0+dev.d865706282268ecbdc304a7c2364fe71c1f1bf49
|
|
The latest mypy_primer result on this PR reports a new INTERNAL ERROR in That's a regression signal despite the focused suites passing. Could you reproduce this against the current head, capture the traceback, and add a minimal regression test before merging? The change modifies shared reflected-operator dispatch, so I wouldn't classify this as unrelated without isolating the failing expression. |
Fixes #22377
Summary
Binary operators now use metaclasses to determine dispatch order for both direct class objects (
A * B) and class objects passed astype[A]/type[B]parameters.An overriding right-metaclass
__rmul__takes priority. Inherited or unrelated reflected methods keep left-method priority, and operands with the same metaclass do not try the reflected method.Evidence
intinstead ofstr. Additional cases failed for typed parameters, bounded type variables, and direct class operands sharing a metaclass.After: All regression cases pass.
check-classes,check-expressions,check-overloading,check-generics,check-protocols): 1,502 passed, 4 skipped, 1 xfailed.Merge Danger
Door: two-way
Blast Radius: inference
Affects binary-operator result typing for class objects and parameters annotated with
type[...].