You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Path.cwd() different when used as default argument #704
I'm not new to PyFakeFS but this took me a while to find out the cause of my problem. But I am still not sure if this is a bug or just a misunderstanding on my site.
When PyFakeFS is activated the return value of pathlib.Path.cwd() should be /.
But when I use pathlib.Path.cwd() as a default argument in a function it returns the working directory of the real file system.
Thanks - this is indeed a shortcoming of the patching of default arguments. Apart from the fact that it is off by default (which you already found), it only checks the arguments for file system functions, not for file system classes (in this case pathlib.Path). This case is not mentioned in the documentation, so it is not obvious... It just never came up so far.
The only way to prevent this currently is to reload the module in question using modules_to_reload (provided that the module is not in the test module itself as in your example) instead of using patch_default_args=True.
I may check if it is possible to handle this case additionally if patch_default_args is set. This check is already slow, but as it is not used by default, making it slower yet should not matter that much. This may take some time, though...
I am sorry to bother you again. But maybe I created another edge case. The example code I used here worked with your solution. But porting that into my real productive code doesn't help. I assume this is because of the way I structure that package with it's sub-packages and how I import them.
But I don't get a solution.
Again the problem is that the pathlib.Path.cwd() used as defaut argument returns the CWD from real filesystem and not the fake one.
Please see the original repo buhtzology. The problem is located in the logging branch. Keep that in mind when cloning.
The code to test is located in the file src/buhtzology/_buhtzology.py (see the underline _). The functions in that file are imported in src/buhtzology/__init__.py.
Ok, I had a look, and the reason why it does not work is a bit obscure: it's a problem of the reload order. As buhtzology loads _buhtzology in __init__, that one should be reloaded first, so using modules_to_reload=[buhtzology._buhtzology, buhtzology] (note the order) will fix the problem.
From a users perspective reading the documentation I would suggest to create a separate section with such edge cases. Maybe you can link to that edge cases in the regular documentation about modules_to_reload and patch_default_args.
After thinking about this I realized that there is really no way to fix this without reloading the code.
In principle, we could try to reload only the code used for the default args, which may be possible using some ast magic, but at the moment I don't want to go down that rabbit hole. I'll leave this as a limitation (now a bit better documented) and close this issue.
Activity
buhtz commentedon Aug 13, 2022
Mhm... I thought I found my mistake. But using
patch_default_argsdoesn't help either.The output is the same.
mrbean-bremen commentedon Aug 13, 2022
Thanks - this is indeed a shortcoming of the patching of default arguments. Apart from the fact that it is off by default (which you already found), it only checks the arguments for file system functions, not for file system classes (in this case
pathlib.Path). This case is not mentioned in the documentation, so it is not obvious... It just never came up so far.The only way to prevent this currently is to reload the module in question using
modules_to_reload(provided that the module is not in the test module itself as in your example) instead of usingpatch_default_args=True.I may check if it is possible to handle this case additionally if
patch_default_argsis set. This check is already slow, but as it is not used by default, making it slower yet should not matter that much. This may take some time, though...buhtz commentedon Aug 13, 2022
Thanks a lot. From my side you can close that Issue.
Just for documentation to others here the code that works. I separated it into two files.
Here is
x.py:This is
test_x.pymrbean-bremen commentedon Aug 13, 2022
Thanks - I'll leave it open for a while to investigate if this can be fixed without the need to reload.
buhtz commentedon Aug 13, 2022
I am sorry to bother you again. But maybe I created another edge case. The example code I used here worked with your solution. But porting that into my real productive code doesn't help. I assume this is because of the way I structure that package with it's sub-packages and how I import them.
But I don't get a solution.
Again the problem is that the
pathlib.Path.cwd()used as defaut argument returns the CWD from real filesystem and not the fake one.Please see the original repo
buhtzology. The problem is located in theloggingbranch. Keep that in mind when cloning.The relevant parts of the tree looking like this
The code to test is located in the file
src/buhtzology/_buhtzology.py(see the underline_). The functions in that file are imported insrc/buhtzology/__init__.py.The function to test is
https://codeberg.org/buhtz/buhtzology/src/commit/7be3a19dd3ad08d99f50eb6046c9d572a2d342ca/src/buhtzology/_buhtzology.py#L79
The test is here
https://codeberg.org/buhtz/buhtzology/src/commit/7be3a19dd3ad08d99f50eb6046c9d572a2d342ca/tests/test_buhtzology.py#L239
You have to install the package in Developer Mode via
pip --editable .to make the tests work.I invoke the test via
python3 -m unittest tests.test_buhtzology.Logging.Would be glad if you could take a look into it. Maybe I just have to modify the
modules_to_reloadpart?mrbean-bremen commentedon Aug 13, 2022
Ok, I will probably have a look at it tomorrow.
mrbean-bremen commentedon Aug 14, 2022
Ok, I had a look, and the reason why it does not work is a bit obscure: it's a problem of the reload order. As
buhtzologyloads_buhtzologyin__init__, that one should be reloaded first, so usingmodules_to_reload=[buhtzology._buhtzology, buhtzology](note the order) will fix the problem.buhtz commentedon Aug 14, 2022
Bähm! Thats it!
Thank you so much.
From a users perspective reading the documentation I would suggest to create a separate section with such edge cases. Maybe you can link to that edge cases in the regular documentation about
modules_to_reloadandpatch_default_args.mrbean-bremen commentedon Aug 14, 2022
Yes, that's a good idea. In my experience, most people don't read the documentation, but there are at least some for whom that may be helpful 😀.
Add notes to usage documentation
Add notes to usage documentation - add information for patch_default_…
mrbean-bremen commentedon Aug 14, 2022
After thinking about this I realized that there is really no way to fix this without reloading the code.
In principle, we could try to reload only the code used for the default args, which may be possible using some ast magic, but at the moment I don't want to go down that rabbit hole. I'll leave this as a limitation (now a bit better documented) and close this issue.