Skip to content

Commit 68ab69c

Browse files
authored
Suggest to use a protocol instead of a module (#13861)
1 parent 186876c commit 68ab69c

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

mypy/typeanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,8 @@ def analyze_unbound_type_without_type_info(
761761
else:
762762
notes.append('Perhaps you need "Callable[...]" or a callback protocol?')
763763
elif isinstance(sym.node, MypyFile):
764-
# TODO: suggest a protocol when supported.
765764
message = 'Module "{}" is not valid as a type'
765+
notes.append("Perhaps you meant to use a protocol matching the module structure?")
766766
elif unbound_tvar:
767767
message = 'Type variable "{}" is unbound'
768768
short = name.split(".")[-1]

test-data/unit/check-basic.test

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,8 @@ from typing import Union
350350
class A: ...
351351
class B: ...
352352

353-
x: Union[mock, A] # E: Module "mock" is not valid as a type
353+
x: Union[mock, A] # E: Module "mock" is not valid as a type \
354+
# N: Perhaps you meant to use a protocol matching the module structure?
354355

355356
if isinstance(x, B):
356357
pass
@@ -366,7 +367,8 @@ from typing import overload, Any, Union
366367
@overload
367368
def f(x: int) -> int: ...
368369
@overload
369-
def f(x: str) -> Union[mock, str]: ... # E: Module "mock" is not valid as a type
370+
def f(x: str) -> Union[mock, str]: ... # E: Module "mock" is not valid as a type \
371+
# N: Perhaps you meant to use a protocol matching the module structure?
370372
def f(x):
371373
pass
372374

test-data/unit/check-errorcodes.test

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ x: f # E: Function "__main__.f" is not valid as a type [valid-type] \
245245
# N: Perhaps you need "Callable[...]" or a callback protocol?
246246

247247
import sys
248-
y: sys # E: Module "sys" is not valid as a type [valid-type]
248+
y: sys # E: Module "sys" is not valid as a type [valid-type] \
249+
# N: Perhaps you meant to use a protocol matching the module structure?
249250
z: y # E: Variable "__main__.y" is not valid as a type [valid-type] \
250251
# N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
251252
[builtins fixtures/tuple.pyi]

test-data/unit/fine-grained.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5576,9 +5576,12 @@ import T
55765576
==
55775577
main:4: error: "C" expects no type arguments, but 1 given
55785578
main:4: error: Module "T" is not valid as a type
5579+
main:4: note: Perhaps you meant to use a protocol matching the module structure?
55795580
main:6: error: Free type variable expected in Generic[...]
55805581
main:7: error: Module "T" is not valid as a type
5582+
main:7: note: Perhaps you meant to use a protocol matching the module structure?
55815583
main:10: error: Module "T" is not valid as a type
5584+
main:10: note: Perhaps you meant to use a protocol matching the module structure?
55825585
main:10: error: Bad number of arguments for type alias, expected: 0, given: 1
55835586

55845587
[case testChangeClassToModule]
@@ -5602,8 +5605,10 @@ import C
56025605
==
56035606
==
56045607
main:3: error: Module "C" is not valid as a type
5608+
main:3: note: Perhaps you meant to use a protocol matching the module structure?
56055609
main:5: error: Module not callable
56065610
main:8: error: Module "C" is not valid as a type
5611+
main:8: note: Perhaps you meant to use a protocol matching the module structure?
56075612

56085613
[case testChangeTypeVarToTypeAlias]
56095614

@@ -5653,8 +5658,10 @@ import D
56535658
==
56545659
==
56555660
main:3: error: Module "D" is not valid as a type
5661+
main:3: note: Perhaps you meant to use a protocol matching the module structure?
56565662
main:5: error: Module not callable
56575663
main:8: error: Module "D" is not valid as a type
5664+
main:8: note: Perhaps you meant to use a protocol matching the module structure?
56585665

56595666
[case testChangeTypeAliasToModuleUnqualified]
56605667

@@ -5680,8 +5687,10 @@ import D
56805687
==
56815688
==
56825689
main:3: error: Module "D" is not valid as a type
5690+
main:3: note: Perhaps you meant to use a protocol matching the module structure?
56835691
main:5: error: Module not callable
56845692
main:8: error: Module "D" is not valid as a type
5693+
main:8: note: Perhaps you meant to use a protocol matching the module structure?
56855694

56865695
[case testChangeFunctionToVariableAndRefreshUsingStaleDependency]
56875696
import a

0 commit comments

Comments
 (0)