Skip to content

Commit 858c066

Browse files
committed
add note
1 parent c2f9482 commit 858c066

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

mypy/checkexpr.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1669,10 +1669,17 @@ def check_callable_call(
16691669
return callee.ret_type, callee
16701670

16711671
if callee.is_type_obj() and callee.type_object().is_protocol:
1672+
protocol_name = callee.type_object().name
16721673
self.chk.fail(
1673-
message_registry.CANNOT_INSTANTIATE_PROTOCOL.format(callee.type_object().name),
1674+
message_registry.CANNOT_INSTANTIATE_PROTOCOL.format(protocol_name),
16741675
context,
16751676
)
1677+
if callee.from_type_type:
1678+
self.chk.note(
1679+
f'Consider using "Callable[..., {protocol_name}]" '
1680+
f'instead of "type[{protocol_name}]"',
1681+
context,
1682+
)
16761683
elif (
16771684
callee.is_type_obj()
16781685
and callee.type_object().is_abstract

test-data/unit/check-protocols.test

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

16051605
def f(cls: Type[P]) -> P:
1606-
return cls() # E: Cannot instantiate protocol class "P"
1606+
return cls() # E: Cannot instantiate protocol class "P" \
1607+
# N: Consider using "Callable[..., P]" instead of "type[P]"
16071608
def g() -> P:
16081609
return P() # E: Cannot instantiate protocol class "P"
16091610

@@ -1625,7 +1626,8 @@ class C:
16251626
pass
16261627

16271628
def f(cls: Type[P]) -> P:
1628-
return cls() # E: Cannot instantiate protocol class "P"
1629+
return cls() # E: Cannot instantiate protocol class "P" \
1630+
# N: Consider using "Callable[..., P]" instead of "type[P]"
16291631

16301632
Alias = P
16311633
GoodAlias = C
@@ -1646,14 +1648,16 @@ class C:
16461648
pass
16471649

16481650
var: Type[P]
1649-
var() # E: Cannot instantiate protocol class "P"
1651+
var() # E: Cannot instantiate protocol class "P" \
1652+
# N: Consider using "Callable[..., P]" instead of "type[P]"
16501653
if int():
16511654
var = P # E: Can only assign concrete classes to a variable of type "Type[P]"
16521655
var = B # OK
16531656
var = C # OK
16541657

16551658
var_old = None # type: Type[P] # Old syntax for variable annotations
1656-
var_old() # E: Cannot instantiate protocol class "P"
1659+
var_old() # E: Cannot instantiate protocol class "P" \
1660+
# N: Consider using "Callable[..., P]" instead of "type[P]"
16571661
if int():
16581662
var_old = P # E: Can only assign concrete classes to a variable of type "Type[P]"
16591663
var_old = B # OK
@@ -1669,7 +1673,8 @@ class Logger:
16691673
class C(Protocol):
16701674
@classmethod
16711675
def action(cls) -> None:
1672-
cls() # E: Cannot instantiate protocol class "C"
1676+
cls() # E: Cannot instantiate protocol class "C" \
1677+
# N: Consider using "Callable[..., C]" instead of "type[C]"
16731678
Logger.log(cls) #OK for classmethods
16741679
[builtins fixtures/classmethod.pyi]
16751680

test-data/unit/check-selftype.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,8 @@ T = TypeVar('T', bound=HasX)
10231023
class Meta(type):
10241024
def do_x(cls: Type[T]) -> T:
10251025
cls.x
1026-
return cls() # E: Cannot instantiate protocol class "HasX"
1026+
return cls() # E: Cannot instantiate protocol class "HasX" \
1027+
# N: Consider using "Callable[..., HasX]" instead of "type[HasX]"
10271028

10281029
class Good(metaclass=Meta):
10291030
x: int

0 commit comments

Comments
 (0)