https://pantsbuild.org/ logo
#general
Title
# general
r

ripe-cpu-85141

08/16/2022, 4:04 PM
Hello, I'm fixing dependencies in my project as right now, I'm pulling everything for everything and Pants seems to have some issues inferring some dependencies with pytest and pytest-mock. I'm having lines like
Copy code
@pytest.fixture(autouse=True)
def dont_do_this(mocker):
    mocker.patch("my.module.class.method")
and Pants doesn't seem to add
my/module.py
in the dependencies even with
Copy code
[python-infer]
string_imports = true
Am I missing something or should I manually add those deps in my BUILD file?
b

bitter-ability-32190

08/16/2022, 4:07 PM
Are you putting
pytest-mock
in your
pytest
extra_requirements
? Then it's in every test execution/sandbox: https://www.pantsbuild.org/v2.13/docs/python-test-goal#pytest-version-and-plugins
r

ripe-cpu-85141

08/16/2022, 4:09 PM
Sorry, my message wasn't complete I just modified it šŸ˜• I do have
pytest-mock
in the config.
b

bitter-ability-32190

08/16/2022, 4:09 PM
What does
./pants dependencies path/to/conftest.py
say?
r

ripe-cpu-85141

08/16/2022, 4:10 PM
I think that as the string contains a class and a method, this isn't just a "string_import" so I guess pants ignores it. This is just hypothesis, I haven't check the code
b

bitter-ability-32190

08/16/2022, 4:10 PM
Yup was just about to say that coke
Does it need to be depended on though? I see two cases: ā€¢ Test doens't depend on
my.module
therefore does it really matter if we patch it or not? They aren't using it. ā€¢ Test does, which therefore the patching does its thang
r

ripe-cpu-85141

08/16/2022, 4:13 PM
I have one case where the patching is on the conftest.py to disable a function that is costly and not needed on the tests. As it is an "auto used fixture", virtually all tests use it
I can add it in the BUILD file as a dependency of
conftest.py
but I think that is a pretty common use case so I was wondering if I am missing something.
b

bitter-ability-32190

08/16/2022, 4:27 PM
I don't think that answers my question. Either the code-under-test doesn't depend on the thing being patched, in which case we're OK. Or it does and so it gets patched. I'm not seeing the issue
r

ripe-cpu-85141

08/16/2022, 6:28 PM
conftest is loaded no matter what. So the call to mocker.patch will happen no matter what test
b

bitter-ability-32190

08/16/2022, 6:28 PM
Does the patch call fail if the module isn't found?
r

ripe-cpu-85141

08/16/2022, 6:29 PM
yes and that makes the whole suite to fail
b

bitter-ability-32190

08/16/2022, 6:29 PM
Oh yeah, that piece of info was missing šŸ™ˆ
You could wrap it in a
try/except
Or you could manually add the dependency to
conftest.py
in your
BUILD.pants
r

ripe-cpu-85141

08/16/2022, 6:30 PM
well, that's actually a nice idea
because right now that forces me to always import a bunch of stuff for nothing if the test doesn't use the thing I'm patching
āœ… 1
I'll try that. Thanks for the idea!
Copy code
with contextlib.suppress(ModuleNotFoundError):
   mocker.patch(...)
works like a charm šŸ™‚
b

bitter-ability-32190

08/16/2022, 7:57 PM
HOW AM I JUST LEARNING ABOUT THIS?!?!
šŸ˜‚ 1
5 Views