Skip to content

Commit 04991b7

Browse files
committed
Guido comments and polish
1 parent 37ba7d4 commit 04991b7

File tree

7 files changed

+51
-37
lines changed

7 files changed

+51
-37
lines changed

mypy/fastparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from typing import Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, cast, List, Set
55
from mypy.sharedparse import (
6-
special_function_elide_names, argument_elide_name, is_overload_part,
6+
special_function_elide_names, argument_elide_name,
77
)
88
from mypy.nodes import (
99
MypyFile, Node, ImportBase, Import, ImportAll, ImportFrom, FuncDef,

mypy/fastparse2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from typing import Tuple, Union, TypeVar, Callable, Sequence, Optional, Any, cast, List, Set
2121
from mypy.sharedparse import (
22-
special_function_elide_names, argument_elide_name, is_overload_part,
22+
special_function_elide_names, argument_elide_name,
2323
)
2424
from mypy.nodes import (
2525
MypyFile, Node, ImportBase, Import, ImportAll, ImportFrom, FuncDef, OverloadedFuncDef,

mypy/parse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
EllipsisToken
1616
)
1717
from mypy.sharedparse import (
18-
special_function_elide_names, argument_elide_name, is_overload_part,
18+
special_function_elide_names, argument_elide_name,
1919
)
2020
from mypy.nodes import (
2121
MypyFile, Import, ImportAll, ImportFrom, FuncDef, OverloadedFuncDef,

mypy/semanal.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,14 @@ def is_defined_type_var(self, tvar: str, context: Context) -> bool:
422422
return self.lookup_qualified(tvar, context).kind == BOUND_TVAR
423423

424424
def visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
425-
# Decide whether to analyze this as a property or an overload.
426-
# If an overload, and we're outside a stub, find the impl and set it.
427-
# Remove it from the item list, it's special.
425+
# OverloadedFuncDef refers to any legitimate situation where you have
426+
# more than one declaration for the same function in a row. This occurs
427+
# with a @property with a setter or a deleter, and for a classic
428+
# @overload.
429+
430+
# Decide whether to analyze this as a property or an overload. If an
431+
# overload, and we're outside a stub, find the impl and set it. Remove
432+
# the impl from the item list, it's special.
428433
t = [] # type: List[CallableType]
429434
non_overload_indexes = []
430435
for i, item in enumerate(defn.items):

mypy/sharedparse.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from typing import Union, Tuple
22

3-
from mypy.nodes import Decorator, NameExpr, Statement, MemberExpr, Expression
4-
53
"""Shared logic between our three mypy parser files."""
64

75

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

10199
def argument_elide_name(name: Union[str, Tuple, None]) -> bool:
102100
return isinstance(name, str) and name.startswith("__")
103-
104-
105-
def _is_overload_decorator(dec: Expression) -> bool:
106-
if isinstance(dec, NameExpr) and dec.name in {'overload', 'property', 'abstractproperty'}:
107-
return True
108-
elif isinstance(dec, MemberExpr) and dec.name in {'setter', 'deleter'}:
109-
return True
110-
return False
111-
112-
113-
def is_overload_part(stmt: Decorator) -> bool:
114-
return any(_is_overload_decorator(dec) for dec in stmt.decorators)

test-data/unit/check-overloading.test

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ def f(x: 'A') -> 'B': ...
88
def f(x: 'B') -> 'A': ...
99

1010
def f(x: Any) -> Any:
11-
if isinstance(x, A):
12-
return B()
13-
else:
14-
return A()
11+
pass
1512

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

20+
[case testTypeCheckOverloadWithImplementationPy2]
21+
# flags: --python-version 2.7
22+
23+
from typing import overload
24+
@overload
25+
def f(x):
26+
# type: (A) -> B
27+
pass
28+
29+
@overload
30+
def f(x):
31+
# type: (B) -> A
32+
pass
33+
34+
def f(x):
35+
pass
36+
37+
reveal_type(f(A())) # E: Revealed type is '__main__.B'
38+
reveal_type(f(B())) # E: Revealed type is '__main__.A'
39+
40+
class A: pass
41+
class B: pass
42+
[builtins fixtures/isinstance.pyi]
43+
2344
[case testTypeCheckOverloadWithImplementationError]
2445
from typing import overload, Any
2546

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

3152
def f(x: Any) -> Any:
32-
if isinstance(x, A):
33-
y = x
34-
y = B() # E: Incompatible types in assignment (expression has type "B", variable has type "A")
35-
return y
36-
else:
37-
return A()
53+
foo = 1
54+
foo = "bar" # E: Incompatible types in assignment (expression has type "str", variable has type "int")
3855

39-
reveal_type(f(A())) # E: Revealed type is '__main__.B'
40-
reveal_type(f(B())) # E: Revealed type is '__main__.A'
56+
@overload
57+
def g(x: 'A') -> 'B': ...
58+
@overload
59+
def g(x: 'B') -> 'A': ...
60+
61+
def g(x):
62+
foo = 1
63+
foo = "bar"
64+
65+
reveal_type(f(A())) # E: Revealed type is '__main__.B'
66+
reveal_type(f(B())) # E: Revealed type is '__main__.A'
4167

4268
class A: pass
4369
class B: pass
@@ -57,10 +83,7 @@ def f(x: 'A') -> 'B': ...
5783
def f(x: 'B') -> 'A': ...
5884

5985
def f(x: 'A') -> Any: # E: Overloaded function implementation cannot accept all possible arguments of signature 2
60-
if x is a:
61-
return B()
62-
else:
63-
return A()
86+
pass
6487

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

test-data/unit/semanal-errors.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ from typing import overload
10821082
def dec(x): pass
10831083
@dec
10841084
def f(): pass
1085-
@dec # E: Name 'f' already defined
1085+
@dec # E: Name 'f' already defined
10861086
def f(): pass
10871087
[out]
10881088

0 commit comments

Comments
 (0)