Fix false positive unreachable error for isinstance(x, UnionType) on TypeForm - #22022
Open
Dextheking1 wants to merge 1 commit into
Open
Dextheking1 wants to merge 1 commit into
Dextheking1 wants to merge 1 commit into
Conversation
…TypeForm With --warn-unreachable, mypy reported 'Statement is unreachable' for isinstance(t, UnionType) when t: TypeForm[Any]. This is a false positive: at runtime a type expression like int | str is an instance of types.UnionType, so the check can succeed. Teach is_overlapping_types that a TypeForm whose item can be a union (Any, a union, or an unconstrained type variable) overlaps with types.UnionType. TypeForm[int] and friends remain correctly unreachable, and the bare-Union isinstance case is untouched. Fixes python#21910.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
With
--warn-unreachable, mypy reportedStatement is unreachable [unreachable]forisinstance(t, UnionType)whent: TypeForm[Any]:This is a false positive: at runtime a type expression like
int | stris an instance oftypes.UnionType, so the check can genuinely succeed. mypy treatedTypeFormandtypes.UnionTypeas disjoint.(The sibling behavior for
isinstance(t, Union)with bareUnionis untouched — bareUnionstill errors witharg-typesince it can't be used inisinstanceat runtime.)Fix
mypy/meet.py: teachis_overlapping_typesthat aTypeForm[T]overlaps withtypes.UnionTypewhen the item can be a union expression — i.e.Any, a union, or an unconstrained type variable (via new helper_type_form_item_can_be_union). The check runs before the existingType[C]vs metaclass case, which would otherwise returnFalse.Precision is preserved:
TypeForm[int]vsUnionTypeis still correctly reported unreachable (intis never aUnionTypeinstance)type[Any](non-TypeForm) behavior is unchangedtypes.UnionType; the else-branch keepsTypeForm[Any]Test evidence
New regression test
testTypeFormIsinstanceUnionTypeintest-data/unit/check-typeform.test(flags:--warn-unreachable) coveringTypeForm[Any],TypeForm[int | str](reachable, narrows totypes.UnionType) andTypeForm[int](still unreachable).Statement is unreachableon the reachable branches) and passes aftercheck-typeform.test+check-isinstance.test(244 passed),check-narrowing.test(175 passed, 1 xfailed),check-python310.test(202 passed, 1 skipped),check-overloading.test(272 passed, 1 skipped),testsubtypes.py(26 passed)Closes #21910.