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

Require all overloads be either sync or async #16167

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 20 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ def visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:

def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
num_abstract = 0
num_async = 0
if not defn.items:
# In this case we have already complained about none of these being
# valid overloads.
Expand All @@ -643,8 +644,11 @@ def _visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
self.visit_decorator_inner(fdef, allow_empty=True)
if fdef.func.abstract_status in (IS_ABSTRACT, IMPLICITLY_ABSTRACT):
num_abstract += 1
if fdef.func.is_coroutine:
num_async += 1
if num_abstract not in (0, len(defn.items)):
self.fail(message_registry.INCONSISTENT_ABSTRACT_OVERLOAD, defn)
self.check_sync_or_async_overloads(defn, num_async)
if defn.impl:
defn.impl.accept(self)
if not defn.is_property:
Expand Down Expand Up @@ -698,6 +702,22 @@ def extract_callable_type(self, inner_type: Type | None, ctx: Context) -> Callab
self.msg.not_callable(inner_type, ctx)
return outer_type

def check_sync_or_async_overloads(self, defn: OverloadedFuncDef, num_async: int) -> None:
if isinstance(defn.impl, Decorator):
impl_type: bool | None = defn.impl.func.is_coroutine
elif isinstance(defn.impl, FuncDef):
impl_type = defn.impl.is_coroutine
else:
impl_type = None

len_items = len(defn.items)
if (
num_async not in (0, len_items)
or (num_async == 0 and impl_type is True)
or (num_async == len_items and impl_type is False)
):
self.fail(message_registry.INCONSISTENT_ASYNC_OVERLOAD.format(), defn)

def check_overlapping_overloads(self, defn: OverloadedFuncDef) -> None:
# At this point we should have set the impl already, and all remaining
# items are decorators
Expand Down
3 changes: 3 additions & 0 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
INCONSISTENT_ABSTRACT_OVERLOAD: Final = ErrorMessage(
"Overloaded method has both abstract and non-abstract variants"
)
INCONSISTENT_ASYNC_OVERLOAD: Final = ErrorMessage(
"All overloaded items but be either sync or async"
)
MULTIPLE_OVERLOADS_REQUIRED: Final = ErrorMessage("Single overload definition, multiple required")
READ_ONLY_PROPERTY_OVERRIDES_READ_WRITE: Final = ErrorMessage(
"Read-only property cannot override read-write property"
Expand Down
40 changes: 40 additions & 0 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -6674,3 +6674,43 @@ c2 = MyCallable("test")
reveal_type(c2) # N: Revealed type is "__main__.MyCallable[builtins.str]"
reveal_type(c2()) # should be int # N: Revealed type is "builtins.int"
[builtins fixtures/tuple.pyi]

[case testAllOverloadsAreEitherSyncOrAsync]
from typing import overload, Union

# Correct async:

@overload
async def iter_data1(s: str) -> str: ...
@overload
async def iter_data1(s: int) -> int: ...
async def iter_data1(s): ...

# Correct sync:
@overload
def iter_data2(s: str) -> str: ...
@overload
def iter_data2(s: int) -> int: ...
def iter_data2(s): ...

# Mixed items:
@overload # E: All overloaded items but be either sync or async
async def iter_data3(s: str) -> str: ...
@overload
def iter_data3(s: int) -> int: ...
def iter_data3(s): ...

# Wrong sync impl:
@overload # E: All overloaded items but be either sync or async
async def iter_data4(s: str) -> str: ...
@overload
async def iter_data4(s: int) -> int: ...
def iter_data4(s): ...

# Wrong async impl:
@overload # E: All overloaded items but be either sync or async
def iter_data5(s: str) -> str: ...
@overload
def iter_data5(s: int) -> int: ...
async def iter_data5(s): ...
[builtins fixtures/tuple.pyi]