Skip to content

capture warning when exception is raised (fix #9036) #11129

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

Merged
merged 1 commit into from
Jul 1, 2023

Conversation

Cheukting
Copy link
Contributor

@Cheukting Cheukting commented Jun 22, 2023

continue PR #10208 by @eltimen

Fixes #9036 and #11032 (same issue)

When a warning is being warned while an exception is raised, pytest.warns should still capture the warning if not matched.

CC @Zac-HD

@Cheukting
Copy link
Contributor Author

Seems this change will break the original deprecated_call behaviour, need to investigate more

@Cheukting
Copy link
Contributor Author

Cheukting commented Jun 23, 2023

This is the behaviour that changes:

If the block of the code being tested by deprecated_call() raises an exception, it must raise the exception undisturbed.

@Cheukting
Copy link
Contributor Author

From the previous discussion, it seems if we fix this missing warning bug we will have to change the previous behaviour: letting an exception through if there's a warning.

@Zac-HD I would need advice on this, if I got a green light to change this behaviour then I can remove the old test

@Cheukting
Copy link
Contributor Author

Ah not enough coverage... will look at that as well...

@Zac-HD
Copy link
Member

Zac-HD commented Jun 23, 2023

Yep, we'll need to change that behavior.

@Zac-HD
Copy link
Member

Zac-HD commented Jun 27, 2023

Just waiting for #10937 to avoid a merge conflict.

Copy link
Member

@Zac-HD Zac-HD left a comment

Choose a reason for hiding this comment

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

Thanks @Cheukting!

@Zac-HD Zac-HD merged commit ba60649 into pytest-dev:main Jul 1, 2023
jni pushed a commit to napari/npe2 that referenced this pull request Mar 8, 2024
This test is failing in CIs in #340.

I think it is because there is no warning raised (not sure why the
`UserWarning` is there). It used to pass because of this bug in pytest
pytest-dev/pytest#9036, but this was fixed in
pytest-dev/pytest#11129 and included in a recent
release, thus our CIs are failing.

Note that I have update the test to be more specific about the exception
raised (`PackageNotFoundError`) but it is not needed, subclass
exceptions are still accepted by pytest. Also added a message to be more
specific.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
imphil added a commit to imphil/cocotb that referenced this pull request Apr 17, 2024
Pytest 8 introduced new behavior for `pytest.warn()`, which isn't
compatible with our code in `test_unicode_handle_assignment_deprecated`.
Update the test to be skipped entirely for Icarus and GHDL instead of
testing for specific exceptions. (The test doesn't do anything on these
simulators anyways.)

See also pytest-dev/pytest#11129

Error message, as seen in CI:
```

     1.00ns INFO     cocotb.regression                  running test_unicode_handle_assignment_deprecated (2/195)
     1.00ns INFO     cocotb.regression                  test_unicode_handle_assignment_deprecated failed: errored with unexpected type
  Traceback (most recent call last):
    File "/home/runner/work/cocotb/cocotb/tests/test_cases/test_cocotb/test_deprecated.py", line 39, in test_unicode_handle_assignment_deprecated
      dut.stream_in_string.value = "Bad idea"
    File "/home/runner/work/cocotb/cocotb/.nox/dev_test_sim-sim-icarus-toplevel_lang-verilog-gpi_interface-vpi/lib/python3.8/site-packages/cocotb/handle.py", line 370, in __getattr__
      raise AttributeError(f"{self._name} contains no object named {name}")
  AttributeError: sample_module contains no object named stream_in_string

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
    File "/home/runner/work/cocotb/cocotb/tests/test_cases/test_cocotb/test_deprecated.py", line 39, in test_unicode_handle_assignment_deprecated
      dut.stream_in_string.value = "Bad idea"
    File "/home/runner/work/cocotb/cocotb/.nox/dev_test_sim-sim-icarus-toplevel_lang-verilog-gpi_interface-vpi/lib/python3.8/site-packages/_pytest/recwarn.py", line 322, in __exit__
      fail(
    File "/home/runner/work/cocotb/cocotb/.nox/dev_test_sim-sim-icarus-toplevel_lang-verilog-gpi_interface-vpi/lib/python3.8/site-packages/_pytest/outcomes.py", line 166, in fail
      raise Failed(msg=reason, pytrace=pytrace)
  Failed: DID NOT WARN. No warnings of type (<class 'DeprecationWarning'>,) were emitted.
    Emitted warnings: [].
```
imphil added a commit to cocotb/cocotb that referenced this pull request Apr 18, 2024
Pytest 8 introduced new behavior for `pytest.warn()`, which isn't
compatible with our code in `test_unicode_handle_assignment_deprecated`.
Update the test to be skipped entirely for Icarus and GHDL instead of
testing for specific exceptions. (The test doesn't do anything on these
simulators anyways.)

See also pytest-dev/pytest#11129

Error message, as seen in CI:
```

     1.00ns INFO     cocotb.regression                  running test_unicode_handle_assignment_deprecated (2/195)
     1.00ns INFO     cocotb.regression                  test_unicode_handle_assignment_deprecated failed: errored with unexpected type
  Traceback (most recent call last):
    File "/home/runner/work/cocotb/cocotb/tests/test_cases/test_cocotb/test_deprecated.py", line 39, in test_unicode_handle_assignment_deprecated
      dut.stream_in_string.value = "Bad idea"
    File "/home/runner/work/cocotb/cocotb/.nox/dev_test_sim-sim-icarus-toplevel_lang-verilog-gpi_interface-vpi/lib/python3.8/site-packages/cocotb/handle.py", line 370, in __getattr__
      raise AttributeError(f"{self._name} contains no object named {name}")
  AttributeError: sample_module contains no object named stream_in_string

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
    File "/home/runner/work/cocotb/cocotb/tests/test_cases/test_cocotb/test_deprecated.py", line 39, in test_unicode_handle_assignment_deprecated
      dut.stream_in_string.value = "Bad idea"
    File "/home/runner/work/cocotb/cocotb/.nox/dev_test_sim-sim-icarus-toplevel_lang-verilog-gpi_interface-vpi/lib/python3.8/site-packages/_pytest/recwarn.py", line 322, in __exit__
      fail(
    File "/home/runner/work/cocotb/cocotb/.nox/dev_test_sim-sim-icarus-toplevel_lang-verilog-gpi_interface-vpi/lib/python3.8/site-packages/_pytest/outcomes.py", line 166, in fail
      raise Failed(msg=reason, pytrace=pytrace)
  Failed: DID NOT WARN. No warnings of type (<class 'DeprecationWarning'>,) were emitted.
    Emitted warnings: [].
```
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

Successfully merging this pull request may close these issues.

pytest.warns and pytest.raises ordering issue
2 participants