Skip to content

[mypyc] Add note about using non-native class to subclass built-in types #19236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions mypyc/irbuild/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,16 @@ def prepare_class_def(
errors.error(
"Inheriting from most builtin types is unimplemented", path, cdef.line
)
errors.note(
"Potential workaround: @mypy_extensions.mypyc_attr(native_class=False)",
path,
cdef.line,
)
errors.note(
"https://mypyc.readthedocs.io/en/stable/native_classes.html#defining-non-native-classes",
path,
cdef.line,
)

# Set up the parent class
bases = [mapper.type_to_ir[base.type] for base in info.bases if base.type in mapper.type_to_ir]
Expand Down
4 changes: 3 additions & 1 deletion mypyc/test-data/commandline.test
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ Foo.lol = 50 # E: Only class variables defined as ClassVar can be assigned to
def decorator(x: Any) -> Any:
return x

class NeverMetaclass(type): # E: Inheriting from most builtin types is unimplemented
class NeverMetaclass(type): # E: Inheriting from most builtin types is unimplemented \
# N: Potential workaround: @mypy_extensions.mypyc_attr(native_class=False) \
# N: https://mypyc.readthedocs.io/en/stable/native_classes.html#defining-non-native-classes
pass

class Concrete1:
Expand Down
4 changes: 3 additions & 1 deletion mypyc/test-data/irbuild-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,9 @@ class BadUse(): # E: native_class must be used with True or False only
from mypy_extensions import mypyc_attr

@mypyc_attr(native_class=True)
class M(type): # E: Inheriting from most builtin types is unimplemented
class M(type): # E: Inheriting from most builtin types is unimplemented \
# N: Potential workaround: @mypy_extensions.mypyc_attr(native_class=False) \
# N: https://mypyc.readthedocs.io/en/stable/native_classes.html#defining-non-native-classes
pass

@mypyc_attr(native_class=True)
Expand Down
47 changes: 47 additions & 0 deletions mypyc/test-data/run-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,53 @@ def welp() -> int:
from native import welp
assert welp() == 35

[case testSubclassUnsupportedException]
from mypy_extensions import mypyc_attr

@mypyc_attr(native_class=False)
class MyError(ZeroDivisionError):
pass

@mypyc_attr(native_class=False)
class MyError2(ZeroDivisionError):
def __init__(self, s: str) -> None:
super().__init__(s + "!")
self.x = s.upper()

def f() -> None:
raise MyError("foobar")

def test_non_native_exception_subclass_basics() -> None:
e = MyError()
assert isinstance(e, MyError)
assert isinstance(e, ZeroDivisionError)
assert isinstance(e, Exception)

e = MyError("x")
assert repr(e) == "MyError('x')"

e2 = MyError2("ab")
assert repr(e2) == "MyError2('ab!')", repr(e2)
assert e2.x == "AB"

def test_raise_non_native_exception_subclass_1() -> None:
try:
f()
except MyError:
x = True
else:
assert False
assert x

def test_raise_non_native_exception_subclass_2() -> None:
try:
f()
except ZeroDivisionError:
x = True
else:
assert False
assert x

[case testSubclassPy]
from b import B, V
class A(B):
Expand Down