From 3d1e5676fbb8fce0b4351a94289f54e22f0fefa4 Mon Sep 17 00:00:00 2001 From: David Peter Date: Mon, 31 Mar 2025 12:52:23 +0200 Subject: [PATCH] [red-knot] Add `ParamSpecArgs` and `ParamSpecKwargs` as `KnownClass` (#17086) ## Summary In preparation for #17017, where we will need them to suppress new false positives (once we understand the `ParamSpec.args`/`ParamSpec.kwargs` properties). ## Test Plan Tested on branch #17017 --- .../annotations/unsupported_special_forms.md | 2 +- crates/red_knot_python_semantic/src/types.rs | 6 ++--- .../src/types/class.rs | 23 ++++++++++++++++--- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/crates/red_knot_python_semantic/resources/mdtest/annotations/unsupported_special_forms.md b/crates/red_knot_python_semantic/resources/mdtest/annotations/unsupported_special_forms.md index bed5c4a56f..175ada1236 100644 --- a/crates/red_knot_python_semantic/resources/mdtest/annotations/unsupported_special_forms.md +++ b/crates/red_knot_python_semantic/resources/mdtest/annotations/unsupported_special_forms.md @@ -56,7 +56,7 @@ def _( reveal_type(d) # revealed: Unknown def foo(a_: e) -> None: - reveal_type(a_) # revealed: @Todo(Support for `typing.ParamSpec` instances in type expressions) + reveal_type(a_) # revealed: @Todo(Support for `typing.ParamSpec`) ``` ## Inheritance diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index c8b9c31182..d45ec530d8 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -3089,9 +3089,9 @@ impl<'db> Type<'db> { Some(KnownClass::TypeVar) => Ok(todo_type!( "Support for `typing.TypeVar` instances in type expressions" )), - Some(KnownClass::ParamSpec) => Ok(todo_type!( - "Support for `typing.ParamSpec` instances in type expressions" - )), + Some( + KnownClass::ParamSpec | KnownClass::ParamSpecArgs | KnownClass::ParamSpecKwargs, + ) => Ok(todo_type!("Support for `typing.ParamSpec`")), Some(KnownClass::TypeVarTuple) => Ok(todo_type!( "Support for `typing.TypeVarTuple` instances in type expressions" )), diff --git a/crates/red_knot_python_semantic/src/types/class.rs b/crates/red_knot_python_semantic/src/types/class.rs index fec4e8bf06..33e14acd24 100644 --- a/crates/red_knot_python_semantic/src/types/class.rs +++ b/crates/red_knot_python_semantic/src/types/class.rs @@ -844,6 +844,8 @@ pub enum KnownClass { SpecialForm, TypeVar, ParamSpec, + ParamSpecArgs, + ParamSpecKwargs, TypeVarTuple, TypeAliasType, NoDefaultType, @@ -893,6 +895,8 @@ impl<'db> KnownClass { | Self::TypeAliasType | Self::TypeVar | Self::ParamSpec + | Self::ParamSpecArgs + | Self::ParamSpecKwargs | Self::TypeVarTuple | Self::WrapperDescriptorType | Self::MethodWrapperType => Truthiness::AlwaysTrue, @@ -969,6 +973,8 @@ impl<'db> KnownClass { Self::SpecialForm => "_SpecialForm", Self::TypeVar => "TypeVar", Self::ParamSpec => "ParamSpec", + Self::ParamSpecArgs => "ParamSpecArgs", + Self::ParamSpecKwargs => "ParamSpecKwargs", Self::TypeVarTuple => "TypeVarTuple", Self::TypeAliasType => "TypeAliasType", Self::NoDefaultType => "_NoDefaultType", @@ -1149,9 +1155,12 @@ impl<'db> KnownClass { | Self::StdlibAlias | Self::SupportsIndex | Self::Sized => KnownModule::Typing, - Self::TypeAliasType | Self::TypeVarTuple | Self::ParamSpec | Self::NewType => { - KnownModule::TypingExtensions - } + Self::TypeAliasType + | Self::TypeVarTuple + | Self::ParamSpec + | Self::ParamSpecArgs + | Self::ParamSpecKwargs + | Self::NewType => KnownModule::TypingExtensions, Self::NoDefaultType => { let python_version = Program::get(db).python_version(db); @@ -1227,6 +1236,8 @@ impl<'db> KnownClass { | Self::StdlibAlias | Self::TypeVar | Self::ParamSpec + | Self::ParamSpecArgs + | Self::ParamSpecKwargs | Self::TypeVarTuple | Self::Sized | Self::Enum @@ -1282,6 +1293,8 @@ impl<'db> KnownClass { | Self::Classmethod | Self::TypeVar | Self::ParamSpec + | Self::ParamSpecArgs + | Self::ParamSpecKwargs | Self::TypeVarTuple | Self::Sized | Self::Enum @@ -1328,6 +1341,8 @@ impl<'db> KnownClass { "TypeAliasType" => Self::TypeAliasType, "TypeVar" => Self::TypeVar, "ParamSpec" => Self::ParamSpec, + "ParamSpecArgs" => Self::ParamSpecArgs, + "ParamSpecKwargs" => Self::ParamSpecKwargs, "TypeVarTuple" => Self::TypeVarTuple, "ChainMap" => Self::ChainMap, "Counter" => Self::Counter, @@ -1405,6 +1420,8 @@ impl<'db> KnownClass { | Self::NoDefaultType | Self::SupportsIndex | Self::ParamSpec + | Self::ParamSpecArgs + | Self::ParamSpecKwargs | Self::TypeVarTuple | Self::Sized | Self::NewType => matches!(module, KnownModule::Typing | KnownModule::TypingExtensions),