Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-flags.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-typeform.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading