forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Overhaul overload semantics, remove erasure, add union math
This pull request: 1. Modifies how mypy handles overloads to match the [proposal][0] I made in the typing repo. 2. Starts removing type erasure from overload checks. 3. Adds support for basic union math. 4. Makes overloads respect keyword-only args This pull request does NOT implement the following features: 1. Special-casing descriptors 2. Various improvements and refactorings for operator methods (e.g. `__add__`, `__radd__`, etc...) 3. Detecting partially overlapping arguments. Note: I think initially we were thinking only unions can be partially overlapping but on reflection, I think tuples and typevars could also be partially overlapping. For example: @overload def f(x: Tuple[int, ...]) -> str: ... @overload def f(x: Tuple[int, int]) -> int: ... T = TypeVar('T', A, B) S = TypeVar('S', B, C) @overload def g(x: T) -> int: ... @overload def g(x: S) -> str: ... 4. Detecting "overlapping argument counts". For example, we should flag the following as an error since the first alternative could potentially overlap with the second. @overload def f(*args: int) -> int: ... @overload def f(x: int, y: int, z: int) -> str: ... 5. The "is-more-precise" relation. It's working in most normal cases, but it does contain a few bugs, mostly relating to typevars. For example, this is currently *not* flagged as an error, even though it should be: class Wrapper(Generic[T]): @overload def f(self, x: int) -> int: ... @overload def f(self, x: T) -> str: ... (This PR does the right thing if 'T' isn't bound to a containing class though:) class Wrapper: @overload def f(self, x: int, y: int) -> int: ... @overload def f(self, x: T, y: T) -> str: ... Currently, what I'm doing is using the existing `is_more_precise` method, which calls `is_proper_subtype`. To fix this, I think I'll either need to (a) just rewrite that method to do what I want with TypeVars or (b) find a way of "unbinding" methods from their class and force the two methods to unify their typevars before running the `is_proper_subtype` check. The plan is to address these 5 TODOs in future pull requests. Items 1 and 2 are basically orthogonal to the overloads overhaul; items 3, 4, and 5 basically boil down to finding ways to teach mypy to detect if one thing is *potentially* compatible with another. For example, mypy contains code to tell if one type is *definitely* a subtype of another; fixing items 3 and 5 involve writing code to check if a type is *potentially* a subtype of another. [0]: python/typing#253
- Loading branch information
1 parent
d6566be
commit 220c3a1
Showing
11 changed files
with
846 additions
and
182 deletions.
There are no files selected for viewing
This file contains 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
Oops, something went wrong.