#πŸ”’ Type hinting ExceptionGroup pytest.raises

4 messages Β· Page 1 of 1 (latest)

hasty garden
#

Hi everyone,

While writing some tests, I encountered an issue with Pyright, which is reporting partially unknown types, and I'm not sure how to fix it.

Here is my test function:

    with pytest.raises(ExceptionGroup) as exc_info:
        tablerone_partial(
            padding_horizontal_no_border=paddings[0],
            padding_vertical_no_border=paddings[1],
            padding_horizontal_with_border=paddings[2],
            padding_vertical_with_border=paddings[3],
        )
    assert all(isinstance(exc, InvalidPaddingValueError) for exc in exc_info.value.exceptions)

In this case, I'm trying to ensure that tablerone_partial raises an ExceptionGroup that only contains InvalidPaddingValueError. The ExceptionGroup holds a more generic exception type, and InvalidPaddingValueError inherits from that generic exception type.

Here are the errors shown by Pyright:

 test_args.py:27:43 - error: Type of "exc_info" is partially unknown
    Type of "exc_info" is "ExceptionInfo[ExceptionGroup[Unknown]]" (reportUnknownVariableType)
  test_args.py:34:62 - error: Type of "exc" is partially unknown
    Type of "exc" is "Unknown | ExceptionGroup[Unknown]" (reportUnknownVariableType)
  test_args.py:34:69 - error: Type of "value" is partially unknown
    Type of "value" is "ExceptionGroup[Unknown]" (reportUnknownMemberType)
  test_args.py:34:69 - error: Type of "exceptions" is partially unknown
    Type of "exceptions" is "tuple[Unknown | ExceptionGroup[Unknown], ...]" (reportUnknownMemberType)

I've tried adding type hints to the ExceptionGroup raised by the tablerone_partial function, but it didn't help. (Mypy doesn't show any errors.)

How can I hint the exc_info to make Pyright recognize the type? Or should I be approaching this differently?

Thanks in advance for any help!

ashen cypressBOT
#

@hasty garden

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

ashen cypressBOT
#

@hasty garden

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.