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

Check tuples of abstract types#15366

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
sobolevn merged 5 commits intomasterfromissue-15264
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from1 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
NextNext commit
Check tuples of abstract types, refs#15264
  • Loading branch information
@sobolevn
sobolevn committedJun 4, 2023
commit88485fd3d5da80a95e7445b6fd94c3859fb827b4
28 changes: 19 additions & 9 deletionsmypy/checkexpr.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2166,15 +2166,7 @@ def check_arg(
if isinstance(caller_type, DeletedType):
self.msg.deleted_as_rvalue(caller_type, context)
# Only non-abstract non-protocol class can be given where Type[...] is expected...
elif (
isinstance(caller_type, CallableType)
and isinstance(callee_type, TypeType)
and caller_type.is_type_obj()
and (caller_type.type_object().is_abstract or caller_type.type_object().is_protocol)
and isinstance(callee_type.item, Instance)
and (callee_type.item.type.is_abstract or callee_type.item.type.is_protocol)
and not self.chk.allow_abstract_call
):
elif self.has_abstract_type_part(caller_type, callee_type):
self.msg.concrete_only_call(callee_type, context)
elif not is_subtype(caller_type, callee_type, options=self.chk.options):
code = self.msg.incompatible_argument(
Expand DownExpand Up@@ -5287,6 +5279,24 @@ def narrow_type_from_binder(
return narrow_declared_type(known_type, restriction)
return known_type

def has_abstract_type_part(
self, caller_type: ProperType, callee_type: ProperType
) -> ProperType | None:
if isinstance(caller_type, TupleType) and isinstance(callee_type, TupleType):
return any(
self.has_abstract_type_part(caller, callee)
for caller, callee in zip(caller_type.items, callee_type.items)
)
return (
isinstance(caller_type, CallableType)
and isinstance(callee_type, TypeType)
and caller_type.is_type_obj()
and (caller_type.type_object().is_abstract or caller_type.type_object().is_protocol)
and isinstance(callee_type.item, Instance)
and (callee_type.item.type.is_abstract or callee_type.item.type.is_protocol)
and not self.chk.allow_abstract_call
)


def has_any_type(t: Type, ignore_in_type_obj: bool = False) -> bool:
"""Whether t contains an Any type"""
Expand Down
18 changes: 18 additions & 0 deletionstest-data/unit/check-abstract.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -196,6 +196,24 @@ x: Type[B]
f(x) # OK
[out]

[case testAbstractTypeInADict]
from typing import Dict, Type
from abc import abstractmethod

class Class:
@abstractmethod
def method(self) -> None:
pass

my_dict_init: Dict[int, Type[Class]] = {0: Class} # E: Only concrete class can be given where "Tuple[int, Type[Class]]" is expected

class Child(Class):
def method(self) -> None: ...

other_dict_init: Dict[int, Type[Class]] = {0: Child} # ok
[builtins fixtures/dict.pyi]
[out]

[case testInstantiationAbstractsInTypeForAliases]
from typing import Type
from abc import abstractmethod
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp