Skip to content

Commit c2f9482

Browse files
committed
Remove TypeType exception for protocol instantiation
This disallows direct instantiation of `cls: type[Proto]` Discussed in https://discuss.python.org/t/compatibility-of-protocol-class-object-with-type-t-and-type-any/48442/2 Users should use `Callable[..., Proto]` instead. This helps with `__init__` unsoundness. Subset of python#18094, which also touched abstract classes.
1 parent fa01a07 commit c2f9482

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,12 +1668,7 @@ def check_callable_call(
16681668
# An Enum() call that failed SemanticAnalyzerPass2.check_enum_call().
16691669
return callee.ret_type, callee
16701670

1671-
if (
1672-
callee.is_type_obj()
1673-
and callee.type_object().is_protocol
1674-
# Exception for Type[...]
1675-
and not callee.from_type_type
1676-
):
1671+
if callee.is_type_obj() and callee.type_object().is_protocol:
16771672
self.chk.fail(
16781673
message_registry.CANNOT_INSTANTIATE_PROTOCOL.format(callee.type_object().name),
16791674
context,

test-data/unit/check-protocols.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ class C:
16031603
pass
16041604

16051605
def f(cls: Type[P]) -> P:
1606-
return cls() # OK
1606+
return cls() # E: Cannot instantiate protocol class "P"
16071607
def g() -> P:
16081608
return P() # E: Cannot instantiate protocol class "P"
16091609

@@ -1625,7 +1625,7 @@ class C:
16251625
pass
16261626

16271627
def f(cls: Type[P]) -> P:
1628-
return cls() # OK
1628+
return cls() # E: Cannot instantiate protocol class "P"
16291629

16301630
Alias = P
16311631
GoodAlias = C
@@ -1646,14 +1646,14 @@ class C:
16461646
pass
16471647

16481648
var: Type[P]
1649-
var()
1649+
var() # E: Cannot instantiate protocol class "P"
16501650
if int():
16511651
var = P # E: Can only assign concrete classes to a variable of type "Type[P]"
16521652
var = B # OK
16531653
var = C # OK
16541654

16551655
var_old = None # type: Type[P] # Old syntax for variable annotations
1656-
var_old()
1656+
var_old() # E: Cannot instantiate protocol class "P"
16571657
if int():
16581658
var_old = P # E: Can only assign concrete classes to a variable of type "Type[P]"
16591659
var_old = B # OK
@@ -1669,7 +1669,7 @@ class Logger:
16691669
class C(Protocol):
16701670
@classmethod
16711671
def action(cls) -> None:
1672-
cls() #OK for classmethods
1672+
cls() # E: Cannot instantiate protocol class "C"
16731673
Logger.log(cls) #OK for classmethods
16741674
[builtins fixtures/classmethod.pyi]
16751675

test-data/unit/check-selftype.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ T = TypeVar('T', bound=HasX)
10231023
class Meta(type):
10241024
def do_x(cls: Type[T]) -> T:
10251025
cls.x
1026-
return cls()
1026+
return cls() # E: Cannot instantiate protocol class "HasX"
10271027

10281028
class Good(metaclass=Meta):
10291029
x: int

0 commit comments

Comments
 (0)