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 overload handling with union types in signatures #3300

Merged
merged 6 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,4 @@ def is_more_precise(t: Type, s: Type) -> bool:
# Fall back to subclass check and ignore other properties of the callable.
return is_proper_subtype(t.fallback, s)
return is_proper_subtype(t, s)
return sametypes.is_same_type(t, s)
return is_proper_subtype(t, s)
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need a special case when t is CallableType and s is Instance? It looks like ProperSubtypeVisitor above does exactly the same. So that the whole function boils down to:

    if isinstance(s, AnyType):
        return True
    return is_proper_subtype(t, s)

Copy link
Member

Choose a reason for hiding this comment

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

Good point. I puzzled over this for a while and it really doesn't help that the visitor uses left/right while this function uses t/s.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated.

4 changes: 2 additions & 2 deletions mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ def test_is_more_precise(self) -> None:
assert_true(is_more_precise(fx.b, fx.anyt))
assert_true(is_more_precise(self.tuple(fx.b, fx.a),
self.tuple(fx.b, fx.a)))
assert_true(is_more_precise(self.tuple(fx.b, fx.b),
self.tuple(fx.b, fx.a)))

assert_false(is_more_precise(fx.a, fx.b))
assert_false(is_more_precise(fx.anyt, fx.b))
assert_false(is_more_precise(self.tuple(fx.b, fx.b),
self.tuple(fx.b, fx.a)))

# is_proper_subtype

Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -596,3 +596,18 @@ x: Optional[Union[int, str]]
reveal_type(x) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.None]'
y: Optional[Union[int, None]]
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]'

[case testOverloadWithNoneAndOptional]
from typing import overload, Optional

@overload
def f(x: int) -> str: ...
@overload
def f(x: Optional[int]) -> Optional[str]: ...
def f(x): return x

reveal_type(f(1))
reveal_type(f(None))
[out]
main:9: error: Revealed type is 'builtins.str'
main:10: error: Revealed type is 'Union[builtins.str, builtins.None]'