Skip to content

Commit

Permalink
Guido comments and polish
Browse files Browse the repository at this point in the history
  • Loading branch information
sixolet committed Feb 17, 2017
1 parent 37ba7d4 commit 04991b7
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 37 deletions.
2 changes: 1 addition & 1 deletion mypy/fastparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from typing import Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, cast, List, Set
from mypy.sharedparse import (
special_function_elide_names, argument_elide_name, is_overload_part,
special_function_elide_names, argument_elide_name,
)
from mypy.nodes import (
MypyFile, Node, ImportBase, Import, ImportAll, ImportFrom, FuncDef,
Expand Down
2 changes: 1 addition & 1 deletion mypy/fastparse2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from typing import Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, cast, List, Set
from mypy.sharedparse import (
special_function_elide_names, argument_elide_name, is_overload_part,
special_function_elide_names, argument_elide_name,
)
from mypy.nodes import (
MypyFile, Node, ImportBase, Import, ImportAll, ImportFrom, FuncDef, OverloadedFuncDef,
Expand Down
2 changes: 1 addition & 1 deletion mypy/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
EllipsisToken
)
from mypy.sharedparse import (
special_function_elide_names, argument_elide_name, is_overload_part,
special_function_elide_names, argument_elide_name,
)
from mypy.nodes import (
MypyFile, Import, ImportAll, ImportFrom, FuncDef, OverloadedFuncDef,
Expand Down
11 changes: 8 additions & 3 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,14 @@ def is_defined_type_var(self, tvar: str, context: Context) -> bool:
return self.lookup_qualified(tvar, context).kind == BOUND_TVAR

def visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
# Decide whether to analyze this as a property or an overload.
# If an overload, and we're outside a stub, find the impl and set it.
# Remove it from the item list, it's special.
# OverloadedFuncDef refers to any legitimate situation where you have
# more than one declaration for the same function in a row. This occurs
# with a @property with a setter or a deleter, and for a classic
# @overload.

# Decide whether to analyze this as a property or an overload. If an
# overload, and we're outside a stub, find the impl and set it. Remove
# the impl from the item list, it's special.
t = [] # type: List[CallableType]
non_overload_indexes = []
for i, item in enumerate(defn.items):
Expand Down
14 changes: 0 additions & 14 deletions mypy/sharedparse.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from typing import Union, Tuple

from mypy.nodes import Decorator, NameExpr, Statement, MemberExpr, Expression

"""Shared logic between our three mypy parser files."""


Expand Down Expand Up @@ -100,15 +98,3 @@ def special_function_elide_names(name: str) -> bool:

def argument_elide_name(name: Union[str, Tuple, None]) -> bool:
return isinstance(name, str) and name.startswith("__")


def _is_overload_decorator(dec: Expression) -> bool:
if isinstance(dec, NameExpr) and dec.name in {'overload', 'property', 'abstractproperty'}:
return True
elif isinstance(dec, MemberExpr) and dec.name in {'setter', 'deleter'}:
return True
return False


def is_overload_part(stmt: Decorator) -> bool:
return any(_is_overload_decorator(dec) for dec in stmt.decorators)
55 changes: 39 additions & 16 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ def f(x: 'A') -> 'B': ...
def f(x: 'B') -> 'A': ...

def f(x: Any) -> Any:
if isinstance(x, A):
return B()
else:
return A()
pass

reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
Expand All @@ -20,6 +17,30 @@ class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]

[case testTypeCheckOverloadWithImplementationPy2]
# flags: --python-version 2.7

from typing import overload
@overload
def f(x):
# type: (A) -> B
pass

@overload
def f(x):
# type: (B) -> A
pass

def f(x):
pass

reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'

class A: pass
class B: pass
[builtins fixtures/isinstance.pyi]

[case testTypeCheckOverloadWithImplementationError]
from typing import overload, Any

Expand All @@ -29,15 +50,20 @@ def f(x: 'A') -> 'B': ...
def f(x: 'B') -> 'A': ...

def f(x: Any) -> Any:
if isinstance(x, A):
y = x
y = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
return y
else:
return A()
foo = 1
foo = "bar" # E: Incompatible types in assignment (expression has type "str", variable has type "int")

reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
@overload
def g(x: 'A') -> 'B': ...
@overload
def g(x: 'B') -> 'A': ...

def g(x):
foo = 1
foo = "bar"

reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'

class A: pass
class B: pass
Expand All @@ -57,10 +83,7 @@ def f(x: 'A') -> 'B': ...
def f(x: 'B') -> 'A': ...

def f(x: 'A') -> Any: # E: Overloaded function implementation cannot accept all possible arguments of signature 2
if x is a:
return B()
else:
return A()
pass

reveal_type(f(A())) # E: Revealed type is '__main__.B'
reveal_type(f(B())) # E: Revealed type is '__main__.A'
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ from typing import overload
def dec(x): pass
@dec
def f(): pass
@dec # E: Name 'f' already defined
@dec # E: Name 'f' already defined
def f(): pass
[out]

Expand Down

0 comments on commit 04991b7

Please sign in to comment.