This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author andrei.avk
Recipients andrei.avk
Date 2021-09-16.02:13:09
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1631758390.34.0.18935168801.issue45215@roundup.psfhosted.org>
In-reply-to
Content
Currently using *name* and *parent* args in Mock and MagicMock is problematic in a few ways:

*name*

 - any value can be passed silently but at a later time, any value except for an str will cause
   an exception when repr() or str() on the mock object is done.

 - a string name can be passed but will not be equal to the respective attr value:
   Mock(name='foo').name != 'foo', as users would expect. (this should be documented).

*parent*

 - any value can be passed but, similarly to *name*, will cause an exception when str() or
   repr() is done on the object.

 - this arg is not documented so users will expect it to be set as an attr, but instead the
   attribute is going to be a Mock instance. [1]


I propose to fix these issues by:

 - checking the types that are passed in and display a DeprecationWarning if types are wrong.
 (note that this check should be fast because at first value can be compared to None default,
 which is what it's going to be in vast majority of cases, and isinstance() check is only done
 after that.) (in 3.11)

 - in 3.12, convert warnings into TypeError.

 - Document that *name* attribute will be a Mock instance.

 - Document that *name* argument needs to be a string.

 - Document *parent* argument.

 - In the docs for the two args, point to `configure_mock()` method for setting them to
   arbitrary values.

(Note that other args for Mock() have more specialized names and are much less likely to cause
similar issues.)

[1] https://bugs.python.org/issue39222
History
Date User Action Args
2021-09-16 02:13:10andrei.avksetrecipients: + andrei.avk
2021-09-16 02:13:10andrei.avksetmessageid: <1631758390.34.0.18935168801.issue45215@roundup.psfhosted.org>
2021-09-16 02:13:10andrei.avklinkissue45215 messages
2021-09-16 02:13:09andrei.avkcreate