Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Re-work overload overlap logic#17392

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ilevkivskyi merged 13 commits intopython:masterfromilevkivskyi:overload-overlap
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
4c5fc17
Re-work overload overlap logic
ilevkivskyiJun 16, 2024
fb427c0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]Jun 16, 2024
cc122e2
Another experiment
ilevkivskyiJun 17, 2024
7c10a21
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]Jun 17, 2024
cce3d00
Verify contravariance handling
ilevkivskyiJun 17, 2024
ce5ef00
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]Jun 17, 2024
3900386
Try a bit more pragmatic approach
ilevkivskyiJun 17, 2024
6f4c29d
Merge remote-tracking branch 'upstream/master' into overload-overlap
ilevkivskyiJun 17, 2024
5ee8826
Update tests
ilevkivskyiJun 17, 2024
05a2086
Add some comments, docs, and tests
ilevkivskyiJun 19, 2024
bf96e6d
Add flip order note
ilevkivskyiJun 19, 2024
c154ad1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]Jun 19, 2024
4f1bcf2
Merge branch 'master' into overload-overlap
hauntsaninjaJun 19, 2024
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
PrevPrevious commit
NextNext commit
Another experiment
  • Loading branch information
@ilevkivskyi
ilevkivskyi committedJun 17, 2024
commitcc122e245e3cc28ca385b78f2de7d6bd03f1e3d2
47 changes: 34 additions & 13 deletionsmypy/checker.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1788,7 +1788,7 @@ def is_unsafe_overlapping_op(

current_class = self.scope.active_class()
type_vars = current_class.defn.type_vars if current_class else []
return is_unsafe_overlapping_overload_signatures(first, second, type_vars)
return is_unsafe_overlapping_overload_signatures(first, second, type_vars, partial_only=False)

def check_inplace_operator_method(self, defn: FuncBase) -> None:
"""Check an inplace operator method such as __iadd__.
Expand DownExpand Up@@ -7848,7 +7848,8 @@ def expand_callable_variants(c: CallableType) -> list[CallableType]:


def is_unsafe_overlapping_overload_signatures(
signature: CallableType, other: CallableType, class_type_vars: list[TypeVarLikeType]
signature: CallableType, other: CallableType, class_type_vars: list[TypeVarLikeType],
partial_only: bool = True
) -> bool:
"""Check if two overloaded signatures are unsafely overlapping or partially overlapping.

Expand DownExpand Up@@ -7880,21 +7881,45 @@ def is_unsafe_overlapping_overload_signatures(

for sig_variant in expand_callable_variants(signature):
for other_variant in expand_callable_variants(other):
if is_callable_compatible(
if is_subset_no_promote(sig_variant.ret_type, other_variant.ret_type):
continue
if not (
is_callable_compatible(
sig_variant,
other_variant,
is_compat=is_overlapping_types_for_overload,
check_args_covariantly=False,
is_proper_subtype=False,
is_compat_return=lambda l, r: not is_subset_no_promote(l, r),
allow_partial_overlap=True,
) or is_callable_compatible(
other_variant,
sig_variant,
is_compat=is_overlapping_types_for_overload,
check_args_covariantly=True,
is_proper_subtype=False,
is_compat_return=lambda l, r: not is_subset_no_promote(r, l),
allow_partial_overlap=True,
)
):
continue
if not partial_only or (not is_callable_compatible(
sig_variant,
other_variant,
is_compat=is_overlapping_types_for_overload,
is_compat=is_subset_no_promote,
check_args_covariantly=False,
is_proper_subtype=False,
is_compat_return=lambda l, r: not is_subset_no_promote(l, r),
ignore_return=True,
allow_partial_overlap=True,
) or is_callable_compatible(
) ornotis_callable_compatible(
other_variant,
sig_variant,
is_compat=is_overlapping_types_for_overload,
is_compat=is_subset_no_promote,
check_args_covariantly=True,
is_proper_subtype=False,
is_compat_return=lambda l, r: not is_subset_no_promote(r, l),
ignore_return=True,
allow_partial_overlap=True,
):
)):
return True
return False

Expand DownExpand Up@@ -8391,10 +8416,6 @@ def is_subset_no_promote(left: Type, right: Type) -> bool:


def is_overlapping_types_for_overload(left: Type, right: Type) -> bool:
# For the purpose of unsafe overload checks we consider list[Never] and list[int]
# non-overlapping. This is consistent with how we treat list[int] and list[str] as
# non-overlapping, despite [] belongs to both. Also this will prevent false positives
# for failed type inference during unification.
return is_overlapping_types(
left,
right,
Expand Down
10 changes: 10 additions & 0 deletionstest-data/unit/check-overloading.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6705,3 +6705,13 @@ class B:
def f(self, *args, **kwargs):
pass
[builtins fixtures/tuple.pyi]

[case testSafeOverlapAllowed]
from lib import *
[file lib.pyi]
from typing import overload

@overload
def bar(x: object) -> object: ...
@overload
def bar(x: int = ...) -> int: ...

[8]ページ先頭

©2009-2025 Movatter.jp