Skip to content

Support overloading with TypedDict #3612

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

Merged
merged 1 commit into from
Jun 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2674,6 +2674,12 @@ def overload_arg_similarity(actual: Type, formal: Type) -> int:
return overload_arg_similarity(actual.ret_type, formal.item)
else:
return 0
if isinstance(actual, TypedDictType):
if isinstance(formal, TypedDictType):
# Don't support overloading based on the keys or value types of a TypedDict since
# that would be complicated and probably only marginally useful.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you felt compelled to add a comment about that, how sure are you? What are the symptoms when someone tries to overload on two different TypedDict types? Will calls with any kind of dict then just fail with a vague error? Does this deserves opening a tracker issue to debate it further?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comment below is correct -- signatures are considered to be overlapping. I created a new issue: #3618.

Note that there are other similar issues such as not being able to overload based on tuple structure, I think. I don't remember hearing user complaints about them, so this is perhaps not a high-priority issue. Anyway let's wait for user feedback before we do more work on this.

return 2
return overload_arg_similarity(actual.fallback, formal)
if isinstance(formal, Instance):
if isinstance(actual, CallableType):
actual = actual.fallback
Expand Down
4 changes: 4 additions & 0 deletions mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ class C(A, B): ...
t = t.erase_to_union_or_bound()
if isinstance(s, TypeVarType):
s = s.erase_to_union_or_bound()
if isinstance(t, TypedDictType):
t = t.as_anonymous().fallback
if isinstance(s, TypedDictType):
s = s.as_anonymous().fallback
if isinstance(t, Instance):
if isinstance(s, Instance):
# Consider two classes non-disjoint if one is included in the mro
Expand Down
119 changes: 119 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,125 @@ Point = TypedDict('Point', {'x': 1, 'y': 1}) # E: Invalid field type
[builtins fixtures/dict.pyi]


-- Overloading

[case testTypedDictOverloading]
from typing import overload, Iterable
from mypy_extensions import TypedDict

A = TypedDict('A', {'x': int})

@overload
def f(x: Iterable[str]) -> str: ...
@overload
def f(x: int) -> int: ...
def f(x): pass

a: A
reveal_type(f(a)) # E: Revealed type is 'builtins.str'
reveal_type(f(1)) # E: Revealed type is 'builtins.int'
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

[case testTypedDictOverloading2]
from typing import overload, Iterable
from mypy_extensions import TypedDict

A = TypedDict('A', {'x': int})

@overload
def f(x: Iterable[int]) -> None: ...
@overload
def f(x: int) -> None: ...
def f(x): pass

a: A
f(a) # E: Argument 1 to "f" has incompatible type "A"; expected Iterable[int]
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

[case testTypedDictOverloading3]
from typing import overload
from mypy_extensions import TypedDict

A = TypedDict('A', {'x': int})

@overload
def f(x: str) -> None: ...
@overload
def f(x: int) -> None: ...
def f(x): pass

a: A
f(a) # E: No overload variant of "f" matches argument types [TypedDict(x=builtins.int, _fallback=__main__.A)]
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

[case testTypedDictOverloading4]
from typing import overload
from mypy_extensions import TypedDict

A = TypedDict('A', {'x': int})
B = TypedDict('B', {'x': str})

@overload
def f(x: A) -> int: ...
@overload
def f(x: int) -> str: ...
def f(x): pass

a: A
b: B
reveal_type(f(a)) # E: Revealed type is 'builtins.int'
reveal_type(f(1)) # E: Revealed type is 'builtins.str'
f(b) # E: Argument 1 to "f" has incompatible type "B"; expected "A"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

[case testTypedDictOverloading5]
from typing import overload
from mypy_extensions import TypedDict

A = TypedDict('A', {'x': int})
B = TypedDict('B', {'y': str})
C = TypedDict('C', {'y': int})

@overload
def f(x: A) -> None: ...
@overload
def f(x: B) -> None: ...
def f(x): pass

a: A
b: B
c: C
f(a)
f(b)
f(c) # E: Argument 1 to "f" has incompatible type "C"; expected "A"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]

[case testTypedDictOverloading6]
from typing import overload
from mypy_extensions import TypedDict

A = TypedDict('A', {'x': int})
B = TypedDict('B', {'y': str})

@overload
def f(x: A) -> int: ... # E: Overloaded function signatures 1 and 2 overlap with incompatible return types
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is the situation the comment I called out earlier refers to. I am okay with merging this now but I think it's at least debatable whether this should be allowed. What does PEP 544 (Protocols) do for similar situations?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protocols actually allow overloads currently. For example:

class A(Protocol):
    x: int
class B(Protocol):
    y: str

@overload
def f(x: A) -> int: ...
@overload
def f(x: B) -> str: ...
def f(x): ...

class C:
    x: int
    y: str

reveal_type(f(C()))  # No error, but revealed type is ``Any``

It looks like this hits second part of issue #3295 (silently inferring Any for some overloads).

In general I think we should support overloading on protocols, although arbitrary two protocols are obviously overlapping. This means however that we should decide on python/typing#253

@overload
def f(x: B) -> str: ...
def f(x): pass

a: A
b: B
reveal_type(f(a)) # E: Revealed type is 'Any'
reveal_type(f(b)) # E: Revealed type is 'Any'
[builtins fixtures/dict.pyi]
[typing fixtures/typing-full.pyi]


-- Special cases

[case testForwardReferenceInTypedDict]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fixtures/dict.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class object:

class type: pass

class dict(Iterable[KT], Mapping[KT, VT], Generic[KT, VT]):
class dict(Mapping[KT, VT], Iterable[KT], Generic[KT, VT]):
@overload
def __init__(self, **kwargs: VT) -> None: pass
@overload
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/fixtures/typing-full.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ class Sequence(Iterable[T], Generic[T]):
@abstractmethod
def __getitem__(self, n: Any) -> T: pass

class Mapping(Generic[T, U]):
class Mapping(Iterable[T], Generic[T, U]):
@overload
def get(self, k: T) -> Optional[U]: ...
@overload
def get(self, k: T, default: Union[U, V]) -> Union[U, V]: ...

class MutableMapping(Generic[T, U]): pass
class MutableMapping(Mapping[T, U]): pass

TYPE_CHECKING = 1