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

Support overloading with TypedDict #3612

Merged
merged 1 commit into from
Jun 26, 2017
Merged

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jun 26, 2017

Fix #3609.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Approved despite some quibbles (maybe you can open a tracker issue?)

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.

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

@gvanrossum gvanrossum merged commit c344496 into master Jun 26, 2017
@gvanrossum gvanrossum deleted the typeddict-overloading branch June 29, 2017 23:23
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.

3 participants