Skip to content

Add a few more tests for mypyc_attr native_class (dunder methods and metaclasses) #18999

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 3 commits into from
May 2, 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
11 changes: 11 additions & 0 deletions mypyc/test-data/irbuild-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1370,3 +1370,14 @@ class NonNativeClassContradiction(): # E: Class is marked as native_class=True
@mypyc_attr(native_class="yes")
class BadUse(): # E: native_class must be used with True or False only
pass

[case testMypycAttrNativeClassMetaError]
from mypy_extensions import mypyc_attr

@mypyc_attr(native_class=True)
class M(type): # E: Inheriting from most builtin types is unimplemented
pass

@mypyc_attr(native_class=True)
class A(metaclass=M): # E: Class is marked as native_class=True but it can't be a native class
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe also mention in the error message that the metaclass is not supported with a native class?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Let me separate this out to its own PR. Right now is_implicit_extension_class (in mypyc/irbuild/util.py) just returns True/False. To print a nice message we would have to go through each individual check in is_implicit_extension_class and make a nice user-facing message.
It is very good feedback, and I agree with you, just think it can be split out to separate PR.

pass
75 changes: 75 additions & 0 deletions mypyc/test-data/run-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2887,3 +2887,78 @@ def test_function():
explicit_ext_inst = AnnotatedExtensionClass()
with assertRaises(AttributeError):
setattr(explicit_ext_inst, 'attr_instance', 6)

[case testMypycAttrNativeClassDunder]
from mypy_extensions import mypyc_attr
from typing import Generic, Optional, TypeVar

_T = TypeVar("_T")

get_count = set_count = del_count = 0

@mypyc_attr(native_class=False)
class Bar(Generic[_T]):
# Note the lack of __deletable__
def __init__(self) -> None:
self.value: str = 'start'
def __get__(self, instance: _T, owner: Optional[type[_T]] = None) -> str:
global get_count
get_count += 1
return self.value
def __set__(self, instance: _T, value: str) -> None:
global set_count
set_count += 1
self.value = value
def __delete__(self, instance: _T) -> None:
global del_count
del_count += 1
del self.value

@mypyc_attr(native_class=False)
class Foo(object):
bar: Bar = Bar()

[file driver.py]
import native

f = native.Foo()
assert(hasattr(f, 'bar'))
assert(native.get_count == 1)
assert(f.bar == 'start')
assert(native.get_count == 2)
f.bar = 'test'
assert(f.bar == 'test')
assert(native.set_count == 1)
del f.bar
assert(not hasattr(f, 'bar'))
assert(native.del_count == 1)

[case testMypycAttrNativeClassMeta]
from mypy_extensions import mypyc_attr
from typing import ClassVar, TypeVar

_T = TypeVar("_T")

@mypyc_attr(native_class=False)
class M(type):
count: ClassVar[int] = 0
def make(cls: type[_T]) -> _T:
M.count += 1
return cls()

# implicit native_class=False
# see testMypycAttrNativeClassMetaError for when trying to set it True
class A(metaclass=M):
pass

[file driver.py]
import native

a: native.A = native.A.make()
assert(native.A.count == 1)

class B(native.A):
pass

b: B = B.make()
assert(B.count == 2)