Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-90562: Fix super() without args calls for dataclasses with slots #111538

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
Loading
from
Open
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
Next Next commit
Looks like we don't need to unwrap anything
  • Loading branch information
sobolevn committed Mar 12, 2024
commit ee3edbff1b81fae68a39c9f113b0d0debe49dc09
88 changes: 79 additions & 9 deletions 88 Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict
from typing import get_type_hints
from collections import deque, OrderedDict, namedtuple, defaultdict
from functools import total_ordering
from functools import total_ordering, wraps, cache

import typing # Needed for the string "typing.ClassVar[int]" to work as an annotation.
import dataclasses # Needed for the string "dataclasses.InitVar[int]" to work as an annotation.
Expand Down Expand Up @@ -3499,6 +3499,13 @@ class A(Base):

def test_super_without_params_and_slots(self):
# https://github.com/python/cpython/issues/111500

def decorator(func):
@wraps(func)
def inner(*args, **kwargs):
return func(*args, **kwargs)
return inner

for slots in (True, False):
with self.subTest(slots=slots):
@dataclass(slots=slots)
Expand All @@ -3508,15 +3515,18 @@ def __post_init__(self):
self.x = 1
def method(self):
return 2
@decorator
def decorated(self):
return 3
@property
def prop(self):
return 3
return 4
@classmethod
def clsmethod(cls):
return 4
return 5
@staticmethod
def stmethod():
return 5
return 6

@dataclass(slots=slots)
class Child(Base):
Expand All @@ -3528,6 +3538,11 @@ def __post_init__(self):
self.z = 3
def method(self):
return super().method()
def decorated1(self):
return super().decorated()
@decorator
def decorated2(self):
return super().decorated()
@property
def prop(self):
return super().prop
Expand All @@ -3540,24 +3555,79 @@ def stmethod1():
@staticmethod
def stmethod2():
return super().stmethod()
def cached1(self):
return super().cached()

inst = Child()
self.assertEqual(inst.x, 1)
self.assertEqual(inst.y, 2)
self.assertEqual(inst.z, 3)
self.assertEqual(inst.method(), 2)
self.assertEqual(inst.prop, 3)
self.assertEqual(inst.clsmethod(), 4)
self.assertEqual(Child.clsmethod(), 4)
self.assertEqual(inst.stmethod1(), 5)
self.assertEqual(Child.stmethod1(), 5)
self.assertEqual(inst.decorated1(), 3)
self.assertEqual(inst.decorated2(), 3)
self.assertEqual(inst.prop, 4)
self.assertEqual(inst.clsmethod(), 5)
self.assertEqual(Child.clsmethod(), 5)
self.assertEqual(inst.stmethod1(), 6)
self.assertEqual(Child.stmethod1(), 6)
# These failures match regular classes:
msg = r"super\(\): no arguments"
with self.assertRaisesRegex(RuntimeError, msg):
inst.stmethod2()
with self.assertRaisesRegex(RuntimeError, msg):
Child.stmethod2()

def test_super_without_params_wrapped(self):
for slots in (True, False):
with self.subTest(slots=slots):
@dataclass(slots=slots, frozen=True)
class Base:
@cache
def cached(self):
return 1
def regular(self):
return 2
@classmethod
@cache
def cached_cl(cls):
return 3
@classmethod
def regular_cl(cls):
return 4

@dataclass(slots=slots, frozen=True)
class Child(Base):
def cached1(self):
return super().cached() + 10
@cache
def cached2(self):
return super().cached() + 20
@cache
def cached3(self):
return super().regular() + 30
@classmethod
@cache
def cached_cl1(cls):
return super().cached_cl() + 40
@classmethod
def cached_cl2(cls):
return super().cached_cl() + 50
@classmethod
@cache
def cached_cl3(cls):
return super().regular_cl() + 60

inst = Child()
self.assertEqual(inst.cached(), 1)
self.assertEqual(inst.cached1(), 11)
self.assertEqual(inst.cached2(), 21)
self.assertEqual(inst.cached3(), 32)
self.assertEqual(inst.cached_cl(), 3)
self.assertEqual(inst.regular_cl(), 4)
self.assertEqual(inst.cached_cl1(), 43)
self.assertEqual(inst.cached_cl2(), 53)
self.assertEqual(inst.cached_cl3(), 64)


class TestDescriptors(unittest.TestCase):
def test_set_name(self):
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.