diff --git a/crates/ty_python_semantic/src/place.rs b/crates/ty_python_semantic/src/place.rs index 589a4720f4..6e46819b14 100644 --- a/crates/ty_python_semantic/src/place.rs +++ b/crates/ty_python_semantic/src/place.rs @@ -1509,6 +1509,7 @@ mod implicit_globals { "__annotate__" if Program::get(db).python_version(db) >= PythonVersion::PY314 => { let signature = Signature::new( Parameters::new( + db, [Parameter::positional_only(Some(Name::new_static("format"))) .with_annotated_type(KnownClass::Int.to_instance(db))], ), diff --git a/crates/ty_python_semantic/src/types.rs b/crates/ty_python_semantic/src/types.rs index 01385a776d..2b509ce6dc 100644 --- a/crates/ty_python_semantic/src/types.rs +++ b/crates/ty_python_semantic/src/types.rs @@ -1829,8 +1829,11 @@ impl<'db> Type<'db> { Some(CallableTypes::one(CallableType::single( db, Signature::new( - Parameters::new([Parameter::positional_only(None) - .with_annotated_type(newtype.base(db).instance_type(db))]), + Parameters::new( + db, + [Parameter::positional_only(None) + .with_annotated_type(newtype.base(db).instance_type(db))], + ), Some(Type::NewTypeInstance(newtype)), ), ))) @@ -5505,8 +5508,11 @@ impl<'db> Type<'db> { Type::DataclassTransformer(_) => Binding::single( self, Signature::new( - Parameters::new([Parameter::positional_only(Some(Name::new_static("func"))) - .with_annotated_type(Type::object())]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("func"))) + .with_annotated_type(Type::object())], + ), None, ), ) @@ -5521,14 +5527,17 @@ impl<'db> Type<'db> { ) => Binding::single( self, Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("a"))) - .type_form() - .with_annotated_type(Type::any()), - Parameter::positional_only(Some(Name::new_static("b"))) - .type_form() - .with_annotated_type(Type::any()), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("a"))) + .type_form() + .with_annotated_type(Type::any()), + Parameter::positional_only(Some(Name::new_static("b"))) + .type_form() + .with_annotated_type(Type::any()), + ], + ), Some(KnownClass::ConstraintSet.to_instance(db)), ), ) @@ -5538,11 +5547,12 @@ impl<'db> Type<'db> { Binding::single( self, Signature::new( - Parameters::new([Parameter::positional_only(Some(Name::new_static( - "a", - ))) - .type_form() - .with_annotated_type(Type::any())]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("a"))) + .type_form() + .with_annotated_type(Type::any())], + ), Some(KnownClass::Bool.to_instance(db)), ), ) @@ -5557,13 +5567,16 @@ impl<'db> Type<'db> { self, Signature::new_generic( Some(GenericContext::from_typevar_instances(db, [val_ty])), - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("value"))) - .with_annotated_type(Type::TypeVar(val_ty)), - Parameter::positional_only(Some(Name::new_static("type"))) - .type_form() - .with_annotated_type(Type::any()), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("value"))) + .with_annotated_type(Type::TypeVar(val_ty)), + Parameter::positional_only(Some(Name::new_static("type"))) + .type_form() + .with_annotated_type(Type::any()), + ], + ), Some(Type::TypeVar(val_ty)), ), ) @@ -5574,14 +5587,15 @@ impl<'db> Type<'db> { Binding::single( self, Signature::new( - Parameters::new([Parameter::positional_only(Some(Name::new_static( - "arg", - ))) - // We need to set the type to `Any` here (instead of `Never`), - // in order for every `assert_never` call to pass the argument - // check. If we set it to `Never`, we'll get invalid-argument-type - // errors instead of `type-assertion-failure` errors. - .with_annotated_type(Type::any())]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("arg"))) + // We need to set the type to `Any` here (instead of `Never`), + // in order for every `assert_never` call to pass the argument + // check. If we set it to `Never`, we'll get invalid-argument-type + // errors instead of `type-assertion-failure` errors. + .with_annotated_type(Type::any())], + ), Some(Type::none(db)), ), ) @@ -5591,13 +5605,16 @@ impl<'db> Type<'db> { Some(KnownFunction::Cast) => Binding::single( self, Signature::new( - Parameters::new([ - Parameter::positional_or_keyword(Name::new_static("typ")) - .type_form() - .with_annotated_type(Type::any()), - Parameter::positional_or_keyword(Name::new_static("val")) - .with_annotated_type(Type::any()), - ]), + Parameters::new( + db, + [ + Parameter::positional_or_keyword(Name::new_static("typ")) + .type_form() + .with_annotated_type(Type::any()), + Parameter::positional_or_keyword(Name::new_static("val")) + .with_annotated_type(Type::any()), + ], + ), Some(Type::any()), ), ) @@ -5609,18 +5626,20 @@ impl<'db> Type<'db> { [ // def dataclass(cls: None, /) -> Callable[[type[_T]], type[_T]]: ... Signature::new( - Parameters::new([Parameter::positional_only(Some( - Name::new_static("cls"), - )) - .with_annotated_type(Type::none(db))]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("cls"))) + .with_annotated_type(Type::none(db))], + ), None, ), // def dataclass(cls: type[_T], /) -> type[_T]: ... Signature::new( - Parameters::new([Parameter::positional_only(Some( - Name::new_static("cls"), - )) - .with_annotated_type(KnownClass::Type.to_instance(db))]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("cls"))) + .with_annotated_type(KnownClass::Type.to_instance(db))], + ), None, ), // TODO: make this overload Python-version-dependent @@ -5639,38 +5658,41 @@ impl<'db> Type<'db> { // weakref_slot: bool = False, // ) -> Callable[[type[_T]], type[_T]]: ... Signature::new( - Parameters::new([ - Parameter::keyword_only(Name::new_static("init")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(true)), - Parameter::keyword_only(Name::new_static("repr")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(true)), - Parameter::keyword_only(Name::new_static("eq")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(true)), - Parameter::keyword_only(Name::new_static("order")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(false)), - Parameter::keyword_only(Name::new_static("unsafe_hash")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(false)), - Parameter::keyword_only(Name::new_static("frozen")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(false)), - Parameter::keyword_only(Name::new_static("match_args")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(true)), - Parameter::keyword_only(Name::new_static("kw_only")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(false)), - Parameter::keyword_only(Name::new_static("slots")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(false)), - Parameter::keyword_only(Name::new_static("weakref_slot")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(false)), - ]), + Parameters::new( + db, + [ + Parameter::keyword_only(Name::new_static("init")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(true)), + Parameter::keyword_only(Name::new_static("repr")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(true)), + Parameter::keyword_only(Name::new_static("eq")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(true)), + Parameter::keyword_only(Name::new_static("order")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(false)), + Parameter::keyword_only(Name::new_static("unsafe_hash")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(false)), + Parameter::keyword_only(Name::new_static("frozen")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(false)), + Parameter::keyword_only(Name::new_static("match_args")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(true)), + Parameter::keyword_only(Name::new_static("kw_only")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(false)), + Parameter::keyword_only(Name::new_static("slots")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(false)), + Parameter::keyword_only(Name::new_static("weakref_slot")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(false)), + ], + ), None, ), ], @@ -5700,11 +5722,12 @@ impl<'db> Type<'db> { Binding::single( self, Signature::new( - Parameters::new([Parameter::positional_only(Some(Name::new_static( - "o", - ))) - .with_annotated_type(Type::any()) - .with_default_type(Type::BooleanLiteral(false))]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("o"))) + .with_annotated_type(Type::any()) + .with_default_type(Type::BooleanLiteral(false))], + ), Some(KnownClass::Bool.to_instance(db)), ), ) @@ -5723,16 +5746,21 @@ impl<'db> Type<'db> { self, [ Signature::new( - Parameters::new([Parameter::positional_or_keyword( - Name::new_static("object"), - ) - .with_annotated_type(Type::object()) - .with_default_type(Type::string_literal(db, ""))]), + Parameters::new( + db, + [Parameter::positional_or_keyword(Name::new_static("object")) + .with_annotated_type(Type::object()) + .with_default_type(Type::string_literal(db, ""))], + ), Some(KnownClass::Str.to_instance(db)), ), Signature::new( - Parameters::new([ - Parameter::positional_or_keyword(Name::new_static("object")) + Parameters::new( + db, + [ + Parameter::positional_or_keyword(Name::new_static( + "object", + )) // TODO: Should be `ReadableBuffer` instead of this union type: .with_annotated_type(UnionType::from_elements( db, @@ -5742,13 +5770,18 @@ impl<'db> Type<'db> { ], )) .with_default_type(Type::bytes_literal(db, b"")), - Parameter::positional_or_keyword(Name::new_static("encoding")) + Parameter::positional_or_keyword(Name::new_static( + "encoding", + )) .with_annotated_type(KnownClass::Str.to_instance(db)) .with_default_type(Type::string_literal(db, "utf-8")), - Parameter::positional_or_keyword(Name::new_static("errors")) + Parameter::positional_or_keyword(Name::new_static( + "errors", + )) .with_annotated_type(KnownClass::Str.to_instance(db)) .with_default_type(Type::string_literal(db, "strict")), - ]), + ], + ), Some(KnownClass::Str.to_instance(db)), ), ], @@ -5771,29 +5804,33 @@ impl<'db> Type<'db> { self, [ Signature::new( - Parameters::new([Parameter::positional_only(Some( - Name::new_static("o"), - )) - .with_annotated_type(Type::any())]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("o"))) + .with_annotated_type(Type::any())], + ), Some(type_instance), ), Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("name"))) - .with_annotated_type(str_instance), - Parameter::positional_only(Some(Name::new_static("bases"))) - .with_annotated_type(Type::homogeneous_tuple( - db, - type_instance, - )), - Parameter::positional_only(Some(Name::new_static("dict"))) - .with_annotated_type( - KnownClass::Dict.to_specialized_instance( + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("name"))) + .with_annotated_type(str_instance), + Parameter::positional_only(Some(Name::new_static("bases"))) + .with_annotated_type(Type::homogeneous_tuple( db, - [str_instance, Type::any()], + type_instance, + )), + Parameter::positional_only(Some(Name::new_static("dict"))) + .with_annotated_type( + KnownClass::Dict.to_specialized_instance( + db, + [str_instance, Type::any()], + ), ), - ), - ]), + ], + ), Some(type_instance), ), ], @@ -5832,19 +5869,23 @@ impl<'db> Type<'db> { self, [ Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("t"))) - .with_annotated_type(Type::any()), - Parameter::positional_only(Some(Name::new_static("obj"))) - .with_annotated_type(Type::any()), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("t"))) + .with_annotated_type(Type::any()), + Parameter::positional_only(Some(Name::new_static("obj"))) + .with_annotated_type(Type::any()), + ], + ), Some(KnownClass::Super.to_instance(db)), ), Signature::new( - Parameters::new([Parameter::positional_only(Some( - Name::new_static("t"), - )) - .with_annotated_type(Type::any())]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("t"))) + .with_annotated_type(Type::any())], + ), Some(KnownClass::Super.to_instance(db)), ), Signature::new( @@ -5871,24 +5912,27 @@ impl<'db> Type<'db> { Binding::single( self, Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("message"))) - .with_annotated_type(Type::LiteralString), - Parameter::keyword_only(Name::new_static("category")) - .with_annotated_type(UnionType::from_elements( - db, - [ - // TODO: should be `type[Warning]` - Type::any(), - KnownClass::NoneType.to_instance(db), - ], - )) - // TODO: should be `type[Warning]` - .with_default_type(Type::any()), - Parameter::keyword_only(Name::new_static("stacklevel")) - .with_annotated_type(KnownClass::Int.to_instance(db)) - .with_default_type(Type::IntLiteral(1)), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("message"))) + .with_annotated_type(Type::LiteralString), + Parameter::keyword_only(Name::new_static("category")) + .with_annotated_type(UnionType::from_elements( + db, + [ + // TODO: should be `type[Warning]` + Type::any(), + KnownClass::NoneType.to_instance(db), + ], + )) + // TODO: should be `type[Warning]` + .with_default_type(Type::any()), + Parameter::keyword_only(Name::new_static("stacklevel")) + .with_annotated_type(KnownClass::Int.to_instance(db)) + .with_default_type(Type::IntLiteral(1)), + ], + ), Some(KnownClass::Deprecated.to_instance(db)), ), ) @@ -5908,26 +5952,29 @@ impl<'db> Type<'db> { Binding::single( self, Signature::new( - Parameters::new([ - Parameter::positional_or_keyword(Name::new_static("name")) - .with_annotated_type(KnownClass::Str.to_instance(db)), - Parameter::positional_or_keyword(Name::new_static("value")) - .with_annotated_type(Type::any()) - .type_form(), - Parameter::keyword_only(Name::new_static("type_params")) - .with_annotated_type(Type::homogeneous_tuple( - db, - UnionType::from_elements( + Parameters::new( + db, + [ + Parameter::positional_or_keyword(Name::new_static("name")) + .with_annotated_type(KnownClass::Str.to_instance(db)), + Parameter::positional_or_keyword(Name::new_static("value")) + .with_annotated_type(Type::any()) + .type_form(), + Parameter::keyword_only(Name::new_static("type_params")) + .with_annotated_type(Type::homogeneous_tuple( db, - [ - KnownClass::TypeVar.to_instance(db), - KnownClass::ParamSpec.to_instance(db), - KnownClass::TypeVarTuple.to_instance(db), - ], - ), - )) - .with_default_type(Type::empty_tuple(db)), - ]), + UnionType::from_elements( + db, + [ + KnownClass::TypeVar.to_instance(db), + KnownClass::ParamSpec.to_instance(db), + KnownClass::TypeVarTuple.to_instance(db), + ], + ), + )) + .with_default_type(Type::empty_tuple(db)), + ], + ), None, ), ) @@ -5936,63 +5983,71 @@ impl<'db> Type<'db> { Some(KnownClass::Property) => { let getter_signature = Signature::new( - Parameters::new([ - Parameter::positional_only(None).with_annotated_type(Type::any()) - ]), + Parameters::new( + db, + [Parameter::positional_only(None).with_annotated_type(Type::any())], + ), Some(Type::any()), ); let setter_signature = Signature::new( - Parameters::new([ - Parameter::positional_only(None).with_annotated_type(Type::any()), - Parameter::positional_only(None).with_annotated_type(Type::any()), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(None).with_annotated_type(Type::any()), + Parameter::positional_only(None).with_annotated_type(Type::any()), + ], + ), Some(Type::none(db)), ); let deleter_signature = Signature::new( - Parameters::new([ - Parameter::positional_only(None).with_annotated_type(Type::any()) - ]), + Parameters::new( + db, + [Parameter::positional_only(None).with_annotated_type(Type::any())], + ), Some(Type::any()), ); Binding::single( self, Signature::new( - Parameters::new([ - Parameter::positional_or_keyword(Name::new_static("fget")) - .with_annotated_type(UnionType::from_elements( - db, - [ - Type::single_callable(db, getter_signature), - Type::none(db), - ], - )) - .with_default_type(Type::none(db)), - Parameter::positional_or_keyword(Name::new_static("fset")) - .with_annotated_type(UnionType::from_elements( - db, - [ - Type::single_callable(db, setter_signature), - Type::none(db), - ], - )) - .with_default_type(Type::none(db)), - Parameter::positional_or_keyword(Name::new_static("fdel")) - .with_annotated_type(UnionType::from_elements( - db, - [ - Type::single_callable(db, deleter_signature), - Type::none(db), - ], - )) - .with_default_type(Type::none(db)), - Parameter::positional_or_keyword(Name::new_static("doc")) - .with_annotated_type(UnionType::from_elements( - db, - [KnownClass::Str.to_instance(db), Type::none(db)], - )) - .with_default_type(Type::none(db)), - ]), + Parameters::new( + db, + [ + Parameter::positional_or_keyword(Name::new_static("fget")) + .with_annotated_type(UnionType::from_elements( + db, + [ + Type::single_callable(db, getter_signature), + Type::none(db), + ], + )) + .with_default_type(Type::none(db)), + Parameter::positional_or_keyword(Name::new_static("fset")) + .with_annotated_type(UnionType::from_elements( + db, + [ + Type::single_callable(db, setter_signature), + Type::none(db), + ], + )) + .with_default_type(Type::none(db)), + Parameter::positional_or_keyword(Name::new_static("fdel")) + .with_annotated_type(UnionType::from_elements( + db, + [ + Type::single_callable(db, deleter_signature), + Type::none(db), + ], + )) + .with_default_type(Type::none(db)), + Parameter::positional_or_keyword(Name::new_static("doc")) + .with_annotated_type(UnionType::from_elements( + db, + [KnownClass::Str.to_instance(db), Type::none(db)], + )) + .with_default_type(Type::none(db)), + ], + ), None, ), ) @@ -6014,12 +6069,15 @@ impl<'db> Type<'db> { [ Signature::new(Parameters::empty(), Some(Type::empty_tuple(db))), Signature::new( - Parameters::new([Parameter::positional_only(Some( - Name::new_static("iterable"), - )) - .with_annotated_type( - KnownClass::Iterable.to_specialized_instance(db, [object]), - )]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static( + "iterable", + ))) + .with_annotated_type( + KnownClass::Iterable.to_specialized_instance(db, [object]), + )], + ), Some(Type::homogeneous_tuple(db, object)), ), ], @@ -6047,19 +6105,22 @@ impl<'db> Type<'db> { Binding::single( self, Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("typename"))) - .with_annotated_type(KnownClass::Str.to_instance(db)), - Parameter::positional_only(Some(Name::new_static("fields"))) - .with_annotated_type(KnownClass::Dict.to_instance(db)) - .with_default_type(Type::any()), - Parameter::keyword_only(Name::new_static("total")) - .with_annotated_type(KnownClass::Bool.to_instance(db)) - .with_default_type(Type::BooleanLiteral(true)), - // Future compatibility, in case new keyword arguments will be added: - Parameter::keyword_variadic(Name::new_static("kwargs")) - .with_annotated_type(Type::any()), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("typename"))) + .with_annotated_type(KnownClass::Str.to_instance(db)), + Parameter::positional_only(Some(Name::new_static("fields"))) + .with_annotated_type(KnownClass::Dict.to_instance(db)) + .with_default_type(Type::any()), + Parameter::keyword_only(Name::new_static("total")) + .with_annotated_type(KnownClass::Bool.to_instance(db)) + .with_default_type(Type::BooleanLiteral(true)), + // Future compatibility, in case new keyword arguments will be added: + Parameter::keyword_variadic(Name::new_static("kwargs")) + .with_annotated_type(Type::any()), + ], + ), None, ), ) @@ -6154,8 +6215,11 @@ impl<'db> Type<'db> { Type::KnownInstance(KnownInstanceType::NewType(newtype)) => Binding::single( self, Signature::new( - Parameters::new([Parameter::positional_only(None) - .with_annotated_type(newtype.base(db).instance_type(db))]), + Parameters::new( + db, + [Parameter::positional_only(None) + .with_annotated_type(newtype.base(db).instance_type(db))], + ), Some(Type::NewTypeInstance(newtype)), ), ) @@ -12084,25 +12148,31 @@ impl<'db> KnownBoundMethodType<'db> { | KnownBoundMethodType::PropertyDunderGet(_) => Either::Left(Either::Left( [ Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("instance"))) - .with_annotated_type(Type::none(db)), - Parameter::positional_only(Some(Name::new_static("owner"))) - .with_annotated_type(KnownClass::Type.to_instance(db)), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("instance"))) + .with_annotated_type(Type::none(db)), + Parameter::positional_only(Some(Name::new_static("owner"))) + .with_annotated_type(KnownClass::Type.to_instance(db)), + ], + ), None, ), Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("instance"))) - .with_annotated_type(Type::object()), - Parameter::positional_only(Some(Name::new_static("owner"))) - .with_annotated_type(UnionType::from_elements( - db, - [KnownClass::Type.to_instance(db), Type::none(db)], - )) - .with_default_type(Type::none(db)), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("instance"))) + .with_annotated_type(Type::object()), + Parameter::positional_only(Some(Name::new_static("owner"))) + .with_annotated_type(UnionType::from_elements( + db, + [KnownClass::Type.to_instance(db), Type::none(db)], + )) + .with_default_type(Type::none(db)), + ], + ), None, ), ] @@ -12113,56 +12183,68 @@ impl<'db> KnownBoundMethodType<'db> { )), KnownBoundMethodType::PropertyDunderSet(_) => { Either::Right(std::iter::once(Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("instance"))) - .with_annotated_type(Type::object()), - Parameter::positional_only(Some(Name::new_static("value"))) - .with_annotated_type(Type::object()), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("instance"))) + .with_annotated_type(Type::object()), + Parameter::positional_only(Some(Name::new_static("value"))) + .with_annotated_type(Type::object()), + ], + ), None, ))) } KnownBoundMethodType::StrStartswith(_) => { Either::Right(std::iter::once(Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("prefix"))) - .with_annotated_type(UnionType::from_elements( - db, - [ - KnownClass::Str.to_instance(db), - Type::homogeneous_tuple(db, KnownClass::Str.to_instance(db)), - ], - )), - Parameter::positional_only(Some(Name::new_static("start"))) - .with_annotated_type(UnionType::from_elements( - db, - [KnownClass::SupportsIndex.to_instance(db), Type::none(db)], - )) - .with_default_type(Type::none(db)), - Parameter::positional_only(Some(Name::new_static("end"))) - .with_annotated_type(UnionType::from_elements( - db, - [KnownClass::SupportsIndex.to_instance(db), Type::none(db)], - )) - .with_default_type(Type::none(db)), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("prefix"))) + .with_annotated_type(UnionType::from_elements( + db, + [ + KnownClass::Str.to_instance(db), + Type::homogeneous_tuple( + db, + KnownClass::Str.to_instance(db), + ), + ], + )), + Parameter::positional_only(Some(Name::new_static("start"))) + .with_annotated_type(UnionType::from_elements( + db, + [KnownClass::SupportsIndex.to_instance(db), Type::none(db)], + )) + .with_default_type(Type::none(db)), + Parameter::positional_only(Some(Name::new_static("end"))) + .with_annotated_type(UnionType::from_elements( + db, + [KnownClass::SupportsIndex.to_instance(db), Type::none(db)], + )) + .with_default_type(Type::none(db)), + ], + ), Some(KnownClass::Bool.to_instance(db)), ))) } KnownBoundMethodType::ConstraintSetRange => { Either::Right(std::iter::once(Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("lower_bound"))) - .type_form() - .with_annotated_type(Type::any()), - Parameter::positional_only(Some(Name::new_static("typevar"))) - .type_form() - .with_annotated_type(Type::any()), - Parameter::positional_only(Some(Name::new_static("upper_bound"))) - .type_form() - .with_annotated_type(Type::any()), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("lower_bound"))) + .type_form() + .with_annotated_type(Type::any()), + Parameter::positional_only(Some(Name::new_static("typevar"))) + .type_form() + .with_annotated_type(Type::any()), + Parameter::positional_only(Some(Name::new_static("upper_bound"))) + .type_form() + .with_annotated_type(Type::any()), + ], + ), Some(KnownClass::ConstraintSet.to_instance(db)), ))) } @@ -12177,45 +12259,57 @@ impl<'db> KnownBoundMethodType<'db> { KnownBoundMethodType::ConstraintSetImpliesSubtypeOf(_) => { Either::Right(std::iter::once(Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("ty"))) - .type_form() - .with_annotated_type(Type::any()), - Parameter::positional_only(Some(Name::new_static("of"))) - .type_form() - .with_annotated_type(Type::any()), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("ty"))) + .type_form() + .with_annotated_type(Type::any()), + Parameter::positional_only(Some(Name::new_static("of"))) + .type_form() + .with_annotated_type(Type::any()), + ], + ), Some(KnownClass::ConstraintSet.to_instance(db)), ))) } KnownBoundMethodType::ConstraintSetSatisfies(_) => { Either::Right(std::iter::once(Signature::new( - Parameters::new([Parameter::positional_only(Some(Name::new_static("other"))) - .with_annotated_type(KnownClass::ConstraintSet.to_instance(db))]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("other"))) + .with_annotated_type(KnownClass::ConstraintSet.to_instance(db))], + ), Some(KnownClass::ConstraintSet.to_instance(db)), ))) } KnownBoundMethodType::ConstraintSetSatisfiedByAllTypeVars(_) => { Either::Right(std::iter::once(Signature::new( - Parameters::new([Parameter::keyword_only(Name::new_static("inferable")) - .type_form() - .with_annotated_type(UnionType::from_elements( - db, - [Type::homogeneous_tuple(db, Type::any()), Type::none(db)], - )) - .with_default_type(Type::none(db))]), + Parameters::new( + db, + [Parameter::keyword_only(Name::new_static("inferable")) + .type_form() + .with_annotated_type(UnionType::from_elements( + db, + [Type::homogeneous_tuple(db, Type::any()), Type::none(db)], + )) + .with_default_type(Type::none(db))], + ), Some(KnownClass::Bool.to_instance(db)), ))) } KnownBoundMethodType::GenericContextSpecializeConstrained(_) => { Either::Right(std::iter::once(Signature::new( - Parameters::new([Parameter::positional_only(Some(Name::new_static( - "constraints", - ))) - .with_annotated_type(KnownClass::ConstraintSet.to_instance(db))]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("constraints"))) + .with_annotated_type(KnownClass::ConstraintSet.to_instance(db)), + ], + ), Some(UnionType::from_elements( db, [KnownClass::Specialization.to_instance(db), Type::none(db)], @@ -12255,29 +12349,35 @@ impl WrapperDescriptorKind { let descriptor = class.to_instance(db); [ Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(descriptor), - Parameter::positional_only(Some(Name::new_static("instance"))) - .with_annotated_type(none), - Parameter::positional_only(Some(Name::new_static("owner"))) - .with_annotated_type(type_instance), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(descriptor), + Parameter::positional_only(Some(Name::new_static("instance"))) + .with_annotated_type(none), + Parameter::positional_only(Some(Name::new_static("owner"))) + .with_annotated_type(type_instance), + ], + ), None, ), Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(descriptor), - Parameter::positional_only(Some(Name::new_static("instance"))) - .with_annotated_type(Type::object()), - Parameter::positional_only(Some(Name::new_static("owner"))) - .with_annotated_type(UnionType::from_elements( - db, - [type_instance, none], - )) - .with_default_type(none), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(descriptor), + Parameter::positional_only(Some(Name::new_static("instance"))) + .with_annotated_type(Type::object()), + Parameter::positional_only(Some(Name::new_static("owner"))) + .with_annotated_type(UnionType::from_elements( + db, + [type_instance, none], + )) + .with_default_type(none), + ], + ), None, ), ] @@ -12293,14 +12393,17 @@ impl WrapperDescriptorKind { WrapperDescriptorKind::PropertyDunderSet => { let object = Type::object(); Either::Right(std::iter::once(Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(KnownClass::Property.to_instance(db)), - Parameter::positional_only(Some(Name::new_static("instance"))) - .with_annotated_type(object), - Parameter::positional_only(Some(Name::new_static("value"))) - .with_annotated_type(object), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(KnownClass::Property.to_instance(db)), + Parameter::positional_only(Some(Name::new_static("instance"))) + .with_annotated_type(object), + Parameter::positional_only(Some(Name::new_static("value"))) + .with_annotated_type(object), + ], + ), None, ))) } diff --git a/crates/ty_python_semantic/src/types/class.rs b/crates/ty_python_semantic/src/types/class.rs index 033f326c39..26a7a2c42c 100644 --- a/crates/ty_python_semantic/src/types/class.rs +++ b/crates/ty_python_semantic/src/types/class.rs @@ -808,13 +808,14 @@ impl<'db> ClassType<'db> { name: &str, ) -> Member<'db> { fn synthesize_getitem_overload_signature<'db>( + db: &'db dyn Db, index_annotation: Type<'db>, return_annotation: Type<'db>, ) -> Signature<'db> { let self_parameter = Parameter::positional_only(Some(Name::new_static("self"))); let index_parameter = Parameter::positional_only(Some(Name::new_static("index"))) .with_annotated_type(index_annotation); - let parameters = Parameters::new([self_parameter, index_parameter]); + let parameters = Parameters::new(db, [self_parameter, index_parameter]); Signature::new(parameters, Some(return_annotation)) } @@ -845,9 +846,11 @@ impl<'db> ClassType<'db> { .map(Type::IntLiteral) .unwrap_or_else(|| KnownClass::Int.to_instance(db)); - let parameters = - Parameters::new([Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(Type::instance(db, self))]); + let parameters = Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(Type::instance(db, self))], + ); let synthesized_dunder_method = Type::function_like_callable(db, Signature::new(parameters, Some(return_type))); @@ -975,6 +978,7 @@ impl<'db> ClassType<'db> { ); Some(synthesize_getitem_overload_signature( + db, index_annotation, return_type, )) @@ -992,11 +996,13 @@ impl<'db> ClassType<'db> { // __getitem__(self, index: slice[Any, Any, Any], /) -> tuple[str | float | bytes, ...] // overload_signatures.push(synthesize_getitem_overload_signature( + db, KnownClass::SupportsIndex.to_instance(db), all_elements_unioned, )); overload_signatures.push(synthesize_getitem_overload_signature( + db, KnownClass::Slice.to_instance(db), Type::homogeneous_tuple(db, all_elements_unioned), )); @@ -1066,11 +1072,14 @@ impl<'db> ClassType<'db> { iterable_parameter.with_default_type(Type::empty_tuple(db)); } - let parameters = Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(SubclassOfType::from(db, self)), - iterable_parameter, - ]); + let parameters = Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(SubclassOfType::from(db, self)), + iterable_parameter, + ], + ); let synthesized_dunder = Type::function_like_callable( db, @@ -2212,7 +2221,10 @@ impl<'db> ClassLiteral<'db> { .get(name) { let property_getter_signature = Signature::new( - Parameters::new([Parameter::positional_only(Some(Name::new_static("self")))]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("self")))], + ), Some(field.declared_ty), ); let property_getter = Type::single_callable(db, property_getter_signature); @@ -2424,10 +2436,10 @@ impl<'db> ClassLiteral<'db> { let signature = match name { "__new__" | "__init__" => Signature::new_generic( inherited_generic_context.or_else(|| self.inherited_generic_context(db)), - Parameters::new(parameters), + Parameters::new(db, parameters), return_ty, ), - _ => Signature::new(Parameters::new(parameters), return_ty), + _ => Signature::new(Parameters::new(db, parameters), return_ty), }; Some(Type::function_like_callable(db, signature)) }; @@ -2454,14 +2466,17 @@ impl<'db> ClassLiteral<'db> { } let signature = Signature::new( - Parameters::new([ - Parameter::positional_or_keyword(Name::new_static("self")) - // TODO: could be `Self`. - .with_annotated_type(instance_ty), - Parameter::positional_or_keyword(Name::new_static("other")) - // TODO: could be `Self`. - .with_annotated_type(instance_ty), - ]), + Parameters::new( + db, + [ + Parameter::positional_or_keyword(Name::new_static("self")) + // TODO: could be `Self`. + .with_annotated_type(instance_ty), + Parameter::positional_or_keyword(Name::new_static("other")) + // TODO: could be `Self`. + .with_annotated_type(instance_ty), + ], + ), Some(KnownClass::Bool.to_instance(db)), ); @@ -2474,10 +2489,11 @@ impl<'db> ClassLiteral<'db> { if unsafe_hash || (frozen && eq) { let signature = Signature::new( - Parameters::new([Parameter::positional_or_keyword(Name::new_static( - "self", - )) - .with_annotated_type(instance_ty)]), + Parameters::new( + db, + [Parameter::positional_or_keyword(Name::new_static("self")) + .with_annotated_type(instance_ty)], + ), Some(KnownClass::Int.to_instance(db)), ); @@ -2559,12 +2575,15 @@ impl<'db> ClassLiteral<'db> { (CodeGeneratorKind::DataclassLike(_), "__setattr__") => { if has_dataclass_param(DataclassFlags::FROZEN) { let signature = Signature::new( - Parameters::new([ - Parameter::positional_or_keyword(Name::new_static("self")) - .with_annotated_type(instance_ty), - Parameter::positional_or_keyword(Name::new_static("name")), - Parameter::positional_or_keyword(Name::new_static("value")), - ]), + Parameters::new( + db, + [ + Parameter::positional_or_keyword(Name::new_static("self")) + .with_annotated_type(instance_ty), + Parameter::positional_or_keyword(Name::new_static("name")), + Parameter::positional_or_keyword(Name::new_static("value")), + ], + ), Some(Type::Never), ); @@ -2599,14 +2618,17 @@ impl<'db> ClassLiteral<'db> { return Some(Type::Callable(CallableType::new( db, CallableSignature::single(Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(Type::Never), - Parameter::positional_only(Some(Name::new_static("value"))) - .with_annotated_type(Type::any()), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(Type::Never), + Parameter::positional_only(Some(Name::new_static("value"))) + .with_annotated_type(Type::any()), + ], + ), Some(Type::none(db)), )), true, @@ -2617,14 +2639,17 @@ impl<'db> ClassLiteral<'db> { let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str())); Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(key_type), - Parameter::positional_only(Some(Name::new_static("value"))) - .with_annotated_type(field.declared_ty), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(key_type), + Parameter::positional_only(Some(Name::new_static("value"))) + .with_annotated_type(field.declared_ty), + ], + ), Some(Type::none(db)), ) }); @@ -2643,12 +2668,15 @@ impl<'db> ClassLiteral<'db> { let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str())); Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(key_type), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(key_type), + ], + ), Some(field.declared_ty), ) }); @@ -2675,12 +2703,15 @@ impl<'db> ClassLiteral<'db> { // once the generics solver takes default arguments into account. let get_sig = Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(key_type), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(key_type), + ], + ), Some(if field.is_required() { field.declared_ty } else { @@ -2693,14 +2724,17 @@ impl<'db> ClassLiteral<'db> { let get_with_default_sig = Signature::new_generic( Some(GenericContext::from_typevar_instances(db, [t_default])), - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(key_type), - Parameter::positional_only(Some(Name::new_static("default"))) - .with_annotated_type(Type::TypeVar(t_default)), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(key_type), + Parameter::positional_only(Some(Name::new_static("default"))) + .with_annotated_type(Type::TypeVar(t_default)), + ], + ), Some(if field.is_required() { field.declared_ty } else { @@ -2716,12 +2750,15 @@ impl<'db> ClassLiteral<'db> { // Fallback overloads for unknown keys .chain(std::iter::once({ Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(KnownClass::Str.to_instance(db)), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(KnownClass::Str.to_instance(db)), + ], + ), Some(UnionType::from_elements( db, [Type::unknown(), Type::none(db)], @@ -2734,14 +2771,17 @@ impl<'db> ClassLiteral<'db> { Signature::new_generic( Some(GenericContext::from_typevar_instances(db, [t_default])), - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(KnownClass::Str.to_instance(db)), - Parameter::positional_only(Some(Name::new_static("default"))) - .with_annotated_type(Type::TypeVar(t_default)), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(KnownClass::Str.to_instance(db)), + Parameter::positional_only(Some(Name::new_static("default"))) + .with_annotated_type(Type::TypeVar(t_default)), + ], + ), Some(UnionType::from_elements( db, [Type::unknown(), Type::TypeVar(t_default)], @@ -2771,12 +2811,15 @@ impl<'db> ClassLiteral<'db> { // `.pop()` without default let pop_sig = Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(key_type), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(key_type), + ], + ), Some(field.declared_ty), ); @@ -2786,14 +2829,17 @@ impl<'db> ClassLiteral<'db> { let pop_with_default_sig = Signature::new_generic( Some(GenericContext::from_typevar_instances(db, [t_default])), - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(key_type), - Parameter::positional_only(Some(Name::new_static("default"))) - .with_annotated_type(Type::TypeVar(t_default)), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(key_type), + Parameter::positional_only(Some(Name::new_static("default"))) + .with_annotated_type(Type::TypeVar(t_default)), + ], + ), Some(UnionType::from_elements( db, [field.declared_ty, Type::TypeVar(t_default)], @@ -2816,14 +2862,17 @@ impl<'db> ClassLiteral<'db> { // `setdefault` always returns the field type Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::positional_only(Some(Name::new_static("key"))) - .with_annotated_type(key_type), - Parameter::positional_only(Some(Name::new_static("default"))) - .with_annotated_type(field.declared_ty), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::positional_only(Some(Name::new_static("key"))) + .with_annotated_type(key_type), + Parameter::positional_only(Some(Name::new_static("default"))) + .with_annotated_type(field.declared_ty), + ], + ), Some(field.declared_ty), ) }); @@ -2837,12 +2886,15 @@ impl<'db> ClassLiteral<'db> { (CodeGeneratorKind::TypedDict, "update") => { // TODO: synthesize a set of overloads with precise types let signature = Signature::new( - Parameters::new([ - Parameter::positional_only(Some(Name::new_static("self"))) - .with_annotated_type(instance_ty), - Parameter::variadic(Name::new_static("args")), - Parameter::keyword_variadic(Name::new_static("kwargs")), - ]), + Parameters::new( + db, + [ + Parameter::positional_only(Some(Name::new_static("self"))) + .with_annotated_type(instance_ty), + Parameter::variadic(Name::new_static("args")), + Parameter::keyword_variadic(Name::new_static("kwargs")), + ], + ), Some(Type::none(db)), ); diff --git a/crates/ty_python_semantic/src/types/display.rs b/crates/ty_python_semantic/src/types/display.rs index 428a65ca9c..217e0f9a7c 100644 --- a/crates/ty_python_semantic/src/types/display.rs +++ b/crates/ty_python_semantic/src/types/display.rs @@ -2302,21 +2302,21 @@ mod tests { } fn display_signature<'db>( - db: &dyn Db, + db: &'db dyn Db, parameters: impl IntoIterator>, return_ty: Option>, ) -> String { - Signature::new(Parameters::new(parameters), return_ty) + Signature::new(Parameters::new(db, parameters), return_ty) .display(db) .to_string() } fn display_signature_multiline<'db>( - db: &dyn Db, + db: &'db dyn Db, parameters: impl IntoIterator>, return_ty: Option>, ) -> String { - Signature::new(Parameters::new(parameters), return_ty) + Signature::new(Parameters::new(db, parameters), return_ty) .display_with(db, super::DisplaySettings::default().multiline()) .to_string() } diff --git a/crates/ty_python_semantic/src/types/infer/builder.rs b/crates/ty_python_semantic/src/types/infer/builder.rs index 68b35bec62..af98132015 100644 --- a/crates/ty_python_semantic/src/types/infer/builder.rs +++ b/crates/ty_python_semantic/src/types/infer/builder.rs @@ -3361,9 +3361,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { // TODO: `Unpack` Parameters::todo() } else { - Parameters::new(parameter_types.iter().map(|param_type| { - Parameter::positional_only(None).with_annotated_type(*param_type) - })) + Parameters::new( + self.db(), + parameter_types.iter().map(|param_type| { + Parameter::positional_only(None).with_annotated_type(*param_type) + }), + ) }; Type::single_callable(self.db(), Signature::new(parameters, None)) @@ -7993,6 +7996,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> { .map(|param| Parameter::keyword_variadic(param.name().id.clone())); Parameters::new( + self.db(), positional_only .into_iter() .chain(positional_or_keyword) diff --git a/crates/ty_python_semantic/src/types/infer/builder/type_expression.rs b/crates/ty_python_semantic/src/types/infer/builder/type_expression.rs index 14a4079eb4..af23bc646e 100644 --- a/crates/ty_python_semantic/src/types/infer/builder/type_expression.rs +++ b/crates/ty_python_semantic/src/types/infer/builder/type_expression.rs @@ -1636,9 +1636,12 @@ impl<'db> TypeInferenceBuilder<'db, '_> { // TODO: `Unpack` Parameters::todo() } else { - Parameters::new(parameter_types.iter().map(|param_type| { - Parameter::positional_only(None).with_annotated_type(*param_type) - })) + Parameters::new( + self.db(), + parameter_types.iter().map(|param_type| { + Parameter::positional_only(None).with_annotated_type(*param_type) + }), + ) }); } ast::Expr::Subscript(subscript) => { diff --git a/crates/ty_python_semantic/src/types/property_tests/type_generation.rs b/crates/ty_python_semantic/src/types/property_tests/type_generation.rs index 58808e48c2..ecc274546a 100644 --- a/crates/ty_python_semantic/src/types/property_tests/type_generation.rs +++ b/crates/ty_python_semantic/src/types/property_tests/type_generation.rs @@ -76,24 +76,29 @@ impl CallableParams { pub(crate) fn into_parameters(self, db: &TestDb) -> Parameters<'_> { match self { CallableParams::GradualForm => Parameters::gradual_form(), - CallableParams::List(params) => Parameters::new(params.into_iter().map(|param| { - let mut parameter = match param.kind { - ParamKind::PositionalOnly => Parameter::positional_only(param.name), - ParamKind::PositionalOrKeyword => { - Parameter::positional_or_keyword(param.name.unwrap()) + CallableParams::List(params) => Parameters::new( + db, + params.into_iter().map(|param| { + let mut parameter = match param.kind { + ParamKind::PositionalOnly => Parameter::positional_only(param.name), + ParamKind::PositionalOrKeyword => { + Parameter::positional_or_keyword(param.name.unwrap()) + } + ParamKind::Variadic => Parameter::variadic(param.name.unwrap()), + ParamKind::KeywordOnly => Parameter::keyword_only(param.name.unwrap()), + ParamKind::KeywordVariadic => { + Parameter::keyword_variadic(param.name.unwrap()) + } + }; + if let Some(annotated_ty) = param.annotated_ty { + parameter = parameter.with_annotated_type(annotated_ty.into_type(db)); } - ParamKind::Variadic => Parameter::variadic(param.name.unwrap()), - ParamKind::KeywordOnly => Parameter::keyword_only(param.name.unwrap()), - ParamKind::KeywordVariadic => Parameter::keyword_variadic(param.name.unwrap()), - }; - if let Some(annotated_ty) = param.annotated_ty { - parameter = parameter.with_annotated_type(annotated_ty.into_type(db)); - } - if let Some(default_ty) = param.default_ty { - parameter = parameter.with_default_type(default_ty.into_type(db)); - } - parameter - })), + if let Some(default_ty) = param.default_ty { + parameter = parameter.with_default_type(default_ty.into_type(db)); + } + parameter + }), + ), } } } diff --git a/crates/ty_python_semantic/src/types/protocol_class.rs b/crates/ty_python_semantic/src/types/protocol_class.rs index 48b76bf98d..45ef664a2f 100644 --- a/crates/ty_python_semantic/src/types/protocol_class.rs +++ b/crates/ty_python_semantic/src/types/protocol_class.rs @@ -212,7 +212,10 @@ impl<'db> ProtocolInterface<'db> { // Synthesize a read-only property (one that has a getter but no setter) // which returns the specified type from its getter. let property_getter_signature = Signature::new( - Parameters::new([Parameter::positional_only(Some(Name::new_static("self")))]), + Parameters::new( + db, + [Parameter::positional_only(Some(Name::new_static("self")))], + ), Some(ty.normalized(db)), ); let property_getter = Type::single_callable(db, property_getter_signature); diff --git a/crates/ty_python_semantic/src/types/signatures.rs b/crates/ty_python_semantic/src/types/signatures.rs index ebe2945693..612a7cf324 100644 --- a/crates/ty_python_semantic/src/types/signatures.rs +++ b/crates/ty_python_semantic/src/types/signatures.rs @@ -558,11 +558,12 @@ impl<'db> Signature<'db> { // Discard the definition when normalizing, so that two equivalent signatures // with different `Definition`s share the same Salsa ID when normalized definition: None, - parameters: self - .parameters - .iter() - .map(|param| param.normalized_impl(db, visitor)) - .collect(), + parameters: Parameters::new( + db, + self.parameters + .iter() + .map(|param| param.normalized_impl(db, visitor)), + ), return_ty: self .return_ty .map(|return_ty| return_ty.normalized_impl(db, visitor)), @@ -587,14 +588,17 @@ impl<'db> Signature<'db> { ), None => None, }; + let parameters = { + let mut parameters = Vec::with_capacity(self.parameters.len()); + for param in &self.parameters { + parameters.push(param.recursive_type_normalized_impl(db, div, nested, visitor)?); + } + Parameters::new(db, parameters) + }; Some(Self { generic_context: self.generic_context, definition: self.definition, - parameters: self - .parameters - .iter() - .map(|param| param.recursive_type_normalized_impl(db, div, nested, visitor)) - .collect::>()?, + parameters, return_ty, }) } @@ -662,7 +666,7 @@ impl<'db> Signature<'db> { parameters.next(); } - let mut parameters = Parameters::new(parameters); + let mut parameters = Parameters::new(db, parameters); let mut return_ty = self.return_ty; if let Some(self_type) = self_type { parameters = parameters.apply_type_mapping_impl( @@ -1344,7 +1348,10 @@ pub(crate) struct Parameters<'db> { } impl<'db> Parameters<'db> { - pub(crate) fn new(parameters: impl IntoIterator>) -> Self { + pub(crate) fn new( + _db: &'db dyn Db, + parameters: impl IntoIterator>, + ) -> Self { let value: Vec> = parameters.into_iter().collect(); let is_gradual = value.len() == 2 && value @@ -1598,6 +1605,7 @@ impl<'db> Parameters<'db> { }); Self::new( + db, positional_only .into_iter() .chain(positional_or_keyword) @@ -1715,12 +1723,6 @@ impl<'db, 'a> IntoIterator for &'a Parameters<'db> { } } -impl<'db> FromIterator> for Parameters<'db> { - fn from_iter>>(iter: T) -> Self { - Self::new(iter) - } -} - impl<'db> std::ops::Index for Parameters<'db> { type Output = Parameter<'db>;