Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pants test using a different module version than it's assigned resolve #20945

Closed
enriquemaffezzini opened this issue May 21, 2024 · 5 comments
Closed

Comments

@enriquemaffezzini
Copy link

enriquemaffezzini commented May 21, 2024

I have a test that depends on the packaging module. The resolve python_rasa has packaging<=21.3 in its requirements 3rdparty/requirements/python_rasa.txt and its lock-file "version": "20.9" for packaging.

The BUILD file in project's root folder overrides the resolve to python_rasa:

projects/chatbot/BUILD

__defaults__(
    {
        (python_sources, python_source, python_tests, pex_binaries, pex_binary): dict(
            resolve="python_rasa"
        )
    }
)
resource(name="pyproject", source="pyproject.toml")

As a redundancy, I also pointed out the resolve for the test despite the BUILD in the project's root folder should be enough:

projects/chatbot/tests/BUILD

python_tests(
    name="tests",
    resolve="python_rasa"
)

Finally, the current test file is currently just printing the packagingversion as pants run runs just fine and pants test fails due to a packaging version problem on the real test.

projects/chatbot/tests/test_packaging.py

import packaging
print("Packaging version:", packaging.__version__)
from rasa.shared.nlu.training_data.message import Message
def test_placebo():
    assert 1 == 1

pants test projects/chatbot/tests/:: prints Packaging version: 23.0 failing with ImportError: cannot import name 'LegacyVersion' from 'packaging.version'; class that is deprecated after packaging 21.3. The logs show that pants test builds 3rdparty/lockfiles/python_rasa.lock, which are the correct ones with packaging version 20.9.

Curiously enough, pants run projects/chatbot/tests/test_packaging.py prints Packaging version: 20.9 passing the test.

pants dependencies --transitive projects/chatbot/tests/test_packaging.pywould return:

3rdparty/lockfiles/python_rasa.lock:_python_rasa_lockfile
3rdparty/requirements/python_rasa.txt:../requirements_rasa
3rdparty:requirements_rasa#es-dep-news-trf
3rdparty:requirements_rasa#packaging
3rdparty:requirements_rasa#rasa
3rdparty:requirements_rasa#spacy

Any idea why is pants test is building packaging 23.0 instead of 20.9?

@benjyw
Copy link
Sponsor Contributor

benjyw commented May 22, 2024

pytest itself depends on packaging, so I suspect that is the issue. Are you installing pytest from a custom resolve?

If so, you can set the packaging version in that resolve's lockfile inputs.

One convenient way to do this is to add pytest to your python_rasa resolve (it may already be there because your tests probably import from it?) and then set:

[pytest]
install_from_resolve = "python_rasa"  
# Enumerate the requirements that pytest actually needs to run itself.
# If you don't it will use the entire lockfile, which means every lockfile change will invalidate all tests.
requirements =[...]  

@enriquemaffezzini
Copy link
Author

You nailed it! Using install_from_resolve = "python_rasa" in the toml file does the trick.

Can you think of any way in which I can force this project tests to use the project's resolve pytest or to override the pytest resolve for such tests? I need to keep the rest of the projects with the same pytest setting. Specifying the resolve in the test folder build file like this is not having the expected effect:

projects/chatbot/tests/BUILD

python_tests(
    name="tests",
    resolve="python_rasa"
)

@benjyw
Copy link
Sponsor Contributor

benjyw commented May 22, 2024

Unfortunately that is not currently possible - the resolve that pytest runs from is set globally. However this is a much-requested feature in the New Python Backend discussions. Feel free to chime in there.

For now, probably best is to generate a custom pytest lockfile that pins packaging.

This is an unfortunate problem with pytest - it loads the code under test into the same namespace as its own code. Really, it should "shade" itself and its dependencies.

@enriquemaffezzini
Copy link
Author

Thanks a lot for your guidance was very valuable. We got everything working following your advice

@benjyw
Copy link
Sponsor Contributor

benjyw commented Jun 5, 2024

Glad to hear it! Hopefully in the future this will be handled more smoothly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants