From 45b565cbb5b21e16ef58d1c649f7bb1f96cbad24 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 12 Dec 2024 11:50:34 +0000 Subject: [PATCH] [red-knot] `Any` cannot be parameterized (#14933) --- .../resources/mdtest/annotations/any.md | 12 ++++++++++++ crates/red_knot_python_semantic/src/types/infer.rs | 12 +++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/red_knot_python_semantic/resources/mdtest/annotations/any.md b/crates/red_knot_python_semantic/resources/mdtest/annotations/any.md index 17e5a99b2a..86320c030a 100644 --- a/crates/red_knot_python_semantic/resources/mdtest/annotations/any.md +++ b/crates/red_knot_python_semantic/resources/mdtest/annotations/any.md @@ -69,3 +69,15 @@ y: int = Subclass() # error: [invalid-assignment] def _(s: Subclass): reveal_type(s) # revealed: Subclass ``` + +## Invalid + +`Any` cannot be parameterized: + +```py +from typing import Any + +# error: [invalid-type-parameter] "Type `typing.Any` expected no type parameter" +def f(x: Any[int]): + reveal_type(x) # revealed: Unknown +``` diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 432cc6de87..f50d4219ef 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -4863,7 +4863,17 @@ impl<'db> TypeInferenceBuilder<'db> { } KnownInstanceType::Type => self.infer_subclass_of_type_expression(parameters), KnownInstanceType::Tuple => self.infer_tuple_type_expression(parameters), - KnownInstanceType::Any => Type::Any, + KnownInstanceType::Any => { + self.diagnostics.add_lint( + &INVALID_TYPE_PARAMETER, + subscript.into(), + format_args!( + "Type `{}` expected no type parameter", + known_instance.repr(self.db) + ), + ); + Type::Unknown + } } }