Skip to content

Commit

Permalink
Address review
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Sep 26, 2024
1 parent 99948cf commit 5dcf523
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
10 changes: 6 additions & 4 deletions Lib/test/test_unittest/testmock/testhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
Mock, ANY, _CallList, patch, PropertyMock, _callable
)

from dataclasses import dataclass, field, InitVar
from datetime import datetime
from functools import partial
from typing import ClassVar

class SomeClass(object):
def one(self, a, b): pass
Expand Down Expand Up @@ -1034,10 +1036,7 @@ def f(a): pass
self.assertEqual(mock.mock_calls, [])
self.assertEqual(rv.mock_calls, [])

def test_dataclass(self):
from dataclasses import dataclass, field, InitVar
from typing import ClassVar

def test_dataclass_post_init(self):
@dataclass
class WithPostInit:
a: int = field(init=False)
Expand All @@ -1062,6 +1061,7 @@ def __post_init__(self):
with self.assertRaisesRegex(AttributeError, msg):
mock.b

def test_dataclass_default(self):
@dataclass
class WithDefault:
a: int
Expand All @@ -1075,6 +1075,7 @@ class WithDefault:
self.assertIsInstance(mock.a, int)
self.assertIsInstance(mock.b, int)

def test_dataclass_with_method(self):
@dataclass
class WithMethod:
a: int
Expand All @@ -1089,6 +1090,7 @@ def b(self) -> int:
self.assertIsInstance(mock.a, int)
mock.b.assert_not_called()

def test_dataclass_with_non_fields(self):
@dataclass
class WithNonFields:
a: ClassVar[int]
Expand Down
9 changes: 4 additions & 5 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import pkgutil
from inspect import iscoroutinefunction
import threading
from dataclasses import fields, is_dataclass
from types import CodeType, ModuleType, MethodType
from unittest.util import safe_repr
from functools import wraps, partial
Expand Down Expand Up @@ -2755,12 +2756,10 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
f'[object={spec!r}]')
is_async_func = _is_async_func(spec)

placeholder = object()
entries = [(entry, placeholder) for entry in dir(spec)]
entries = [(entry, _missing) for entry in dir(spec)]
# Not using `is_dataclass` to avoid an import of dataclasses module
# for types that don't need that.
if is_type and instance and hasattr(spec, '__dataclass_fields__'):
from dataclasses import fields
if is_type and instance and is_dataclass(spec):
dataclass_fields = fields(spec)
entries.extend((f.name, f.type) for f in dataclass_fields)
_kwargs = {'spec': [f.name for f in dataclass_fields]}
Expand Down Expand Up @@ -2837,7 +2836,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
# AttributeError on being fetched?
# we could be resilient against it, or catch and propagate the
# exception when the attribute is fetched from the mock
if original is placeholder:
if original is _missing:
try:
original = getattr(spec, entry)
except AttributeError:
Expand Down

0 comments on commit 5dcf523

Please sign in to comment.