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

@typeguard on a test breaks pytest assertion rewriting #477

Open
2 tasks done
adamtheturtle opened this issue Aug 22, 2024 · 5 comments
Open
2 tasks done

@typeguard on a test breaks pytest assertion rewriting #477

adamtheturtle opened this issue Aug 22, 2024 · 5 comments
Labels

Comments

@adamtheturtle
Copy link

Things to check first

  • I have searched the existing issues and didn't find my bug already reported there

  • I have checked that my bug is still present in the latest release

Typeguard version

4.3.0

Python version

3.12

What happened?

Install pytest and typeguard.
Run the following script:

# test_example.py

import pytest

def test_example() -> None:
	result = 1
	assert result == 2

pytest.main()

See output including:

    def test_example() -> None:
    	result = 1
>   	assert result == 2
E    assert 1 == 2

Now run:

# test_example.py

import pytest
from typeguard import typechecked

@typechecked
def test_example() -> None:
	result = 1
	assert result == 2

pytest.main()

See:

    def test_example() -> None:
    	result = 1
>   	assert result == 2
E    AssertionError

How can we reproduce the bug?

Run the code examples from the previous section.

@agronholm
Copy link
Owner

I don't think there's much I can do about this, apart from returning @typechecked to wrapping the underlying function again.

@adamtheturtle
Copy link
Author

Thank you @agronholm . I am not familiar with typeguard internals, so I don't know what encouraged the change away from that.

In case it is useful, I'll point you to https://docs.pytest.org/en/stable/how-to/writing_plugins.html#assertion-rewriting and pytest.register_assert_rewrite.

@agronholm
Copy link
Owner

Thank you @agronholm . I am not familiar with typeguard internals, so I don't know what encouraged the change away from that.

  1. It creates an additional stack frame for every call
  2. Without AST rewriting, I cannot inject code to check assignments to annotated local variables

@agronholm
Copy link
Owner

In case it is useful, I'll point you to https://docs.pytest.org/en/stable/how-to/writing_plugins.html#assertion-rewriting and pytest.register_assert_rewrite.

It's not useful, sorry. This API is for enabling assertion rewrite outside of the collected pytest modules.

@adamtheturtle
Copy link
Author

Thank you again for the quick and thoughtful responses.

What do you think of making those trade-offs, but potentially only when PYTEST_CURRENT_TEST is set (https://docs.pytest.org/en/latest/example/simple.html#pytest-current-test-environment-variable)?

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

No branches or pull requests

2 participants