I can only use positional arguments with a function that uses @patch. Running being_patched(True) works. Running being_patched(arg=True) does not.
The general principle is positional args cannot come after keyword args. I just find the the interaction with @patch to be unfortunate.
Is there an alternative?
I am running Python 3.11.
import traceback
from unittest.mock import patch
import os.path
@patch('os.path.isfile')
def being_patched(arg, isfile_mock):
print(f"Inside being_patched. arg={arg} isfile_mock={isfile_mock}")
if __name__ == '__main__':
being_patched(True) # <== Works
print("\n----------\n")
try:
being_patched(arg=True) # <== Fails
except TypeError:
traceback.print_exc()
Produces this output:
Inside being_patched. arg=True isfile_mock=<MagicMock name='isfile' id='3181488491344'>
----------
Traceback (most recent call last):
File "C:\Users\Mike Ulm\PycharmProjects\Exercises\USDataMap\patch_after_kwargs.py", line 13, in <module>
being_patched(arg=True)
File "C:\PythonFiles\Python311\Lib\unittest\mock.py", line 1375, in patched
return func(*newargs, **newkeywargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
**TypeError: being_patched() got multiple values for argument 'arg'**
Running python 3.11