[pytest-dev] autospec for monkeypatch

Florian Schulze mail at florian-schulze.net
Mon Mar 22 04:44:41 EDT 2021


There is a difference between setattr and setitem. The setattr variant 
actually has the raising option, the setitem variant does not. I think 
it might be useful to add it for setitem as well. For backward 
compatibility it would need to default to False. Maybe there is a better 
API to achieve the same.

Regards,
Florian Schulze


On 21 Mar 2021, at 23:46, Brian Okken wrote:

> Yep. I think that’s it. I totally forgot about raising. I’ll try 
> it out.
>
> - Brian
>
>> On Mar 21, 2021, at 1:06 PM, Bruno Oliveira <nicoddemus at gmail.com> 
>> wrote:
>>
>> 
>>> there is no option to only accept existing keys.
>>
>> I'm not sure what they mean... there's a `raising` keyword argument, 
>> which defaults to True.
>>
>> []
>> Bruno
>>
>>> On Sun, Mar 21, 2021 at 2:41 PM Brian Okken 
>>> <variedthoughts at gmail.com> wrote:
>>> I think I want to get some extra help on this good question.
>>> - Brian
>>>
>>> - Brian
>>>
>>> Begin forwarded message:
>>>
>>>> From: Dimitri Blyumin via Fireside <contactform at fireside.fm>
>>>> Date: March 20, 2021 at 7:36:58 PM PDT
>>>> To: brian at pythontesting.net
>>>> Subject: [Test & Code :  Python Testing] Listener Feedback from 
>>>> Dimitri Blyumin
>>>> Reply-To: dimitri.blyumin at gmail.com
>>>>
>>>> Name: Dimitri Blyumin
>>>> Email: dimitri.blyumin at gmail.com
>>>> Twitter:
>>>> Website:
>>>>
>>>> Hi Brian,
>>>> I'm going through your excellent "Python Testing with pytest" book, 
>>>> and came across this potential scenario in monkeypatching: if we 
>>>> use setitem and misspell the key - it will create a new key:value 
>>>> and will leave the original one as is, no KeyError or warning.
>>>> Looking at the docs - there is no option to only accept existing 
>>>> keys.
>>>> I think this can be dangerous in some cases when the user expects a 
>>>> value to be patched, and it is not, e.g. change environment in the 
>>>> config dict from PRD to DEV.
>>>> Did you encounter this and is there a way to ensure that only 
>>>> existing keys are used in patching?
>>>>
>>>>
>>>> --
>>>> Fireside Labs, LLC (http://fireside.fm/)
>>> _______________________________________________
>>> pytest-dev mailing list
>>> pytest-dev at python.org
>>> https://mail.python.org/mailman/listinfo/pytest-dev


> _______________________________________________
> pytest-dev mailing list
> pytest-dev at python.org
> https://mail.python.org/mailman/listinfo/pytest-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/pytest-dev/attachments/20210322/c9e0c5b7/attachment-0001.html>


More information about the pytest-dev mailing list