Repository navigation
Keep ad-hoc intersections when narrowing with a tuple of types - #22141
Open
mohitduhan19 wants to merge 2 commits into
Open
mohitduhan19 wants to merge 2 commits into
mohitduhan19 wants to merge 2 commits into
Conversation
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: spark (https://github.com/apache/spark)
+ python/pyspark/sql/session.py:1236: error: Argument "names" to "_inferSchema" of "SparkSession" has incompatible type "<subclass of "pyspark.sql.types.DataType" and "tuple[Any, ...]"> | list[str] | None"; expected "list[str] | None" [arg-type]
+ python/pyspark/sql/session.py:1278: error: Argument "names" to "_inferSchemaFromList" of "SparkSession" has incompatible type "<subclass of "pyspark.sql.types.DataType" and "tuple[Any, ...]"> | list[str] | None"; expected "list[str] | None" [arg-type]
meson (https://github.com/mesonbuild/meson)
+ mesonbuild/optinterpreter.py:127:24: error: Incompatible return value type (got "int | float", expected "TYPE_elementary | HoldableObject | MesonInterpreterObject | Sequence[TYPE_var] | dict[str, TYPE_var]") [return-value]
+ mesonbuild/ast/interpreter.py:692:24: error: Incompatible return value type (got "int | float", expected "str | int | bool | HoldableObject | MesonInterpreterObject | <6 more items>") [return-value]
apprise (https://github.com/caronc/apprise)
- apprise/apprise_attachment.py:186: error: Item "AttachBase" of "str | AttachBase | list[str | AttachBase | AppriseAttachment]" has no attribute "__iter__" (not iterable) [union-attr]
+ apprise/apprise_attachment.py:186: error: Item "AttachBase" of "str | AttachBase | <subclass of "tuple[Any, ...]" and "apprise.apprise_attachment.AppriseAttachment"> | list[str | AttachBase | AppriseAttachment]" has no attribute "__iter__" (not iterable) [union-attr]
scipy-stubs (https://github.com/scipy/scipy-stubs)
- tests/misc/test_submodules_empty.pyi:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
|
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.
Fixes #22122
Problem
Boverlaps withA, soconditional_types()returnsB | C. The ad-hoc intersection fallback inconditional_types_with_intersection()only runs when the whole result is uninhabited, so it never runs here. When the result is later narrowed against the declared typeA, theCpart is dropped andxbecomes justB. A common subclass ofAandC(e.g.class D(A, C)) still passes the check at runtime, so the narrowing is unsound, and mypyc-compiled code raisesTypeError: B object expected; got D.A single
isinstance(x, C)already narrows correctly to<subclass of "A" and "C">.Fix
When there are several type ranges and the overall result is inhabited, the yes-type is now built per range. A range that overlaps with the current type keeps its existing result. A range that is disjoint from it (an
Instanceon both sides) adds the same ad-hoc intersection that a singleisinstancecheck would produce.If no range is disjoint, the result is unchanged, so single-type checks and fully overlapping tuples behave exactly as before.
isinstance(x, (B, C))now narrows toB | <subclass of "A" and "C">.Tests
Added
testIsInstanceAdHocIntersectionTuplePartialOverlap(the issue's example, reversed tuple order, a fully overlapping tuple that stays unchanged, and a union declared type) andtestIsInstanceAdHocIntersectionTuplePartialOverlapFinal(a@finaldisjoint class adds nothing).Updated
testIsInstanceWithStarExpressionAndVariable: with the test fixtures,intandtuplecan have a common subclass, so the result now includes<subclass of "builtins.int" and "tuple[Any, ...]">. This matches whatisinstance(var, tuple)alone already gives with the same fixtures. With real typeshed,intandtuplecan't share a subclass, so users won't see this.Locally,
testcheck,testfinegrained,testdeps,testpythonevaland mypyctest_irbuildpass, and the self-check,blackandruffare clean.