Skip to content

filterwarnings doesn't deal with ExceptionGroups of pytest.PytestUnraisableExceptionWarning #14096

@Redoubts

Description

@Redoubts

I have a test annotated with @pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") because I depend on a library I know will likely leak. However, I noticed this doesn't work if there are 2 leaks at the same time:

  |   File "<python venv>/site-packages/_pytest/unraisableexception.py", line 95, in cleanup
  |     collect_unraisable(config)
  |   File "<python venv>/site-packages/_pytest/unraisableexception.py", line 81, in collect_unraisable
  |     raise ExceptionGroup("multiple unraisable exception warnings", errors)
  | ExceptionGroup: multiple unraisable exception warnings (2 sub-exceptions)
  +-+---------------- 1 ----------------
    | Traceback (most recent call last):
    |   File "<python venv>/site-packages/_pytest/unraisableexception.py", line 33, in gc_collect_harder
    |     gc.collect()
    | ResourceWarning: unclosed <socket.socket fd=57, family=2, type=1, proto=6, laddr=('ip1', ...), raddr=('ip2', ...)>
    |
    | The above exception was the direct cause of the following exception:
    |
    | Traceback (most recent call last):
    |   File "<python venv>/site-packages/_pytest/unraisableexception.py", line 67, in collect_unraisable
    |     warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
    | pytest.PytestUnraisableExceptionWarning: Exception ignored in: <socket.socket fd=57, family=2, type=1, proto=6, laddr=('ip1', ...), raddr=('ip2', ...)>
Enable tracemalloc to get traceback where the object was allocated.
See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info.
    +---------------- 2 ----------------
    | Traceback (most recent call last):
    |   File "<python venv>lib/python3.11/asyncio/selector_events.py", line 868, in __del__
    |     _warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
    | ResourceWarning: unclosed transport <_SelectorSocketTransport fd=57>
    |
    | The above exception was the direct cause of the following exception:
    |
    | Traceback (most recent call last):
    |   File "<python venv>/site-packages/_pytest/unraisableexception.py", line 67, in collect_unraisable
    |     warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
    | pytest.PytestUnraisableExceptionWarning: Exception ignored in: <function _SelectorTransport.__del__ at 0x...>
Enable tracemalloc to get traceback where the object was allocated.
See https://docs.pytest.org/en/stable/how-to/capture-warnings.html#resource-warnings for more info.
    +------------------------------------

It would be nice if filterwarnings could deal with this condition easily -- like filter out the matching sub exceptions. Maybe it already can? I haven't seen much written on the internet about it...

pytest 8.4.2, python 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions