Skip to content

Disable assertion rewriting external modules #13421

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 16 commits into
base: main
Choose a base branch
from

Conversation

Tusenka
Copy link

@Tusenka Tusenka commented May 13, 2025

Disable assertion rewriting of external modules. Closes 13403.

@Tusenka
Copy link
Author

Tusenka commented May 13, 2025

Need to squash commits - OK to close, I'll reopen a new one with one commit

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label May 13, 2025
Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

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

thank you for getting this started

i believe we need to put the path handling into assertion state so it can correctly pass from the configuration and include the invocation dir/rootdir in a more safe manner than the current heusterics

@Tusenka Tusenka force-pushed the disable_assertion_rewriting_external_modules branch from 23dcf0c to 54f46d7 Compare May 16, 2025 05:01
@nicoddemus
Copy link
Member

nicoddemus commented May 16, 2025

I skimmed through the issue (I'm short on time so I cannot do a more through research), but looking at the code is not immediately clear to me so thought I would ask:

Note that we want to rewrite asserts for files belonging to a pytest plugin, even if they are not test_*.py files. How does this patch relate to that? This is an important behavior that should be kept.

@Tusenka Tusenka force-pushed the disable_assertion_rewriting_external_modules branch from 6a9b406 to 1592f85 Compare May 16, 2025 16:10
@Tusenka
Copy link
Author

Tusenka commented May 16, 2025

I skimmed through the issue (I'm short on time so I cannot do a more through research), but looking at the code is not immediately clear to me so thought I would ask:

Note that we want to rewrite asserts for files belonging to a pytest plugin, even if they are not test_*.py files. How does this patch relate to that? This is an important behavior that should be kept.

At present time the fix is applied only for path which applies test_*py. The plugins are processed separately. I'll add some tests against that important part.

@Tusenka
Copy link
Author

Tusenka commented May 18, 2025

I skimmed through the issue (I'm short on time so I cannot do a more through research), but looking at the code is not immediately clear to me so thought I would ask:
Note that we want to rewrite asserts for files belonging to a pytest plugin, even if they are not test_*.py files. How does this patch relate to that? This is an important behavior that should be kept.

At present time the fix is applied only for path which applies test_*py. The plugins are processed separately. I'll add some tests against that important part.

Added some tests for plugin rewriting, it works now

@Tusenka Tusenka force-pushed the disable_assertion_rewriting_external_modules branch from 16f3fc9 to ba4263d Compare May 19, 2025 04:13
with mock.patch.object(hook, "fnpats", ["*.py"]):
assert hook.find_spec("file") is None

def test_assert_rewrite_correct_for_conftfest(
Copy link
Author

Choose a reason for hiding this comment

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

mamy name it in another way - test_assert_rewrite_for_conftfest

@Tusenka Tusenka force-pushed the disable_assertion_rewriting_external_modules branch from 4e1f8b9 to db7dcd4 Compare May 22, 2025 04:12
@@ -108,6 +109,19 @@ def __init__(self, config: Config, mode) -> None:
self.trace = config.trace.root.get("assertion")
self.hook: rewrite.AssertionRewritingHook | None = None

@property
Copy link
Member

Choose a reason for hiding this comment

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

this must mot use getwd instead use the invoction params

Copy link
Author

@Tusenka Tusenka May 24, 2025

Choose a reason for hiding this comment

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

then we cannot change it on runtime as far as invocation param for pytester changes rootpath

Copy link
Member

Choose a reason for hiding this comment

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

sorry for the type - this code shoud use the rootdir or the invocationdir from the invocation params of the config
see https://docs.pytest.org/en/stable/reference/reference.html#pytest.Config.invocation_params as well as the config rootdir

Copy link
Author

@Tusenka Tusenka May 25, 2025

Choose a reason for hiding this comment

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

My error, I mean as far as Invocation Param is frozen - it can't be changed on runtime. Pytester starts after the config has been loaded. So to change the rootpath for pytester I need to rewrite the rootpath in someway. I could mock all invocation params or I could use getcwd alongside with config rootdir for the testing purpose.

Copy link
Member

Choose a reason for hiding this comment

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

The complete object can be replaced with a changed one

Copy link
Author

Choose a reason for hiding this comment

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

ok. would be done)

@Tusenka Tusenka force-pushed the disable_assertion_rewriting_external_modules branch from 939ca74 to 96f6846 Compare May 24, 2025 11:18
@Tusenka Tusenka force-pushed the disable_assertion_rewriting_external_modules branch from 3b36041 to 23e0d70 Compare May 27, 2025 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided (automation) changelog entry is part of PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disable assertion rewriting of external modules for python_files = *.py
3 participants