Skip to content

Disallow calling type on Protocol #18095

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 14 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1802,10 +1802,22 @@ def check_callable_call(

if (
callee.is_type_obj()
and (len(arg_types) == 1)
and len(arg_types) == 1
and is_equivalent(callee.ret_type, self.named_type("builtins.type"))
):
callee = callee.copy_modified(ret_type=TypeType.make_normalized(arg_types[0]))
proper_arg = get_proper_type(arg_types[0])
if isinstance(proper_arg, Instance) and proper_arg.type.is_protocol:
callee = callee.copy_modified(
ret_type=UnionType(
[
self.named_type("builtins.type"),
self.named_type("types.ModuleType"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(sorry, I got confused myself, deleted & reposted)

self.named_type('types.ModuleType') isn't the correct type here:

import types
assert type(types) is types.ModuleType

When you call type(some_module), you obtain types.ModuleType class itself, not its instance. So this union isn't type | types.ModuleType | Any, it should be type | type[ModuleType] | Any, and at this point type[ModuleType] member is redundant (it's also a type, isn't it?).

AnyType(TypeOfAny.special_form),
]
)
)
else:
callee = callee.copy_modified(ret_type=TypeType.make_normalized(arg_types[0]))

if callable_node:
# Store the inferred callable type.
Expand Down
29 changes: 29 additions & 0 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -4215,3 +4215,32 @@ def g4(a: Input[bytes], b: Output[str]) -> None:
f(a, b) # E: Cannot infer type argument 1 of "f"

[builtins fixtures/tuple.pyi]

[case testTypeCallOnProtocol]
from typing import Any, Protocol, cast
import types

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

import mod

a: P = mod
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: ...

[builtins fixtures/tuple.pyi]
Loading