-
-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
gh-124176: create_autospec
must not change how dataclass defaults are mocked
#124724
base: main
Are you sure you want to change the base?
Conversation
…ults are mocked
if is_type and instance and is_dataclass(spec): | ||
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} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How come we don't end up using UnionType
here from the int | None
annotation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I override entries
with default_entries
here: https://github.com/python/cpython/pull/124724/files#diff-347d0254250a1ab7ab8e31b405e2c35b74cd2838df4ee74f1b658a459eb91f1aR2765
This way field(default=0)
has the priority over the annotation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two questions from that:
-
What happens with a class such as this?
@dataclass class SampleClass: sample_attr: int | None
-
What does the mock machinery do with the
field
object it gets in the case you show above?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- In this case
spec
would beUnionType
, because we have no default - Mock does nothing special to
field
,dataclass
itself setsSampleClass.sample_attr = default
, and we just usegetattr
to get the existing default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to add a unit test (or more if necessary) showing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
I agree with the backport labels, since we want to backport the test case, even if the functional change doesn't apply cleanly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but I've suggested some naming clarifications for the new test cases.
@@ -1107,6 +1107,32 @@ class WithNonFields: | |||
with self.assertRaisesRegex(AttributeError, msg): | |||
mock.b | |||
|
|||
def test_dataclass_with_wider_default(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default value type here is actually narrower than the field annotation , so describing it as with_wider_default
ends up being confusing because "default" is being used in two different senses. (the new dataclass handling picks up the wider field annotation by default, so we want the narrower class attribute handling that reads the default value to override that)
Suggested names below:
def test_dataclass_with_wider_default(self): | |
def test_dataclass_default_value_type_overrides_field_annotation(self): |
class WithWiderDefault: | ||
narrow_default: int | None = field(default=30) | ||
|
||
for mock in [ | ||
create_autospec(WithWiderDefault, instance=True), | ||
create_autospec(WithWiderDefault()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
class WithWiderDefault: | |
narrow_default: int | None = field(default=30) | |
for mock in [ | |
create_autospec(WithWiderDefault, instance=True), | |
create_autospec(WithWiderDefault()), | |
class WithUnionAnnotation: | |
narrow_default: int | None = field(default=30) | |
for mock in [ | |
create_autospec(WithUnionAnnotation, instance=True), | |
create_autospec(WithUnionAnnotation()), |
def test_dataclass_with_no_default(self): | ||
@dataclass | ||
class WithWiderDefault: | ||
narrow_default: int | None | ||
|
||
mock = create_autospec(WithWiderDefault, instance=True) | ||
self.assertIs(mock.narrow_default.__class__, type(int | None)) | ||
|
||
mock = create_autospec(WithWiderDefault(1)) | ||
self.assertIs(mock.narrow_default.__class__, int) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_dataclass_with_no_default(self): | |
@dataclass | |
class WithWiderDefault: | |
narrow_default: int | None | |
mock = create_autospec(WithWiderDefault, instance=True) | |
self.assertIs(mock.narrow_default.__class__, type(int | None)) | |
mock = create_autospec(WithWiderDefault(1)) | |
self.assertIs(mock.narrow_default.__class__, int) | |
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)) | |
mock = create_autospec(WithUnionAnnotation(1)) | |
self.assertIs(mock.no_default.__class__, int) |
@@ -2758,13 +2758,15 @@ 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_fields = fields(spec) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is obscure enough that I think it's worth adding a comment explaining why the code works as it does:
dataclass_fields = fields(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) |
See #124176 (comment)
@ncoghlan found that my change introduced a behavior change (or even a regression) to dataclasses with defaults.
Before my change
create_autospec
would use the default's type for_spec_class
.After my change it would use the annotation's type.
Which is not always correct. For example,
int | str
would produce a__class__
oftypes.UnionType
People might rely on that, so no need to break this.