8000 Add a few more tests for mypyc_attr native_class (dunder methods and metaclasses) by svalentin · Pull Request #18999 · python/mypy · GitHub
[go: up one dir, main page]

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 1 commit
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
Prev Previous commit
Use 3.9 syntax + count dunder calls
  • Loading branch information
  • 8000
svalentin committed May 2, 2025
commit 7059bbe720dcfd8386a7e3d3c4ab4cbfe85f439f
1 change: 0 additions & 1 deletion mypyc/test-data/irbuild-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1381,4 +1381,3 @@ class M(type): # E: Inheriting from most builtin types is unimplemented
@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

16 changes: 14 additions & 2 deletions mypyc/test-data/run-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2890,20 +2890,28 @@ def test_function():

[case testMypycAttrNativeClassDunder]
from mypy_extensions import mypyc_attr
from typing import Generic, TypeVar
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: type[_T] | None = None) -> str:
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)
Expand All @@ -2915,11 +2923,15 @@ 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
Expand Down
0