Skip to content

gh-124176: create_autospec must not change how dataclass defaults are mocked #124724

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 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
26 changes: 26 additions & 0 deletions Lib/test/test_unittest/testmock/testhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,32 @@ class WithNonFields:
with self.assertRaisesRegex(AttributeError, msg):
mock.b

def test_dataclass_default_value_type_overrides_field_annotation(self):
# If field defines an actual default, we don't need to change
# the default type. Since this is how it used to work before #124176
@dataclass
class WithUnionAnnotation:
narrow_default: int | None = field(default=30)

for mock in [
create_autospec(WithUnionAnnotation, instance=True),
create_autospec(WithUnionAnnotation()),
]:
with self.subTest(mock=mock):
self.assertIs(mock.narrow_default.__class__, int)
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel both of these tests would be clearer if they actually showed what the thing ends up being, not just it's type, something like:

Suggested change
self.assertIs(mock.narrow_default.__class__, int)
self.assertIs(mock.narrow_default.__class__, int)
self.assertEqual(mock.narrow_default, 30)

Copy link
Contributor

Choose a reason for hiding this comment

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

Adding something along these lines would also distinguish the cases where we're expecting to mock a specific value from those where we're deriving an instance spec from a declared runtime type


def test_dataclass_field_with_no_default_value(self):
@dataclass
class WithUnionAnnotation:
no_default: int | None

mock = create_autospec(WithUnionAnnotation, instance=True)
self.assertIs(mock.no_default.__class__, type(int | None))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with these two, but this one is particularly confusing.
How can an object be both None and and int at the same time?

I know it's tangential to this PR, but I have to admit I'm not sure what the intention of instance=True|False is, the docs don't make it any clearer for me :-/

"You can use a class as the spec for an instance object by passing instance=True"

Does that mean:

  • create_autospec will produce a mock instance based on treating spec as a class
  • you can produce a mock class by passing in an instance as spec with instance=True
  • something else?

@sobolevn / @ncoghlan - do either of you know?

Copy link
Contributor

Choose a reason for hiding this comment

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

Some objects need real runtime resources to instantiate, so deriving an instance mock spec from an actual instance isn't practical. "instance = True" tells the module to do the best it can to mock an instance based on a class definition without needing to make a real instance.

3.14 is getting an enhancement to mock data class fields with no defaults based on their type annotations, but it has the same limitation in the absence of a default value as type checkers do: it can only narrow the field type for lazily populated fields down to a union, not to a concrete type.

This PR fixes a compatibility issue with the handling of data class fields that do have defaults (they get a mocked spec based on their default value instead of their declared union type).

I admit the handling of complex annotations isn't great (they are speced based on the annotation itself). Maybe we should treat the field annotation as an unconstrained mock (effectively Any) if the annotation doesn't correspond to a true runtime type?

Copy link
Contributor

Choose a reason for hiding this comment

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

A potentially more conservative approach:

  • create a separate feature request for an "annotation=..." option to build mock specs from arbitrary runtime type annotations

  • in the meantime, restrict this feature to the following cases:

    • concrete runtime types
    • type unions that include None (mocked as None)
    • annotations of the accepted types

Any other field would become an unconstrained mock. This would still mean the field existed though (even in strict mode), and hence users could set it to something more specific in their test code.

Copy link
Member Author

Choose a reason for hiding this comment

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

annotations of the accepted types

Sorry, I didn't get this part. Can you please provide an example?

Copy link
Contributor

Choose a reason for hiding this comment

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

For me, I'm looking to understand what this is:

self.assertIs(mock.no_default, ...what is this?...)

Copy link
Member Author

Choose a reason for hiding this comment

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

@cjw296 this currently would be:

>>> int | None
int | None
>>> type(int | None)
<class 'types.UnionType'>

This is a typing primitive. Docs: https://docs.python.org/3/library/types.html#types.UnionType

Copy link
Contributor

Choose a reason for hiding this comment

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

The instance attribute of the mock instance would be a type? That doesn't make any sense on the face of it, what am I missing?

Copy link
Contributor

Choose a reason for hiding this comment

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

@cjw296 You're not missing anything, that's a case I'm suggesting we should change to instead be mocked as None (type union that includes None as one of the permitted cases).

@sobolevn "Annotations of the accepted types" refers to things like Annotated[int, SomeAnnotation("such as a docstring")]. They're specified as being purely informational, so we can just ignore the wrapper.

Example:

@dataclass
class Example:
    a: Annotated[int, "example"]
>>> fields(Example)[0].type
typing.Annotated[int, 'example']
>>> fields(Example)[0].type.__origin__
<class 'int'>


mock = create_autospec(WithUnionAnnotation(1))
self.assertIs(mock.no_default.__class__, int)


class TestCallList(unittest.TestCase):

def test_args_list_contains_call_list(self):
Expand Down
12 changes: 9 additions & 3 deletions Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2758,13 +2758,19 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
f'[object={spec!r}]')
is_async_func = _is_async_func(spec)

entries = [(entry, _missing) for entry in dir(spec)]
base_entries = {entry: _missing for entry in dir(spec)}
if is_type and instance and is_dataclass(spec):
# Dataclass instance mocks created from a class may not have all of their fields
# prepopulated with default values. Create an initial set of attribute entries from
# the dataclass field annotations, but override them with the actual attribute types
# when fields have already been populated.
dataclass_fields = fields(spec)
entries.extend((f.name, f.type) for f in dataclass_fields)
entries = {f.name: f.type for f in dataclass_fields}
entries.update(base_entries)
_kwargs = {'spec': [f.name for f in dataclass_fields]}
else:
_kwargs = {'spec': spec}
entries = base_entries

if spec_set:
_kwargs = {'spec_set': spec}
Expand Down Expand Up @@ -2822,7 +2828,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
_name='()', _parent=mock,
wraps=wrapped)

for entry, original in entries:
for entry, original in entries.items():
if _is_magic(entry):
# MagicMock already does the useful magic methods for us
continue
Expand Down
Loading