Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
bd73cda81b4e00bfa9385dd1fe1271f355eFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1107,6 +1107,21 @@ class WithNonFields: | ||||||||
| with self.assertRaisesRegex(AttributeError, msg): | ||||||||
| mock.b | ||||||||
| def test_dataclass_with_wider_default(self): | ||||||||
sobolevn marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||||
| # 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 WithWiderDefault: | ||||||||
| narrow_default: int | None = field(default=30) | ||||||||
| for mock in [ | ||||||||
| create_autospec(WithWiderDefault, instance=True), | ||||||||
| create_autospec(WithWiderDefault()), | ||||||||
sobolevn marked this conversation as resolved. OutdatedShow resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||||||||
| ]: | ||||||||
| with self.subTest(mock=mock): | ||||||||
| self.assertIs(mock.narrow_default.__class__, int) | ||||||||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||
| class TestCallList(unittest.TestCase): | ||||||||
| def test_args_list_contains_call_list(self): | ||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| base_entries ={entry: _missing for entry in dir(spec)} | ||
| if is_type and instance and is_dataclass(spec): | ||
| dataclass_fields = fields(spec) | ||
sobolevn marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| entries = {f.name: f.type for f in dataclass_fields} | ||
ncoghlan marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| 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} | ||
| @@ -2822,7 +2824,7 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None, | ||
| _name='()', _parent=mock, | ||
| wraps=wrapped) | ||
| for entry, original in entries.items(): | ||
| if _is_magic(entry): | ||
| # MagicMock already does the useful magic methods for us | ||
| continue | ||