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

Basic support for decorated overloads #15898

Merged
merged 6 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Try more principled way to detect decorated overloads
  • Loading branch information
ilevkivskyi committed Aug 17, 2023
commit c34521d77c16aafd7cf788e19bb97dfc286f800f
10 changes: 8 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,9 +1153,15 @@ def analyze_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
elif not non_overload_indexes:
self.handle_missing_overload_implementation(defn)

if types and (not isinstance(defn.impl, Decorator) or not defn.impl.decorators):
if types and not any(
# If some overload items are decorated with other decorators, then
# the overload type will be determined during type checking.
isinstance(it, Decorator) and len(it.decorators) > 1 for it in defn.items
):
# TODO: should we enforce decorated overloads consistency somehow?
# TODO: how to support decorated overloads in stubs without major slow-down?
# Some existing code uses both styles:
# * Put decorator only on implementation, use "effective" types in overloads
# * Put decorator everywhere, use "bare" types in overloads.
defn.type = Overloaded(types)
defn.type.line = defn.line

Expand Down
3 changes: 1 addition & 2 deletions test-data/unit/check-newsemanal.test
Original file line number Diff line number Diff line change
Expand Up @@ -3207,8 +3207,7 @@ class User:
self.first_name = value

def __init__(self, name: str) -> None:
self.name = name # E: Cannot assign to a method \
# E: Cannot determine type of "name"
self.name = name # E: Cannot assign to a method

[case testNewAnalyzerMemberNameMatchesTypedDict]
from typing import Union, Any
Expand Down