Hello, I'm fixing dependencies in my project as ri...
# general
r
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
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
Sorry, my message wasn't complete I just modified it šŸ˜• I do have
pytest-mock
in the config.
b
What does
./pants dependencies path/to/conftest.py
say?
r
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
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
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
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
conftest is loaded no matter what. So the call to mocker.patch will happen no matter what test
b
Does the patch call fail if the module isn't found?
r
yes and that makes the whole suite to fail
b
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
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
HOW AM I JUST LEARNING ABOUT THIS?!?!
šŸ˜‚ 1