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

fix: unpack unions inside tuples in except handlers #17762

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

subnix
Copy link

@subnix subnix commented Sep 12, 2024

Fixes #17759

mypy unpacks only top-level unions in except handlers, leaving unions inside tuples unchanged. This leads to the failed check because the Union type isn't a subtype of the BaseException type. We can fix this by simplifying and unpacking types inside tuples.

Before the patch:

source = Tuple[Union[Type[ValueError], Type[RuntimeError]], Type[ZeroDivisionError]]
result = [Union[Type[ValueError], Type[RuntimeError]], Type[ZeroDivisionError]]

After the patch:

source = Tuple[Union[Type[ValueError], Type[RuntimeError]], Type[ZeroDivisionError]]
result = [Type[ValueError], Type[RuntimeError], Type[ZeroDivisionError]]

Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

mongo-python-driver (https://github.com/mongodb/mongo-python-driver)
+ pymongo/socket_checker.py:82: error: Unused "type: ignore" comment  [unused-ignore]

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.

mypy doesn't infer Union inside Tuple in an except clause
1 participant