diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index a173025cda29..2d91fe6a1de5 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -391,6 +391,14 @@ def analyze_ref_expr(self, e: RefExpr, lvalue: bool = False) -> Type: result.ret_type.line = e.line result.ret_type.column = e.column if is_type_type_context(self.type_context[-1]): + if self.chk.options.disallow_any_generics and not self.chk.is_typeshed_stub: + fix_instance( + Instance(node, [], line=e.line, column=e.column), + self.msg.fail, + self.msg.note, + disallow_any=True, + options=self.chk.options, + ) # This is the type in a type[] expression, so substitute type # variables with Any. result = erasetype.erase_typevars(result) @@ -4988,7 +4996,9 @@ class LongName(Generic[T]): ... # For example: # A = List[Tuple[T, T]] # x = A() <- same as List[Tuple[Any, Any]], see PEP 484. - disallow_any = self.chk.options.disallow_any_generics and self.is_callee + disallow_any = self.chk.options.disallow_any_generics and ( + self.is_callee or is_type_type_context(self.type_context[-1]) + ) item = get_proper_type( set_any_tvars( alias, diff --git a/test-data/unit/check-flags.test b/test-data/unit/check-flags.test index 1e6105cbc49c..c63d7eaeba6b 100644 --- a/test-data/unit/check-flags.test +++ b/test-data/unit/check-flags.test @@ -2271,6 +2271,20 @@ foo(A) foo(A.foo) [builtins fixtures/classmethod.pyi] +[case testDisallowAnyGenericsForTypeObjectArguments] +# flags: --disallow-any-generics +from typing import TypeVar + +S = TypeVar("S") +T = TypeVar("T") +A = list[S] + +def f(arg: type[T]) -> None: + pass + +f(list) # E: Missing type arguments for generic type "list" +f(A) # E: Missing type arguments for generic type "A" + [case testDisallowSubclassingAny] # flags: --config-file tmp/mypy.ini import m diff --git a/test-data/unit/check-typeform.test b/test-data/unit/check-typeform.test index 6d59cb008111..361ebd9a9e38 100644 --- a/test-data/unit/check-typeform.test +++ b/test-data/unit/check-typeform.test @@ -682,6 +682,22 @@ else: [builtins fixtures/primitives.pyi] [typing fixtures/typing-full.pyi] +[case testDisallowAnyGenericsForTypeFormArguments] +# flags: --python-version 3.14 --enable-incomplete-feature=TypeForm --disallow-any-generics +from typing_extensions import TypeForm, TypeVar + +S = TypeVar("S") +T = TypeVar("T") +A = list[S] + +def accept_typeform(typx: TypeForm[T]) -> None: + pass + +accept_typeform(list) # E: Missing type arguments for generic type "list" +accept_typeform(A) # E: Missing type arguments for generic type "A" +[builtins fixtures/list.pyi] +[typing fixtures/typing-full.pyi] + -- Type Expressions Assignable To TypeForm Variable