Skip to content

Commit 76c4cea

Browse files
committed
Disallow any generic in type type context
Resolves #21145
1 parent e5cd1f9 commit 76c4cea

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

mypy/checkexpr.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,14 @@ def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type:
391391
result.ret_type.line = e.line
392392
result.ret_type.column = e.column
393393
if is_type_type_context(self.type_context[-1]):
394+
if self.chk.options.disallow_any_generics and not self.chk.is_typeshed_stub:
395+
fix_instance(
396+
Instance(node, [], line=e.line, column=e.column),
397+
self.msg.fail,
398+
self.msg.note,
399+
disallow_any=True,
400+
options=self.chk.options,
401+
)
394402
# This is the type in a type[] expression, so substitute type
395403
# variables with Any.
396404
result = erasetype.erase_typevars(result)
@@ -4988,7 +4996,9 @@ class LongName(Generic[T]): ...
49884996
# For example:
49894997
# A = List[Tuple[T, T]]
49904998
# x = A() <- same as List[Tuple[Any, Any]], see PEP 484.
4991-
disallow_any = self.chk.options.disallow_any_generics and self.is_callee
4999+
disallow_any = self.chk.options.disallow_any_generics and (
5000+
self.is_callee or is_type_type_context(self.type_context[-1])
5001+
)
49925002
item = get_proper_type(
49935003
set_any_tvars(
49945004
alias,

test-data/unit/check-flags.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,6 +2271,20 @@ foo(A)
22712271
foo(A.foo)
22722272
[builtins fixtures/classmethod.pyi]
22732273

2274+
[case testDisallowAnyGenericsForTypeObjectArguments]
2275+
# flags: --disallow-any-generics
2276+
from typing import TypeVar
2277+
2278+
S = TypeVar("S")
2279+
T = TypeVar("T")
2280+
A = list[S]
2281+
2282+
def f(arg: type[T]) -> None:
2283+
pass
2284+
2285+
f(list) # E: Missing type arguments for generic type "list"
2286+
f(A) # E: Missing type arguments for generic type "A"
2287+
22742288
[case testDisallowSubclassingAny]
22752289
# flags: --config-file tmp/mypy.ini
22762290
import m

test-data/unit/check-typeform.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,22 @@ else:
682682
[builtins fixtures/primitives.pyi]
683683
[typing fixtures/typing-full.pyi]
684684

685+
[case testDisallowAnyGenericsForTypeFormArguments]
686+
# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm --disallow-any-generics
687+
from typing_extensions import TypeForm, TypeVar
688+
689+
S = TypeVar("S")
690+
T = TypeVar("T")
691+
A = list[S]
692+
693+
def accept_typeform(typx: TypeForm[T]) -> None:
694+
pass
695+
696+
accept_typeform(list) # E: Missing type arguments for generic type "list"
697+
accept_typeform(A) # E: Missing type arguments for generic type "A"
698+
[builtins fixtures/list.pyi]
699+
[typing fixtures/typing-full.pyi]
700+
685701

686702
-- Type Expressions Assignable To TypeForm Variable
687703

0 commit comments

Comments
 (0)