Skip to content

Prevent unimported warnings @ pytest-xdist workers #695

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

webknjaz
Copy link
Member

@webknjaz webknjaz commented Jun 6, 2025

Ref #693

This fixes #693 (comment) for me.
Actually, locally, I can also drop these private attribute assignments from DistMaster and it works with just disabling a single warning on the worker.
However, when I tried doing this in the project (and not in site-packages of where I'm hitting the problem), it breaks tests/test_pytest_cov.py::test_xdist_no_data_collected so I didn't include those changes.

I was hoping that you @ionelmc could take a look and maybe come up with some explanation of why this is happening.

Coverage is being collected and reported, but at the time coveragepy is looking into sys.modules, the module isn't there. I think xdist makes it unpredictable whether the coverage object in master or in the worker would hit this code path. It probably makes sense to disable this warning when under xdist for workers too. It's unreliable at best in such a setup.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR suppresses spurious “unimported source” warnings when collecting coverage under pytest-xdist workers.

  • Disables the private _warn_unimported_source flag on the coverage object in DistMaster.start().
  • Ensures coverage collection proceeds without warnings in parallel test sessions.
Comments suppressed due to low confidence (1)

src/pytest_cov/engine.py:453

  • Add a test to verify that 'unimported source' warnings are suppressed under xdist worker mode to guard against regressions.
self.cov._warn_unimported_source = False

@@ -450,6 +450,7 @@ def start(self):
data_suffix=_data_suffix(f'w{self.nodeid}'),
config_file=self.cov_config,
)
self.cov._warn_unimported_source = False
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably include all those other hacks, after all:

Suggested change
self.cov._warn_unimported_source = False
self.cov._warn_no_data = False
self.cov._warn_unimported_source = False
self.cov._warn_preimported_source = False

Copy link
Member

@ionelmc ionelmc Jun 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, but without a test triggering these warnings you will never really know what effect this will have.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I only know that only one has a visible effect of suppressing a false negative in ansible-pylibssh. I've been trying to think of a reliable test but no luck so far.

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

Successfully merging this pull request may close these issues.

coverage collection fails when using pytest-xdist and pytest 8.4.0
2 participants