Skip to content

Commit 8aebf0b

Browse files
authored
Fix overload interaction with Any (#5233)
Fixes #5232 This also makes few minor amendments to pythoneval tests.
1 parent f0333a8 commit 8aebf0b

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

mypy/checkexpr.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,8 +1304,10 @@ def infer_overload_return_type(self,
13041304
return None
13051305
elif any_causes_overload_ambiguity(matches, return_types, arg_types, arg_kinds, arg_names):
13061306
# An argument of type or containing the type 'Any' caused ambiguity.
1307-
if all(is_subtype(ret_type, return_types[-1]) for ret_type in return_types[:-1]):
1308-
# The last match is a supertype of all the previous ones, so it's safe
1307+
if all(is_subtype(ret_type, return_types[-1]) and
1308+
is_subtype(return_types[-1], ret_type)
1309+
for ret_type in return_types[:-1]):
1310+
# The last match is mutually compatible with all previous ones, so it's safe
13091311
# to return that inferred type.
13101312
return return_types[-1], inferred_types[-1]
13111313
else:

test-data/unit/check-overloading.test

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ def f(x: object) -> object: ...
12761276
def f(x): pass
12771277

12781278
a: Any
1279-
reveal_type(f(a)) # E: Revealed type is 'builtins.object'
1279+
reveal_type(f(a)) # E: Revealed type is 'Any'
12801280

12811281
[case testOverloadWithOverlappingItemsAndAnyArgument2]
12821282
from typing import overload, Any
@@ -1288,7 +1288,7 @@ def f(x: float) -> float: ...
12881288
def f(x): pass
12891289

12901290
a: Any
1291-
reveal_type(f(a)) # E: Revealed type is 'builtins.float'
1291+
reveal_type(f(a)) # E: Revealed type is 'Any'
12921292

12931293
[case testOverloadWithOverlappingItemsAndAnyArgument3]
12941294
from typing import overload, Any
@@ -1312,17 +1312,16 @@ def f(x: object, y: int, z: str) -> object: ...
13121312
def f(x): pass
13131313

13141314
a: Any
1315-
# Any causes ambiguity; we fall back to returning object since it's a
1316-
# supertype of int
1317-
reveal_type(f(a, 1, '')) # E: Revealed type is 'builtins.object'
1315+
# Any causes ambiguity
1316+
reveal_type(f(a, 1, '')) # E: Revealed type is 'Any'
13181317
# Any causes no ambiguity
13191318
reveal_type(f(1, a, a)) # E: Revealed type is 'builtins.int'
13201319
reveal_type(f('', a, a)) # E: Revealed type is 'builtins.object'
13211320
# Like above, but use keyword arguments.
1322-
reveal_type(f(y=1, z='', x=a)) # E: Revealed type is 'builtins.object'
1321+
reveal_type(f(y=1, z='', x=a)) # E: Revealed type is 'Any'
13231322
reveal_type(f(y=a, z='', x=1)) # E: Revealed type is 'builtins.int'
13241323
reveal_type(f(z='', x=1, y=a)) # E: Revealed type is 'builtins.int'
1325-
reveal_type(f(z='', x=a, y=1)) # E: Revealed type is 'builtins.object'
1324+
reveal_type(f(z='', x=a, y=1)) # E: Revealed type is 'Any'
13261325

13271326
[case testOverloadWithOverlappingItemsAndAnyArgument5]
13281327
from typing import overload, Any, Union
@@ -1334,7 +1333,7 @@ def f(x: Union[int, float]) -> float: ...
13341333
def f(x): pass
13351334

13361335
a: Any
1337-
reveal_type(f(a)) # E: Revealed type is 'builtins.float'
1336+
reveal_type(f(a)) # E: Revealed type is 'Any'
13381337

13391338
[case testOverloadWithOverlappingItemsAndAnyArgument6]
13401339
from typing import overload, Any
@@ -1372,7 +1371,7 @@ def g(x): pass
13721371

13731372
a: Any
13741373
reveal_type(f(1, *a)) # E: Revealed type is 'builtins.int'
1375-
reveal_type(g(1, *a)) # E: Revealed type is 'builtins.object'
1374+
reveal_type(g(1, *a)) # E: Revealed type is 'Any'
13761375

13771376
[case testOverloadWithOverlappingItemsAndAnyArgument8]
13781377
from typing import overload, Any

test-data/unit/python2eval.test

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ print b.x + 1
342342
[out]
343343
4
344344

345-
[case testReModuleBytesPython2]
345+
[case testReModuleBytes_python2]
346346
# Regression tests for various overloads in the re module -- bytes version
347347
import re
348348
if False:
@@ -365,7 +365,7 @@ if False:
365365
re.subn(bpat, lambda m: b'', b'')[0] + b''
366366
[out]
367367

368-
[case testReModuleStringPython2]
368+
[case testReModuleString_python2]
369369
# Regression tests for various overloads in the re module -- string version
370370
import re
371371
ure = u'a+'
@@ -393,3 +393,11 @@ def g(): # type: () -> int
393393
yield
394394
[out]
395395
_program.py:2: error: The return type of a generator function should be "Generator" or one of its supertypes
396+
397+
[case testOsPathJoinWorksWithAny_python2]
398+
import os
399+
def f(): # no annotation
400+
return 'tests'
401+
path = 'test'
402+
path = os.path.join(f(), 'test.py')
403+
[out]

test-data/unit/pythoneval.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,9 +1109,11 @@ async def main() -> None:
11091109
((a_x, a_y),) = await asyncio.gather(get_location('start'))
11101110
reveal_type(a_x)
11111111
reveal_type(a_y)
1112+
reveal_type(asyncio.gather(*[asyncio.sleep(1), asyncio.sleep(1)]))
11121113
[out]
11131114
_testAsyncioGatherPreciseType.py:9: error: Revealed type is 'builtins.str'
11141115
_testAsyncioGatherPreciseType.py:10: error: Revealed type is 'builtins.str'
1116+
_testAsyncioGatherPreciseType.py:11: error: Revealed type is 'asyncio.futures.Future[builtins.tuple[Any]]'
11151117

11161118
[case testMultipleInheritanceWorksWithTupleTypeGeneric]
11171119
from typing import SupportsAbs, NamedTuple

0 commit comments

Comments
 (0)