Repository navigation
[mypyc] Fix unchecked results of operations on overriding subclasses - #22416
Merged
JukkaL merged 1 commit intoNov 10, 2026
Merged
Conversation
Several primitives declare an exact return type (str, list, tuple or dict), but can call a method overridden in a Python subclass, and the result was trusted without a type check. For example, if a list subclass defines __add__ that returns an int, `c = a + b; c[0]` with `a: list[int]` would crash. For list and tuple +, +=, *, *= and list.copy(), and dict.copy(), keep calling the override but raise TypeError if the result has the wrong type. Also call __rmul__ instead of __mul__ for `n * seq` when seq is a list or tuple subclass. For str * int and str.lower()/str.upper() (non-ASCII, Python 3.13+), always use the str implementation, so the result is an exact str. This matches how other str primitives such as str + str already work.
JukkaL
approved these changes
Nov 10, 2026
JukkaL
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the PR, looks good! It appears that some bytes primitives have a similar issue -- do you want to make a follow-up PR (e.g. CPyBytes_Multiply)?
Contributor
Author
|
Thanks! Opened #22431 for |
13 of 14 tasks
JukkaL
pushed a commit
that referenced
this pull request
Nov 11, 2026
…asses (#22431) Follow-up to #22416, which fixed this for list, tuple, dict and str. `bytes * int`, `int * bytes`, `bytes.join()`, `bytes.translate()` and `bytes[i:j]` (with indices that don't fit in a short int) call the overriding method when the object is a `bytes` subclass. The primitives declare a `bytes` result, but the result was never checked, so an override that returns another type was used as `bytes`: ```python # sub.py (not compiled) class B(bytes): def __mul__(self, n): return 42 # compiled def f() -> None: b: bytes = B(b"x") r = b * 2 print(len(r)) # interpreted: TypeError; compiled: prints garbage (reads an int as bytes) ``` Same approach as for lists and tuples in #22416: the override is still called (the existing `testBytesJoin` test relies on this), but a non-`bytes` result raises `TypeError`. `int * bytes` now calls `__rmul__` on subclasses, not `__mul__`, matching CPython.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several mypyc primitives declare an exact return type (
str,list,tupleordict), but they can end up calling a method that a Python subclass overrides. The result was then trusted without a type check, which can crash:The same problem affects:
+=,*,n *,*=and.copy();+,*andn *;dict.copy();str * int. A heap str subclass that defines__mul__has a NULLsq_repeat, soPySequence_Repeatfalls through to the override;str.lower()/str.upper()on non-ASCII strings on Python 3.13+.Fix:
TypeError(e.g. "list object expected; got int") if the result has the wrong type.n * seqnow calls__rmul__instead of__mul__on subclasses.str * intandstr.lower()/str.upper()always use thestrimplementation, so the result is an exactstr. Other str primitives such asstr + str(PyUnicode_Concat) already ignore subclass overrides, and this matches them.Added run tests with overriding subclasses defined in a non-compiled module.