Skip to content

Fix couple inconsistencies in protocols vs TypeType #19267

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

Merged
merged 2 commits into from
Jun 11, 2025
Merged
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
2 changes: 1 addition & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7190,7 +7190,7 @@ def check_subtype(
if (
isinstance(supertype, Instance)
and supertype.type.is_protocol
and isinstance(subtype, (CallableType, Instance, TupleType, TypedDictType))
and isinstance(subtype, (CallableType, Instance, TupleType, TypedDictType, TypeType))
):
self.msg.report_protocol_problems(subtype, supertype, context, parent_error=error)
if isinstance(supertype, CallableType) and isinstance(subtype, Instance):
Expand Down
11 changes: 8 additions & 3 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3090,9 +3090,14 @@ def get_bad_protocol_flags(
assert right.type.is_protocol
all_flags: list[tuple[str, set[int], set[int]]] = []
for member in right.type.protocol_members:
if find_member(member, left, left):
item = (member, get_member_flags(member, left), get_member_flags(member, right))
all_flags.append(item)
if find_member(member, left, left, class_obj=class_obj):
all_flags.append(
(
member,
get_member_flags(member, left, class_obj=class_obj),
get_member_flags(member, right),
)
)
bad_flags = []
for name, subflags, superflags in all_flags:
if (
Expand Down
4 changes: 2 additions & 2 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1319,8 +1319,8 @@ def find_member(
is_lvalue=is_lvalue,
is_super=False,
is_operator=is_operator,
original_type=itype,
self_type=subtype,
original_type=TypeType.make_normalized(itype) if class_obj else itype,
self_type=TypeType.make_normalized(subtype) if class_obj else subtype,
context=Context(), # all errors are filtered, but this is a required argument
chk=type_checker,
suppress_errors=True,
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4552,6 +4552,32 @@ t = Test(Mock())
reveal_type(t) # N: Revealed type is "__main__.Test[Any]"
[builtins fixtures/dict.pyi]

[case testProtocolClassObjectDescriptor]
from typing import Any, Protocol, overload

class Desc:
@overload
def __get__(self, instance: None, owner: Any) -> Desc: ...
@overload
def __get__(self, instance: object, owner: Any) -> int: ...
def __get__(self, instance, owner):
pass

class HasDesc(Protocol):
attr: Desc

class HasInt(Protocol):
attr: int

class C:
attr = Desc()

x: HasInt = C()
y: HasDesc = C
z: HasInt = C # E: Incompatible types in assignment (expression has type "type[C]", variable has type "HasInt") \
# N: Following member(s) of "C" have conflicts: \
# N: attr: expected "int", got "Desc"

[case testProtocolErrorReportingNoDuplicates]
from typing import Callable, Protocol, TypeVar

Expand Down