Skip to content

Commit

Permalink
okay, fallback because of primer hits
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Nov 3, 2024
1 parent afa72b9 commit 2dc29e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
11 changes: 9 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1807,8 +1807,15 @@ def check_callable_call(
):
proper_arg = get_proper_type(arg_types[0])
if isinstance(proper_arg, Instance) and proper_arg.type.is_protocol:
self.msg.fail("Calling type() on a protocol class is unsound", context)
callee = callee.copy_modified(ret_type=self.named_type("builtins.object"))
callee = callee.copy_modified(
ret_type=UnionType(
[
self.named_type("builtins.type"),
self.named_type("types.ModuleType"),
AnyType(TypeOfAny.special_form),
]
)
)
else:
callee = callee.copy_modified(ret_type=TypeType.make_normalized(arg_types[0]))

Expand Down
22 changes: 16 additions & 6 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4217,19 +4217,29 @@ def g4(a: Input[bytes], b: Output[str]) -> None:
[builtins fixtures/tuple.pyi]

[case testTypeCallOnProtocol]
from typing import Protocol

import mod
from typing import Any, Protocol, cast
import types

class P(Protocol):
def foo(self) -> None: ...

import mod

a: P = mod
value = type(a) # E: Calling type() on a protocol class is unsound
reveal_type(value) # N: Revealed type is "builtins.object"
reveal_type(value.foo) # E: "object" has no attribute "foo" \
value = type(a)
reveal_type(value) # N: Revealed type is "Union[builtins.type, types.ModuleType, Any]"
reveal_type(value.foo) # E: Item "type" of "Union[type, Module, Any]" has no attribute "foo" \
# N: Revealed type is "Any"

class Namespace: ...
n = Namespace()
n.foo = lambda: None # E: "Namespace" has no attribute "foo"

b: P = cast(Any, n)
value = type(b)
reveal_type(value) # N: Revealed type is "Union[builtins.type, types.ModuleType, Any]"
reveal_type(value.foo) # E: Item "type" of "Union[type, Module, Any]" has no attribute "foo" \
# N: Revealed type is "Any"
[file mod.py]
def foo() -> None: ...

Expand Down

0 comments on commit 2dc29e6

Please sign in to comment.