Skip to content

Commit f7bf384

Browse files
committed
add tests for mixed async-def
1 parent 87377ed commit f7bf384

File tree

8 files changed

+81
-16
lines changed

8 files changed

+81
-16
lines changed

conformance/results/mypy/overloads_consistency.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ errors_diff = """
44
"""
55
output = """
66
overloads_consistency.py:27: error: Overloaded function implementation cannot produce return type of signature 2 [misc]
7-
overloads_consistency.py:42: error: Overloaded function implementation does not accept all possible arguments of signature 2 [misc]
7+
overloads_consistency.py:43: error: Overloaded function implementation does not accept all possible arguments of signature 2 [misc]
88
"""

conformance/results/pyre/overloads_consistency.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ errors_diff = """
44
"""
55
output = """
66
overloads_consistency.py:24:0 Incompatible overload [43]: The return type of overloaded function `return_type` (`str`) is incompatible with the return type of the implementation (`int`).
7-
overloads_consistency.py:39:0 Incompatible overload [43]: The implementation of `parameter_type` does not accept all possible arguments of overload defined on line `39`.
7+
overloads_consistency.py:40:0 Incompatible overload [43]: The implementation of `parameter_type` does not accept all possible arguments of overload defined on line `40`.
88
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = "pyre 0.9.23"
2-
test_duration = 6.6
2+
test_duration = 6.5

conformance/results/pyright/overloads_consistency.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ output = """
66
overloads_consistency.py:27:5 - error: Overloaded implementation is not consistent with signature of overload 2
77
  Function return type "str" is incompatible with type "int"
88
    "str" is not assignable to "int" (reportInconsistentOverload)
9-
overloads_consistency.py:42:5 - error: Overloaded implementation is not consistent with signature of overload 2
9+
overloads_consistency.py:43:5 - error: Overloaded implementation is not consistent with signature of overload 2
1010
  Type "(x: int) -> (int | str)" is not assignable to type "(x: str) -> str"
1111
    Parameter 1: type "str" is incompatible with type "int"
1212
      "str" is not assignable to "int" (reportInconsistentOverload)

conformance/results/pytype/overloads_consistency.toml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,36 @@ Doesn't appear to validate overload consistency at all.
55
conformance_automated = "Fail"
66
errors_diff = """
77
Lines 24, 27: Expected error (tag 'return_type')
8-
Lines 39, 42: Expected error (tag 'parameter_type')
8+
Lines 40, 43: Expected error (tag 'parameter_type')
99
Line 28: Unexpected errors ['overloads_consistency.py:28:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in return_type: bad return type [bad-return-type]']
10-
Line 43: Unexpected errors ['overloads_consistency.py:43:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in parameter_type: bad return type [bad-return-type]']
10+
Line 44: Unexpected errors ['overloads_consistency.py:44:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in parameter_type: bad return type [bad-return-type]']
11+
Line 73: Unexpected errors ['overloads_consistency.py:73:12: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in returns_coroutine: bad return type [bad-return-type]']
12+
Line 88: Unexpected errors ['overloads_consistency.py:88:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in returns_coroutine_2: bad return type [bad-return-type]', 'overloads_consistency.py:88:5: \\x1b[1m\\x1b[31merror\\x1b[39m\\x1b[0m: in returns_coroutine_2: bad return type [bad-return-type]']
1113
"""
1214
output = """
1315
overloads_consistency.py:28:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in return_type: bad return type [bad-return-type]
1416
1517
return 1
1618
\u001b[1m\u001b[31m~\u001b[39m\u001b[0m
1719
18-
overloads_consistency.py:43:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in parameter_type: bad return type [bad-return-type]
20+
overloads_consistency.py:44:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in parameter_type: bad return type [bad-return-type]
1921
2022
return 1
2123
\u001b[1m\u001b[31m~\u001b[39m\u001b[0m
2224
25+
overloads_consistency.py:73:12: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in returns_coroutine: bad return type [bad-return-type]
26+
27+
return 1
28+
\u001b[1m\u001b[31m~\u001b[39m\u001b[0m
29+
30+
overloads_consistency.py:88:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in returns_coroutine_2: bad return type [bad-return-type]
31+
32+
return _wrapped(x)
33+
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
34+
35+
overloads_consistency.py:88:5: \u001b[1m\u001b[31merror\u001b[39m\u001b[0m: in returns_coroutine_2: bad return type [bad-return-type]
36+
37+
return _wrapped(x)
38+
\u001b[1m\u001b[31m~~~~~~~~~~~~~~~~~~\u001b[39m\u001b[0m
39+
2340
"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
version = "pytype 2024.10.11"
2-
test_duration = 30.3
2+
test_duration = 30.1

conformance/results/results.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ <h3>Python Type System Conformance Test Results</h3>
165165
<div class='tc-time'>1.5sec</div>
166166
</th>
167167
<th class='tc-header'><div class='tc-name'>pyre 0.9.23</div>
168-
<div class='tc-time'>6.6sec</div>
168+
<div class='tc-time'>6.5sec</div>
169169
</th>
170170
<th class='tc-header'><div class='tc-name'>pytype 2024.10.11</div>
171-
<div class='tc-time'>30.3sec</div>
171+
<div class='tc-time'>30.1sec</div>
172172
</th>
173173
</tr>
174174
<tr><th class="column" colspan="5">
@@ -673,10 +673,10 @@ <h3>Python Type System Conformance Test Results</h3>
673673
<th class="column col2 partially-conformant">Partial</th>
674674
</tr>
675675
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overloads_consistency</th>
676-
<th class="column col2 not-conformant">Unknown</th>
677-
<th class="column col2 not-conformant">Unknown</th>
678-
<th class="column col2 not-conformant">Unknown</th>
679-
<th class="column col2 not-conformant">Unknown</th>
676+
<th class="column col2 conformant">Pass</th>
677+
<th class="column col2 conformant">Pass</th>
678+
<th class="column col2 conformant">Pass</th>
679+
<th class="column col2 not-conformant"><div class="hover-text">Fail<span class="tooltip-text" id="bottom"><p>Doesn't appear to validate overload consistency at all.</p></span></div></th>
680680
</tr>
681681
<tr><th class="column col1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;overloads_definitions</th>
682682
<th class="column col2 partially-conformant"><div class="hover-text">Partial<span class="tooltip-text" id="bottom"><p>Does not allow an overload with no implementation in an abstract base class.</p><p>Allows @override to be on all overloads and implementation, instead of just implementation.</p></span></div></th>

conformance/tests/overloads_consistency.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Tests consistency of overloads with implementation.
33
"""
44

5-
from typing import overload
5+
from typing import Coroutine, overload
66

77
# > If an overload implementation is defined, type checkers should validate
88
# > that it is consistent with all of its associated overload signatures.
@@ -29,7 +29,8 @@ def return_type(x: int | str) -> int: # E[return_type] an overload returns `str
2929

3030

3131
# Input signature of implementation must be assignable to signature of each
32-
# overload:
32+
# overload. We don't attempt a thorough testing of input signature
33+
# assignability here; see `callables_subtyping.py` for that:
3334

3435
@overload
3536
def parameter_type(x: int) -> int:
@@ -41,3 +42,50 @@ def parameter_type(x: str) -> str: # E[parameter_type]
4142

4243
def parameter_type(x: int) -> int | str: # E[parameter_type] impl type of `x` must be assignable from overload types of `x`
4344
return 1
45+
46+
47+
# > Overloads are allowed to use a mixture of ``async def`` and ``def`` statements
48+
# > within the same overload definition. Type checkers should convert
49+
# > ``async def`` statements to a non-async signature (wrapping the return
50+
# > type in a ``Coroutine``) before testing for implementation consistency
51+
# > and overlapping overloads (described below).
52+
53+
# ...and also...
54+
55+
# > When a type checker checks the implementation for consistency with overloads,
56+
# > it should first apply any transforms that change the effective type of the
57+
# > implementation including the presence of a ``yield`` statement in the
58+
# > implementation body, the use of ``async def``, and the presence of additional
59+
# > decorators.
60+
61+
# An overload can explicitly return `Coroutine`, while the implementation is an
62+
# `async def`:
63+
64+
@overload
65+
def returns_coroutine(x: int) -> Coroutine[None, None, int]:
66+
...
67+
68+
@overload
69+
async def returns_coroutine(x: str) -> str:
70+
...
71+
72+
async def returns_coroutine(x: int | str) -> int | str:
73+
return 1
74+
75+
76+
# The implementation can explicitly return `Coroutine`, while overloads are
77+
# `async def`:
78+
79+
@overload
80+
async def returns_coroutine_2(x: int) -> int:
81+
...
82+
83+
@overload
84+
async def returns_coroutine_2(x: str) -> str:
85+
...
86+
87+
def returns_coroutine_2(x: int | str) -> Coroutine[None, None, int | str]:
88+
return _wrapped(x)
89+
90+
async def _wrapped(x: int | str) -> int | str:
91+
return 2

0 commit comments

Comments
 (0)