Skip to content

meet.is_overlapping_types should be more relaxed for isinstance checks #5529

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

Open
Michael0x2a opened this issue Aug 27, 2018 · 0 comments
Open
Labels
needs discussion priority-1-normal refactoring Changing mypy's internals topic-reachability Detecting unreachable code topic-type-narrowing Conditional type narrowing / binder

Comments

@Michael0x2a
Copy link
Collaborator

Michael0x2a commented Aug 27, 2018

Currently, the implementation of meet.is_overlapping_types(...) defaults to returning False when it encounters two unexpected types, and doesn't attempt to handle multiple inheritance at all. The current implementation also considers two types like List[A] and List[B] to be non-overlapping, even though they may overlap when both lists are empty.

This behavior makes sense when is_overlapping_types is used to detect unsafe overload variants, but may be too restrictive/cause branches to be unexpectedly unreachable when used for isinstance checks and if x is None checks and the like.

For more context, see #5476 (comment) and #3603.

Some possible solutions include:

  1. Add a flag that makes is_overlapping_types default to returning True, not False. The main disadvantage here is that this would then make is_overlapping_types consider two unrelated types like Type[A] and TypedDict[...] to be overlapping, which seems wrong.
  2. Add a flag named allow_multiple_inheritance which makes the "if left and right are Instances" case just always return True. The main disadvantage here is that this may not capture every edge case we want to relax.

It may also be the case that neither solutions work and a more careful rearrangement of is_overlapping_types is necessary. The existing implementation of is_overlapping_types is designed to rule out bad combinations/normalize all types to Instance whenever possible, which may or may not be the correct thing to do for reachability checks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs discussion priority-1-normal refactoring Changing mypy's internals topic-reachability Detecting unreachable code topic-type-narrowing Conditional type narrowing / binder
Projects
None yet
Development

No branches or pull requests

4 participants