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 conditional_type_map to not broaden the type (#10739) #10912

Closed
wants to merge 1 commit into from

Conversation

ahuhn
Copy link

@ahuhn ahuhn commented Aug 2, 2021

Description

Fixes #10739

Previously, if you used an isinstance check that is more broad than the current type, it would broaden the current type to the type of the isinstance.

After this change, an isinstance check can only narrow the type, not expand it.

Test Plan

I added a test testIsInstanceWithNonSubsetOverlap and ran it locally, both the individual test and with ./runtests.py. My new test failed before my change and succeeded after my change.

Then I tried it out on a codebase and I saw a few new failures. I added regression tests to maintain the old behavior

  • testIsInstanceWithTypeVar
  • testIsInstanceWithTypeVarAndSubclass

The change I made to pass those new regression tests was to switch from meet_types to narrow_declared_type.

@github-actions
Copy link
Contributor

github-actions bot commented Aug 2, 2021

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

websockets (https://github.com/aaugustin/websockets.git)
+ src/websockets/legacy/protocol.py:537: error: Redundant cast to "Iterable[Union[str, bytes]]"

spark (https://github.com/apache/spark.git)
+ python/pyspark/pandas/frame.py:6580: error: Unsupported operand types for < ("Tuple[Any, ...]" and "int")
+ python/pyspark/pandas/frame.py:6580: note: Left operand is of type "Union[int, Union[Any, Tuple[Any, ...]]]"
+ python/pyspark/pandas/frame.py:6588: error: No overload variant of "__add__" of "tuple" matches argument type "int"
+ python/pyspark/pandas/frame.py:6588: note: Possible overload variants:
+ python/pyspark/pandas/frame.py:6588: note:     def __add__(self, Tuple[Any, ...]) -> Tuple[Any, ...]
+ python/pyspark/pandas/frame.py:6588: note:     def [_T] __add__(self, Tuple[_T, ...]) -> Tuple[Union[Any, _T], ...]
+ python/pyspark/pandas/frame.py:6588: note: Left operand is of type "Union[int, Union[Any, Tuple[Any, ...]]]"

@ahuhn ahuhn closed this May 19, 2023
@ikonst
Copy link
Contributor

ikonst commented May 20, 2023

@ahuhn Did you close this only due to lack of uptake?

@ahuhn
Copy link
Author

ahuhn commented May 21, 2023

@ikonst I was just cleaning out my old PRs that I hadn't come back to in a while. Happy to re-open it if people are interested in the fix.

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 emits two conflicting results for reveal_type
2 participants