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

Too tight a type constraint on ExceptionInfo.errisinstance #12667

Closed
2 of 4 tasks
gmc444-b opened this issue Jul 29, 2024 · 3 comments · Fixed by #12700
Closed
2 of 4 tasks

Too tight a type constraint on ExceptionInfo.errisinstance #12667

gmc444-b opened this issue Jul 29, 2024 · 3 comments · Fixed by #12700
Labels
topic: typing type-annotation issue type: regression indicates a problem that was introduced in a release which was working previously

Comments

@gmc444-b
Copy link

  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions (pytest versions; error should be OS-independent)
  • minimal example if possible

With pytest 8.2.2 the declaration of ExceptionInfo.errisinstance was

     def errisinstance(
        self, exc: Union[Type[BaseException], Tuple[Type[BaseException], ...]]
    ) -> bool:

With 8.3.1, this has changed, first by introducing a type spec, and then using that typespec for the function signature

EXCEPTION_OR_MORE = Union[Type[Exception], Tuple[Type[Exception], ...]]
    ...
    def errisinstance(self, exc: EXCEPTION_OR_MORE) -> bool:

Version 8.2.2 would pass a mypy check with the following reproducer. Version 8.3.1 fails (I didn't test with 8.3.0 but I'd expect that to also fail). I tested this with mypy versions 1.10.1 and 1.11.0, but I'd expect it to fail for pretty much any supported version of mypy.

import sys
import pytest


def test_iserrinstance() -> None:
    def bad() -> None:
        sys.exit()

    with pytest.raises(BaseException) as exc:
        bad()
    assert exc.errisinstance(SystemExit)

# Error output:
python3.10 -m mypy reproducer.py
reproducer.py:12: error: "ExceptionInfo[BaseException]" has no attribute "iserrinstance"; maybe "errisinstance"?  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

The change was made in this commit but I'm not finding an explanation there as to why the type was changed from BaseException to Exception.

To me, it looks like the best fix is to change EXCEPTION_OR_MORE to

EXCEPTION_OR_MORE = Union[Type[BaseException], Tuple[Type[BaseException], ...]]

restoring backward compatibility.

@RonnyPfannschmidt
Copy link
Member

This is a oversight on my part,it needs a fix

@RonnyPfannschmidt RonnyPfannschmidt added type: regression indicates a problem that was introduced in a release which was working previously topic: typing type-annotation issue labels Jul 29, 2024
@nicoddemus
Copy link
Member

Just to clarify @RonnyPfannschmidt is that something you will tackle, or PRs are welcome?

@RonnyPfannschmidt
Copy link
Member

Prs Welcome, I'm not at capacity ATM

patchback bot pushed a commit that referenced this issue Aug 9, 2024
Fixes #12667

(cherry picked from commit cb98538)
nicoddemus pushed a commit that referenced this issue Aug 9, 2024
Fixes #12667

(cherry picked from commit cb98538)

Co-authored-by: Reagan Lee <96998476+reaganjlee@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: typing type-annotation issue type: regression indicates a problem that was introduced in a release which was working previously
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants