-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
Improve UX for mocking dataclasses that are problematic to instantiate #124176
Comments
Great idea! @ncoghlan do you want to work on this? Or can I take this over? :) |
I already worked around it for my use case (leaving I think the basic cases where the field types are just a simple type will be straightforward (use Checking the result of
|
This will land in 3.14! 🎉 |
@sobolevn , it belatedly occurred to me to ask: should this feature do something special for
Annotated types make the underlying type available in
I also realised we want to avoid a behaviour change for dataclasses that do define a default value of a specific type within a more general type category:
Since the initial implementation processes every While I think that would be a reasonable design if this native dataclass support had been added when dataclasses first entered the standard library, at this point we don't want to change the mocked types of attributes that were already being picked up by the previous For the attributes that do appear in |
I agree with the second part 100%, this is a regression in my new change. PR is on its way. |
…ults are mocked
I agree, the first part would be a new feature request. |
Feature or enhancement
Proposal:
Passing
instance=True
tocreate_autospec
misses fields without default values, even when the given spec is adataclass
object:This is despite dataclass definitions making their field information readily available for introspection on the class object (without requiring instantiation):
A similar problem occurs if the dataclass uses
__post_init__
to set attributes that are not otherwise set:While for most dataclasses it is straightforward to instantiate a specific instance and derive the mock autospec from that, this may not be desirable (or even feasible) if the dataclass requires references to real external resources to create a real instance.
There are various potential workarounds available for this functional gap, but they're all relatively clumsy, and come with their own problems (like not being able to use
spec_set=True
if the missing fields are added manually, or not handling defined methods properly if setting an explicit list of fields instead of usingautospec
, or still not adding the fields only defined in__post_init__
if instantiating a class mock).By contrast, if
create_autospec
were to be made explicitly aware of data classes, it could do a pass overdataclasses.fields(spec)
and use the type information to fill in any missing fields that don't have class level default values set.Has this already been discussed elsewhere?
This is a minor feature, which does not need previous discussion elsewhere
Links to previous discussion of this feature:
Previously filed here, but closed on the basis of
create_autospec
covering the use case: #80761This is only true if the dataclass can be readily instantiated, hence this feature request.
There is also some previous discussion (and assorted attempted workarounds with various flaws) on this Stack Overflow question: https://stackoverflow.com/questions/51640505/how-to-use-spec-when-mocking-data-classes-in-python
Linked PRs
create_autospec
#124429create_autospec
must not change how dataclass defaults are mocked #124724The text was updated successfully, but these errors were encountered: