Skip to content

Conversation

@subnix
Copy link
Contributor

@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]]

@github-actions

This comment has been minimized.

@subnix
Copy link
Contributor Author

subnix commented May 27, 2025

@JelleZijlstra since you initially made the fix (#2846), could you please review it?

@github-actions
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]

@hauntsaninja hauntsaninja merged commit ece4d41 into python:master Nov 28, 2025
20 checks passed
@hauntsaninja
Copy link
Collaborator

Thank you!

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

2 participants