[ty] Add `db` parameter to `Parameters::new` method (#21674)

## Summary

This PR adds a new `db` parameter to `Parameters::new` for
https://github.com/astral-sh/ruff/pull/21445. This change creates a
large diff so thought to split it out as it's just a mechanical change.

The `Parameters::new` method not only creates the `Parameters` but also
analyses the parameters to check what kind it is. For `ParamSpec`
support, it's going to require the `db` to check whether the annotated
type is `ParamSpec` or not. For the current set of parameters that isn't
required because it's only checking whether it's dynamic or not which
doesn't require `db`.
This commit is contained in:
Dhruv Manilawala 2025-11-28 17:59:58 +05:30 committed by GitHub
parent 3ed537e9f1
commit 98681b9356
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 656 additions and 483 deletions

View File

@ -1509,6 +1509,7 @@ mod implicit_globals {
"__annotate__" if Program::get(db).python_version(db) >= PythonVersion::PY314 => { "__annotate__" if Program::get(db).python_version(db) >= PythonVersion::PY314 => {
let signature = Signature::new( let signature = Signature::new(
Parameters::new( Parameters::new(
db,
[Parameter::positional_only(Some(Name::new_static("format"))) [Parameter::positional_only(Some(Name::new_static("format")))
.with_annotated_type(KnownClass::Int.to_instance(db))], .with_annotated_type(KnownClass::Int.to_instance(db))],
), ),

View File

@ -1829,8 +1829,11 @@ impl<'db> Type<'db> {
Some(CallableTypes::one(CallableType::single( Some(CallableTypes::one(CallableType::single(
db, db,
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(None) Parameters::new(
.with_annotated_type(newtype.base(db).instance_type(db))]), db,
[Parameter::positional_only(None)
.with_annotated_type(newtype.base(db).instance_type(db))],
),
Some(Type::NewTypeInstance(newtype)), Some(Type::NewTypeInstance(newtype)),
), ),
))) )))
@ -5505,8 +5508,11 @@ impl<'db> Type<'db> {
Type::DataclassTransformer(_) => Binding::single( Type::DataclassTransformer(_) => Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(Some(Name::new_static("func"))) Parameters::new(
.with_annotated_type(Type::object())]), db,
[Parameter::positional_only(Some(Name::new_static("func")))
.with_annotated_type(Type::object())],
),
None, None,
), ),
) )
@ -5521,14 +5527,17 @@ impl<'db> Type<'db> {
) => Binding::single( ) => Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("a"))) db,
.type_form() [
.with_annotated_type(Type::any()), Parameter::positional_only(Some(Name::new_static("a")))
Parameter::positional_only(Some(Name::new_static("b"))) .type_form()
.type_form() .with_annotated_type(Type::any()),
.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)), Some(KnownClass::ConstraintSet.to_instance(db)),
), ),
) )
@ -5538,11 +5547,12 @@ impl<'db> Type<'db> {
Binding::single( Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(Some(Name::new_static( Parameters::new(
"a", db,
))) [Parameter::positional_only(Some(Name::new_static("a")))
.type_form() .type_form()
.with_annotated_type(Type::any())]), .with_annotated_type(Type::any())],
),
Some(KnownClass::Bool.to_instance(db)), Some(KnownClass::Bool.to_instance(db)),
), ),
) )
@ -5557,13 +5567,16 @@ impl<'db> Type<'db> {
self, self,
Signature::new_generic( Signature::new_generic(
Some(GenericContext::from_typevar_instances(db, [val_ty])), Some(GenericContext::from_typevar_instances(db, [val_ty])),
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("value"))) db,
.with_annotated_type(Type::TypeVar(val_ty)), [
Parameter::positional_only(Some(Name::new_static("type"))) Parameter::positional_only(Some(Name::new_static("value")))
.type_form() .with_annotated_type(Type::TypeVar(val_ty)),
.with_annotated_type(Type::any()), Parameter::positional_only(Some(Name::new_static("type")))
]), .type_form()
.with_annotated_type(Type::any()),
],
),
Some(Type::TypeVar(val_ty)), Some(Type::TypeVar(val_ty)),
), ),
) )
@ -5574,14 +5587,15 @@ impl<'db> Type<'db> {
Binding::single( Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(Some(Name::new_static( Parameters::new(
"arg", db,
))) [Parameter::positional_only(Some(Name::new_static("arg")))
// We need to set the type to `Any` here (instead of `Never`), // We need to set the type to `Any` here (instead of `Never`),
// in order for every `assert_never` call to pass the argument // in order for every `assert_never` call to pass the argument
// check. If we set it to `Never`, we'll get invalid-argument-type // check. If we set it to `Never`, we'll get invalid-argument-type
// errors instead of `type-assertion-failure` errors. // errors instead of `type-assertion-failure` errors.
.with_annotated_type(Type::any())]), .with_annotated_type(Type::any())],
),
Some(Type::none(db)), Some(Type::none(db)),
), ),
) )
@ -5591,13 +5605,16 @@ impl<'db> Type<'db> {
Some(KnownFunction::Cast) => Binding::single( Some(KnownFunction::Cast) => Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_or_keyword(Name::new_static("typ")) db,
.type_form() [
.with_annotated_type(Type::any()), Parameter::positional_or_keyword(Name::new_static("typ"))
Parameter::positional_or_keyword(Name::new_static("val")) .type_form()
.with_annotated_type(Type::any()), .with_annotated_type(Type::any()),
]), Parameter::positional_or_keyword(Name::new_static("val"))
.with_annotated_type(Type::any()),
],
),
Some(Type::any()), Some(Type::any()),
), ),
) )
@ -5609,18 +5626,20 @@ impl<'db> Type<'db> {
[ [
// def dataclass(cls: None, /) -> Callable[[type[_T]], type[_T]]: ... // def dataclass(cls: None, /) -> Callable[[type[_T]], type[_T]]: ...
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(Some( Parameters::new(
Name::new_static("cls"), db,
)) [Parameter::positional_only(Some(Name::new_static("cls")))
.with_annotated_type(Type::none(db))]), .with_annotated_type(Type::none(db))],
),
None, None,
), ),
// def dataclass(cls: type[_T], /) -> type[_T]: ... // def dataclass(cls: type[_T], /) -> type[_T]: ...
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(Some( Parameters::new(
Name::new_static("cls"), db,
)) [Parameter::positional_only(Some(Name::new_static("cls")))
.with_annotated_type(KnownClass::Type.to_instance(db))]), .with_annotated_type(KnownClass::Type.to_instance(db))],
),
None, None,
), ),
// TODO: make this overload Python-version-dependent // TODO: make this overload Python-version-dependent
@ -5639,38 +5658,41 @@ impl<'db> Type<'db> {
// weakref_slot: bool = False, // weakref_slot: bool = False,
// ) -> Callable[[type[_T]], type[_T]]: ... // ) -> Callable[[type[_T]], type[_T]]: ...
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::keyword_only(Name::new_static("init")) db,
.with_annotated_type(KnownClass::Bool.to_instance(db)) [
.with_default_type(Type::BooleanLiteral(true)), Parameter::keyword_only(Name::new_static("init"))
Parameter::keyword_only(Name::new_static("repr")) .with_annotated_type(KnownClass::Bool.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::BooleanLiteral(true)),
.with_default_type(Type::BooleanLiteral(true)), Parameter::keyword_only(Name::new_static("repr"))
Parameter::keyword_only(Name::new_static("eq")) .with_annotated_type(KnownClass::Bool.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::BooleanLiteral(true)),
.with_default_type(Type::BooleanLiteral(true)), Parameter::keyword_only(Name::new_static("eq"))
Parameter::keyword_only(Name::new_static("order")) .with_annotated_type(KnownClass::Bool.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::BooleanLiteral(true)),
.with_default_type(Type::BooleanLiteral(false)), Parameter::keyword_only(Name::new_static("order"))
Parameter::keyword_only(Name::new_static("unsafe_hash")) .with_annotated_type(KnownClass::Bool.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::BooleanLiteral(false)),
.with_default_type(Type::BooleanLiteral(false)), Parameter::keyword_only(Name::new_static("unsafe_hash"))
Parameter::keyword_only(Name::new_static("frozen")) .with_annotated_type(KnownClass::Bool.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::BooleanLiteral(false)),
.with_default_type(Type::BooleanLiteral(false)), Parameter::keyword_only(Name::new_static("frozen"))
Parameter::keyword_only(Name::new_static("match_args")) .with_annotated_type(KnownClass::Bool.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::BooleanLiteral(false)),
.with_default_type(Type::BooleanLiteral(true)), Parameter::keyword_only(Name::new_static("match_args"))
Parameter::keyword_only(Name::new_static("kw_only")) .with_annotated_type(KnownClass::Bool.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::BooleanLiteral(true)),
.with_default_type(Type::BooleanLiteral(false)), Parameter::keyword_only(Name::new_static("kw_only"))
Parameter::keyword_only(Name::new_static("slots")) .with_annotated_type(KnownClass::Bool.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::BooleanLiteral(false)),
.with_default_type(Type::BooleanLiteral(false)), Parameter::keyword_only(Name::new_static("slots"))
Parameter::keyword_only(Name::new_static("weakref_slot")) .with_annotated_type(KnownClass::Bool.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::BooleanLiteral(false)),
.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, None,
), ),
], ],
@ -5700,11 +5722,12 @@ impl<'db> Type<'db> {
Binding::single( Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(Some(Name::new_static( Parameters::new(
"o", db,
))) [Parameter::positional_only(Some(Name::new_static("o")))
.with_annotated_type(Type::any()) .with_annotated_type(Type::any())
.with_default_type(Type::BooleanLiteral(false))]), .with_default_type(Type::BooleanLiteral(false))],
),
Some(KnownClass::Bool.to_instance(db)), Some(KnownClass::Bool.to_instance(db)),
), ),
) )
@ -5723,16 +5746,21 @@ impl<'db> Type<'db> {
self, self,
[ [
Signature::new( Signature::new(
Parameters::new([Parameter::positional_or_keyword( Parameters::new(
Name::new_static("object"), db,
) [Parameter::positional_or_keyword(Name::new_static("object"))
.with_annotated_type(Type::object()) .with_annotated_type(Type::object())
.with_default_type(Type::string_literal(db, ""))]), .with_default_type(Type::string_literal(db, ""))],
),
Some(KnownClass::Str.to_instance(db)), Some(KnownClass::Str.to_instance(db)),
), ),
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_or_keyword(Name::new_static("object")) db,
[
Parameter::positional_or_keyword(Name::new_static(
"object",
))
// TODO: Should be `ReadableBuffer` instead of this union type: // TODO: Should be `ReadableBuffer` instead of this union type:
.with_annotated_type(UnionType::from_elements( .with_annotated_type(UnionType::from_elements(
db, db,
@ -5742,13 +5770,18 @@ impl<'db> Type<'db> {
], ],
)) ))
.with_default_type(Type::bytes_literal(db, b"")), .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_annotated_type(KnownClass::Str.to_instance(db))
.with_default_type(Type::string_literal(db, "utf-8")), .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_annotated_type(KnownClass::Str.to_instance(db))
.with_default_type(Type::string_literal(db, "strict")), .with_default_type(Type::string_literal(db, "strict")),
]), ],
),
Some(KnownClass::Str.to_instance(db)), Some(KnownClass::Str.to_instance(db)),
), ),
], ],
@ -5771,29 +5804,33 @@ impl<'db> Type<'db> {
self, self,
[ [
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(Some( Parameters::new(
Name::new_static("o"), db,
)) [Parameter::positional_only(Some(Name::new_static("o")))
.with_annotated_type(Type::any())]), .with_annotated_type(Type::any())],
),
Some(type_instance), Some(type_instance),
), ),
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("name"))) db,
.with_annotated_type(str_instance), [
Parameter::positional_only(Some(Name::new_static("bases"))) Parameter::positional_only(Some(Name::new_static("name")))
.with_annotated_type(Type::homogeneous_tuple( .with_annotated_type(str_instance),
db, Parameter::positional_only(Some(Name::new_static("bases")))
type_instance, .with_annotated_type(Type::homogeneous_tuple(
)),
Parameter::positional_only(Some(Name::new_static("dict")))
.with_annotated_type(
KnownClass::Dict.to_specialized_instance(
db, 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), Some(type_instance),
), ),
], ],
@ -5832,19 +5869,23 @@ impl<'db> Type<'db> {
self, self,
[ [
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("t"))) db,
.with_annotated_type(Type::any()), [
Parameter::positional_only(Some(Name::new_static("obj"))) Parameter::positional_only(Some(Name::new_static("t")))
.with_annotated_type(Type::any()), .with_annotated_type(Type::any()),
]), Parameter::positional_only(Some(Name::new_static("obj")))
.with_annotated_type(Type::any()),
],
),
Some(KnownClass::Super.to_instance(db)), Some(KnownClass::Super.to_instance(db)),
), ),
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(Some( Parameters::new(
Name::new_static("t"), db,
)) [Parameter::positional_only(Some(Name::new_static("t")))
.with_annotated_type(Type::any())]), .with_annotated_type(Type::any())],
),
Some(KnownClass::Super.to_instance(db)), Some(KnownClass::Super.to_instance(db)),
), ),
Signature::new( Signature::new(
@ -5871,24 +5912,27 @@ impl<'db> Type<'db> {
Binding::single( Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("message"))) db,
.with_annotated_type(Type::LiteralString), [
Parameter::keyword_only(Name::new_static("category")) Parameter::positional_only(Some(Name::new_static("message")))
.with_annotated_type(UnionType::from_elements( .with_annotated_type(Type::LiteralString),
db, Parameter::keyword_only(Name::new_static("category"))
[ .with_annotated_type(UnionType::from_elements(
// TODO: should be `type[Warning]` db,
Type::any(), [
KnownClass::NoneType.to_instance(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")) // TODO: should be `type[Warning]`
.with_annotated_type(KnownClass::Int.to_instance(db)) .with_default_type(Type::any()),
.with_default_type(Type::IntLiteral(1)), 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)), Some(KnownClass::Deprecated.to_instance(db)),
), ),
) )
@ -5908,26 +5952,29 @@ impl<'db> Type<'db> {
Binding::single( Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_or_keyword(Name::new_static("name")) db,
.with_annotated_type(KnownClass::Str.to_instance(db)), [
Parameter::positional_or_keyword(Name::new_static("value")) Parameter::positional_or_keyword(Name::new_static("name"))
.with_annotated_type(Type::any()) .with_annotated_type(KnownClass::Str.to_instance(db)),
.type_form(), Parameter::positional_or_keyword(Name::new_static("value"))
Parameter::keyword_only(Name::new_static("type_params")) .with_annotated_type(Type::any())
.with_annotated_type(Type::homogeneous_tuple( .type_form(),
db, Parameter::keyword_only(Name::new_static("type_params"))
UnionType::from_elements( .with_annotated_type(Type::homogeneous_tuple(
db, db,
[ UnionType::from_elements(
KnownClass::TypeVar.to_instance(db), db,
KnownClass::ParamSpec.to_instance(db), [
KnownClass::TypeVarTuple.to_instance(db), KnownClass::TypeVar.to_instance(db),
], KnownClass::ParamSpec.to_instance(db),
), KnownClass::TypeVarTuple.to_instance(db),
)) ],
.with_default_type(Type::empty_tuple(db)), ),
]), ))
.with_default_type(Type::empty_tuple(db)),
],
),
None, None,
), ),
) )
@ -5936,63 +5983,71 @@ impl<'db> Type<'db> {
Some(KnownClass::Property) => { Some(KnownClass::Property) => {
let getter_signature = Signature::new( let getter_signature = Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(None).with_annotated_type(Type::any()) db,
]), [Parameter::positional_only(None).with_annotated_type(Type::any())],
),
Some(Type::any()), Some(Type::any()),
); );
let setter_signature = Signature::new( let setter_signature = Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(None).with_annotated_type(Type::any()), db,
Parameter::positional_only(None).with_annotated_type(Type::any()), [
]), Parameter::positional_only(None).with_annotated_type(Type::any()),
Parameter::positional_only(None).with_annotated_type(Type::any()),
],
),
Some(Type::none(db)), Some(Type::none(db)),
); );
let deleter_signature = Signature::new( let deleter_signature = Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(None).with_annotated_type(Type::any()) db,
]), [Parameter::positional_only(None).with_annotated_type(Type::any())],
),
Some(Type::any()), Some(Type::any()),
); );
Binding::single( Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_or_keyword(Name::new_static("fget")) db,
.with_annotated_type(UnionType::from_elements( [
db, Parameter::positional_or_keyword(Name::new_static("fget"))
[ .with_annotated_type(UnionType::from_elements(
Type::single_callable(db, getter_signature), db,
Type::none(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( .with_default_type(Type::none(db)),
db, Parameter::positional_or_keyword(Name::new_static("fset"))
[ .with_annotated_type(UnionType::from_elements(
Type::single_callable(db, setter_signature), db,
Type::none(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( .with_default_type(Type::none(db)),
db, Parameter::positional_or_keyword(Name::new_static("fdel"))
[ .with_annotated_type(UnionType::from_elements(
Type::single_callable(db, deleter_signature), db,
Type::none(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( .with_default_type(Type::none(db)),
db, Parameter::positional_or_keyword(Name::new_static("doc"))
[KnownClass::Str.to_instance(db), Type::none(db)], .with_annotated_type(UnionType::from_elements(
)) db,
.with_default_type(Type::none(db)), [KnownClass::Str.to_instance(db), Type::none(db)],
]), ))
.with_default_type(Type::none(db)),
],
),
None, None,
), ),
) )
@ -6014,12 +6069,15 @@ impl<'db> Type<'db> {
[ [
Signature::new(Parameters::empty(), Some(Type::empty_tuple(db))), Signature::new(Parameters::empty(), Some(Type::empty_tuple(db))),
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(Some( Parameters::new(
Name::new_static("iterable"), db,
)) [Parameter::positional_only(Some(Name::new_static(
.with_annotated_type( "iterable",
KnownClass::Iterable.to_specialized_instance(db, [object]), )))
)]), .with_annotated_type(
KnownClass::Iterable.to_specialized_instance(db, [object]),
)],
),
Some(Type::homogeneous_tuple(db, object)), Some(Type::homogeneous_tuple(db, object)),
), ),
], ],
@ -6047,19 +6105,22 @@ impl<'db> Type<'db> {
Binding::single( Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("typename"))) db,
.with_annotated_type(KnownClass::Str.to_instance(db)), [
Parameter::positional_only(Some(Name::new_static("fields"))) Parameter::positional_only(Some(Name::new_static("typename")))
.with_annotated_type(KnownClass::Dict.to_instance(db)) .with_annotated_type(KnownClass::Str.to_instance(db)),
.with_default_type(Type::any()), Parameter::positional_only(Some(Name::new_static("fields")))
Parameter::keyword_only(Name::new_static("total")) .with_annotated_type(KnownClass::Dict.to_instance(db))
.with_annotated_type(KnownClass::Bool.to_instance(db)) .with_default_type(Type::any()),
.with_default_type(Type::BooleanLiteral(true)), Parameter::keyword_only(Name::new_static("total"))
// Future compatibility, in case new keyword arguments will be added: .with_annotated_type(KnownClass::Bool.to_instance(db))
Parameter::keyword_variadic(Name::new_static("kwargs")) .with_default_type(Type::BooleanLiteral(true)),
.with_annotated_type(Type::any()), // Future compatibility, in case new keyword arguments will be added:
]), Parameter::keyword_variadic(Name::new_static("kwargs"))
.with_annotated_type(Type::any()),
],
),
None, None,
), ),
) )
@ -6154,8 +6215,11 @@ impl<'db> Type<'db> {
Type::KnownInstance(KnownInstanceType::NewType(newtype)) => Binding::single( Type::KnownInstance(KnownInstanceType::NewType(newtype)) => Binding::single(
self, self,
Signature::new( Signature::new(
Parameters::new([Parameter::positional_only(None) Parameters::new(
.with_annotated_type(newtype.base(db).instance_type(db))]), db,
[Parameter::positional_only(None)
.with_annotated_type(newtype.base(db).instance_type(db))],
),
Some(Type::NewTypeInstance(newtype)), Some(Type::NewTypeInstance(newtype)),
), ),
) )
@ -12084,25 +12148,31 @@ impl<'db> KnownBoundMethodType<'db> {
| KnownBoundMethodType::PropertyDunderGet(_) => Either::Left(Either::Left( | KnownBoundMethodType::PropertyDunderGet(_) => Either::Left(Either::Left(
[ [
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("instance"))) db,
.with_annotated_type(Type::none(db)), [
Parameter::positional_only(Some(Name::new_static("owner"))) Parameter::positional_only(Some(Name::new_static("instance")))
.with_annotated_type(KnownClass::Type.to_instance(db)), .with_annotated_type(Type::none(db)),
]), Parameter::positional_only(Some(Name::new_static("owner")))
.with_annotated_type(KnownClass::Type.to_instance(db)),
],
),
None, None,
), ),
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("instance"))) db,
.with_annotated_type(Type::object()), [
Parameter::positional_only(Some(Name::new_static("owner"))) Parameter::positional_only(Some(Name::new_static("instance")))
.with_annotated_type(UnionType::from_elements( .with_annotated_type(Type::object()),
db, Parameter::positional_only(Some(Name::new_static("owner")))
[KnownClass::Type.to_instance(db), Type::none(db)], .with_annotated_type(UnionType::from_elements(
)) db,
.with_default_type(Type::none(db)), [KnownClass::Type.to_instance(db), Type::none(db)],
]), ))
.with_default_type(Type::none(db)),
],
),
None, None,
), ),
] ]
@ -12113,56 +12183,68 @@ impl<'db> KnownBoundMethodType<'db> {
)), )),
KnownBoundMethodType::PropertyDunderSet(_) => { KnownBoundMethodType::PropertyDunderSet(_) => {
Either::Right(std::iter::once(Signature::new( Either::Right(std::iter::once(Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("instance"))) db,
.with_annotated_type(Type::object()), [
Parameter::positional_only(Some(Name::new_static("value"))) Parameter::positional_only(Some(Name::new_static("instance")))
.with_annotated_type(Type::object()), .with_annotated_type(Type::object()),
]), Parameter::positional_only(Some(Name::new_static("value")))
.with_annotated_type(Type::object()),
],
),
None, None,
))) )))
} }
KnownBoundMethodType::StrStartswith(_) => { KnownBoundMethodType::StrStartswith(_) => {
Either::Right(std::iter::once(Signature::new( Either::Right(std::iter::once(Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("prefix"))) db,
.with_annotated_type(UnionType::from_elements( [
db, Parameter::positional_only(Some(Name::new_static("prefix")))
[ .with_annotated_type(UnionType::from_elements(
KnownClass::Str.to_instance(db), db,
Type::homogeneous_tuple(db, KnownClass::Str.to_instance(db)), [
], KnownClass::Str.to_instance(db),
)), Type::homogeneous_tuple(
Parameter::positional_only(Some(Name::new_static("start"))) db,
.with_annotated_type(UnionType::from_elements( KnownClass::Str.to_instance(db),
db, ),
[KnownClass::SupportsIndex.to_instance(db), Type::none(db)], ],
)) )),
.with_default_type(Type::none(db)), Parameter::positional_only(Some(Name::new_static("start")))
Parameter::positional_only(Some(Name::new_static("end"))) .with_annotated_type(UnionType::from_elements(
.with_annotated_type(UnionType::from_elements( db,
db, [KnownClass::SupportsIndex.to_instance(db), Type::none(db)],
[KnownClass::SupportsIndex.to_instance(db), Type::none(db)], ))
)) .with_default_type(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)), Some(KnownClass::Bool.to_instance(db)),
))) )))
} }
KnownBoundMethodType::ConstraintSetRange => { KnownBoundMethodType::ConstraintSetRange => {
Either::Right(std::iter::once(Signature::new( Either::Right(std::iter::once(Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("lower_bound"))) db,
.type_form() [
.with_annotated_type(Type::any()), Parameter::positional_only(Some(Name::new_static("lower_bound")))
Parameter::positional_only(Some(Name::new_static("typevar"))) .type_form()
.type_form() .with_annotated_type(Type::any()),
.with_annotated_type(Type::any()), Parameter::positional_only(Some(Name::new_static("typevar")))
Parameter::positional_only(Some(Name::new_static("upper_bound"))) .type_form()
.type_form() .with_annotated_type(Type::any()),
.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)), Some(KnownClass::ConstraintSet.to_instance(db)),
))) )))
} }
@ -12177,45 +12259,57 @@ impl<'db> KnownBoundMethodType<'db> {
KnownBoundMethodType::ConstraintSetImpliesSubtypeOf(_) => { KnownBoundMethodType::ConstraintSetImpliesSubtypeOf(_) => {
Either::Right(std::iter::once(Signature::new( Either::Right(std::iter::once(Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("ty"))) db,
.type_form() [
.with_annotated_type(Type::any()), Parameter::positional_only(Some(Name::new_static("ty")))
Parameter::positional_only(Some(Name::new_static("of"))) .type_form()
.type_form() .with_annotated_type(Type::any()),
.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)), Some(KnownClass::ConstraintSet.to_instance(db)),
))) )))
} }
KnownBoundMethodType::ConstraintSetSatisfies(_) => { KnownBoundMethodType::ConstraintSetSatisfies(_) => {
Either::Right(std::iter::once(Signature::new( Either::Right(std::iter::once(Signature::new(
Parameters::new([Parameter::positional_only(Some(Name::new_static("other"))) Parameters::new(
.with_annotated_type(KnownClass::ConstraintSet.to_instance(db))]), db,
[Parameter::positional_only(Some(Name::new_static("other")))
.with_annotated_type(KnownClass::ConstraintSet.to_instance(db))],
),
Some(KnownClass::ConstraintSet.to_instance(db)), Some(KnownClass::ConstraintSet.to_instance(db)),
))) )))
} }
KnownBoundMethodType::ConstraintSetSatisfiedByAllTypeVars(_) => { KnownBoundMethodType::ConstraintSetSatisfiedByAllTypeVars(_) => {
Either::Right(std::iter::once(Signature::new( Either::Right(std::iter::once(Signature::new(
Parameters::new([Parameter::keyword_only(Name::new_static("inferable")) Parameters::new(
.type_form() db,
.with_annotated_type(UnionType::from_elements( [Parameter::keyword_only(Name::new_static("inferable"))
db, .type_form()
[Type::homogeneous_tuple(db, Type::any()), Type::none(db)], .with_annotated_type(UnionType::from_elements(
)) db,
.with_default_type(Type::none(db))]), [Type::homogeneous_tuple(db, Type::any()), Type::none(db)],
))
.with_default_type(Type::none(db))],
),
Some(KnownClass::Bool.to_instance(db)), Some(KnownClass::Bool.to_instance(db)),
))) )))
} }
KnownBoundMethodType::GenericContextSpecializeConstrained(_) => { KnownBoundMethodType::GenericContextSpecializeConstrained(_) => {
Either::Right(std::iter::once(Signature::new( Either::Right(std::iter::once(Signature::new(
Parameters::new([Parameter::positional_only(Some(Name::new_static( Parameters::new(
"constraints", db,
))) [
.with_annotated_type(KnownClass::ConstraintSet.to_instance(db))]), Parameter::positional_only(Some(Name::new_static("constraints")))
.with_annotated_type(KnownClass::ConstraintSet.to_instance(db)),
],
),
Some(UnionType::from_elements( Some(UnionType::from_elements(
db, db,
[KnownClass::Specialization.to_instance(db), Type::none(db)], [KnownClass::Specialization.to_instance(db), Type::none(db)],
@ -12255,29 +12349,35 @@ impl WrapperDescriptorKind {
let descriptor = class.to_instance(db); let descriptor = class.to_instance(db);
[ [
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(descriptor), [
Parameter::positional_only(Some(Name::new_static("instance"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(none), .with_annotated_type(descriptor),
Parameter::positional_only(Some(Name::new_static("owner"))) Parameter::positional_only(Some(Name::new_static("instance")))
.with_annotated_type(type_instance), .with_annotated_type(none),
]), Parameter::positional_only(Some(Name::new_static("owner")))
.with_annotated_type(type_instance),
],
),
None, None,
), ),
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(descriptor), [
Parameter::positional_only(Some(Name::new_static("instance"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(Type::object()), .with_annotated_type(descriptor),
Parameter::positional_only(Some(Name::new_static("owner"))) Parameter::positional_only(Some(Name::new_static("instance")))
.with_annotated_type(UnionType::from_elements( .with_annotated_type(Type::object()),
db, Parameter::positional_only(Some(Name::new_static("owner")))
[type_instance, none], .with_annotated_type(UnionType::from_elements(
)) db,
.with_default_type(none), [type_instance, none],
]), ))
.with_default_type(none),
],
),
None, None,
), ),
] ]
@ -12293,14 +12393,17 @@ impl WrapperDescriptorKind {
WrapperDescriptorKind::PropertyDunderSet => { WrapperDescriptorKind::PropertyDunderSet => {
let object = Type::object(); let object = Type::object();
Either::Right(std::iter::once(Signature::new( Either::Right(std::iter::once(Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(KnownClass::Property.to_instance(db)), [
Parameter::positional_only(Some(Name::new_static("instance"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(object), .with_annotated_type(KnownClass::Property.to_instance(db)),
Parameter::positional_only(Some(Name::new_static("value"))) Parameter::positional_only(Some(Name::new_static("instance")))
.with_annotated_type(object), .with_annotated_type(object),
]), Parameter::positional_only(Some(Name::new_static("value")))
.with_annotated_type(object),
],
),
None, None,
))) )))
} }

View File

@ -808,13 +808,14 @@ impl<'db> ClassType<'db> {
name: &str, name: &str,
) -> Member<'db> { ) -> Member<'db> {
fn synthesize_getitem_overload_signature<'db>( fn synthesize_getitem_overload_signature<'db>(
db: &'db dyn Db,
index_annotation: Type<'db>, index_annotation: Type<'db>,
return_annotation: Type<'db>, return_annotation: Type<'db>,
) -> Signature<'db> { ) -> Signature<'db> {
let self_parameter = Parameter::positional_only(Some(Name::new_static("self"))); let self_parameter = Parameter::positional_only(Some(Name::new_static("self")));
let index_parameter = Parameter::positional_only(Some(Name::new_static("index"))) let index_parameter = Parameter::positional_only(Some(Name::new_static("index")))
.with_annotated_type(index_annotation); .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)) Signature::new(parameters, Some(return_annotation))
} }
@ -845,9 +846,11 @@ impl<'db> ClassType<'db> {
.map(Type::IntLiteral) .map(Type::IntLiteral)
.unwrap_or_else(|| KnownClass::Int.to_instance(db)); .unwrap_or_else(|| KnownClass::Int.to_instance(db));
let parameters = let parameters = Parameters::new(
Parameters::new([Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(Type::instance(db, self))]); [Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(Type::instance(db, self))],
);
let synthesized_dunder_method = let synthesized_dunder_method =
Type::function_like_callable(db, Signature::new(parameters, Some(return_type))); Type::function_like_callable(db, Signature::new(parameters, Some(return_type)));
@ -975,6 +978,7 @@ impl<'db> ClassType<'db> {
); );
Some(synthesize_getitem_overload_signature( Some(synthesize_getitem_overload_signature(
db,
index_annotation, index_annotation,
return_type, return_type,
)) ))
@ -992,11 +996,13 @@ impl<'db> ClassType<'db> {
// __getitem__(self, index: slice[Any, Any, Any], /) -> tuple[str | float | bytes, ...] // __getitem__(self, index: slice[Any, Any, Any], /) -> tuple[str | float | bytes, ...]
// //
overload_signatures.push(synthesize_getitem_overload_signature( overload_signatures.push(synthesize_getitem_overload_signature(
db,
KnownClass::SupportsIndex.to_instance(db), KnownClass::SupportsIndex.to_instance(db),
all_elements_unioned, all_elements_unioned,
)); ));
overload_signatures.push(synthesize_getitem_overload_signature( overload_signatures.push(synthesize_getitem_overload_signature(
db,
KnownClass::Slice.to_instance(db), KnownClass::Slice.to_instance(db),
Type::homogeneous_tuple(db, all_elements_unioned), Type::homogeneous_tuple(db, all_elements_unioned),
)); ));
@ -1066,11 +1072,14 @@ impl<'db> ClassType<'db> {
iterable_parameter.with_default_type(Type::empty_tuple(db)); iterable_parameter.with_default_type(Type::empty_tuple(db));
} }
let parameters = Parameters::new([ let parameters = Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(SubclassOfType::from(db, self)), [
iterable_parameter, Parameter::positional_only(Some(Name::new_static("self")))
]); .with_annotated_type(SubclassOfType::from(db, self)),
iterable_parameter,
],
);
let synthesized_dunder = Type::function_like_callable( let synthesized_dunder = Type::function_like_callable(
db, db,
@ -2212,7 +2221,10 @@ impl<'db> ClassLiteral<'db> {
.get(name) .get(name)
{ {
let property_getter_signature = Signature::new( 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), Some(field.declared_ty),
); );
let property_getter = Type::single_callable(db, property_getter_signature); let property_getter = Type::single_callable(db, property_getter_signature);
@ -2424,10 +2436,10 @@ impl<'db> ClassLiteral<'db> {
let signature = match name { let signature = match name {
"__new__" | "__init__" => Signature::new_generic( "__new__" | "__init__" => Signature::new_generic(
inherited_generic_context.or_else(|| self.inherited_generic_context(db)), inherited_generic_context.or_else(|| self.inherited_generic_context(db)),
Parameters::new(parameters), Parameters::new(db, parameters),
return_ty, return_ty,
), ),
_ => Signature::new(Parameters::new(parameters), return_ty), _ => Signature::new(Parameters::new(db, parameters), return_ty),
}; };
Some(Type::function_like_callable(db, signature)) Some(Type::function_like_callable(db, signature))
}; };
@ -2454,14 +2466,17 @@ impl<'db> ClassLiteral<'db> {
} }
let signature = Signature::new( let signature = Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_or_keyword(Name::new_static("self")) db,
// TODO: could be `Self`. [
.with_annotated_type(instance_ty), Parameter::positional_or_keyword(Name::new_static("self"))
Parameter::positional_or_keyword(Name::new_static("other")) // TODO: could be `Self`.
// TODO: could be `Self`. .with_annotated_type(instance_ty),
.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)), Some(KnownClass::Bool.to_instance(db)),
); );
@ -2474,10 +2489,11 @@ impl<'db> ClassLiteral<'db> {
if unsafe_hash || (frozen && eq) { if unsafe_hash || (frozen && eq) {
let signature = Signature::new( let signature = Signature::new(
Parameters::new([Parameter::positional_or_keyword(Name::new_static( Parameters::new(
"self", db,
)) [Parameter::positional_or_keyword(Name::new_static("self"))
.with_annotated_type(instance_ty)]), .with_annotated_type(instance_ty)],
),
Some(KnownClass::Int.to_instance(db)), Some(KnownClass::Int.to_instance(db)),
); );
@ -2559,12 +2575,15 @@ impl<'db> ClassLiteral<'db> {
(CodeGeneratorKind::DataclassLike(_), "__setattr__") => { (CodeGeneratorKind::DataclassLike(_), "__setattr__") => {
if has_dataclass_param(DataclassFlags::FROZEN) { if has_dataclass_param(DataclassFlags::FROZEN) {
let signature = Signature::new( let signature = Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_or_keyword(Name::new_static("self")) db,
.with_annotated_type(instance_ty), [
Parameter::positional_or_keyword(Name::new_static("name")), Parameter::positional_or_keyword(Name::new_static("self"))
Parameter::positional_or_keyword(Name::new_static("value")), .with_annotated_type(instance_ty),
]), Parameter::positional_or_keyword(Name::new_static("name")),
Parameter::positional_or_keyword(Name::new_static("value")),
],
),
Some(Type::Never), Some(Type::Never),
); );
@ -2599,14 +2618,17 @@ impl<'db> ClassLiteral<'db> {
return Some(Type::Callable(CallableType::new( return Some(Type::Callable(CallableType::new(
db, db,
CallableSignature::single(Signature::new( CallableSignature::single(Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(Type::Never), .with_annotated_type(instance_ty),
Parameter::positional_only(Some(Name::new_static("value"))) Parameter::positional_only(Some(Name::new_static("key")))
.with_annotated_type(Type::any()), .with_annotated_type(Type::Never),
]), Parameter::positional_only(Some(Name::new_static("value")))
.with_annotated_type(Type::any()),
],
),
Some(Type::none(db)), Some(Type::none(db)),
)), )),
true, true,
@ -2617,14 +2639,17 @@ impl<'db> ClassLiteral<'db> {
let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str())); let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str()));
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(key_type), .with_annotated_type(instance_ty),
Parameter::positional_only(Some(Name::new_static("value"))) Parameter::positional_only(Some(Name::new_static("key")))
.with_annotated_type(field.declared_ty), .with_annotated_type(key_type),
]), Parameter::positional_only(Some(Name::new_static("value")))
.with_annotated_type(field.declared_ty),
],
),
Some(Type::none(db)), Some(Type::none(db)),
) )
}); });
@ -2643,12 +2668,15 @@ impl<'db> ClassLiteral<'db> {
let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str())); let key_type = Type::StringLiteral(StringLiteralType::new(db, name.as_str()));
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(key_type), .with_annotated_type(instance_ty),
]), Parameter::positional_only(Some(Name::new_static("key")))
.with_annotated_type(key_type),
],
),
Some(field.declared_ty), Some(field.declared_ty),
) )
}); });
@ -2675,12 +2703,15 @@ impl<'db> ClassLiteral<'db> {
// once the generics solver takes default arguments into account. // once the generics solver takes default arguments into account.
let get_sig = Signature::new( let get_sig = Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(key_type), .with_annotated_type(instance_ty),
]), Parameter::positional_only(Some(Name::new_static("key")))
.with_annotated_type(key_type),
],
),
Some(if field.is_required() { Some(if field.is_required() {
field.declared_ty field.declared_ty
} else { } else {
@ -2693,14 +2724,17 @@ impl<'db> ClassLiteral<'db> {
let get_with_default_sig = Signature::new_generic( let get_with_default_sig = Signature::new_generic(
Some(GenericContext::from_typevar_instances(db, [t_default])), Some(GenericContext::from_typevar_instances(db, [t_default])),
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(key_type), .with_annotated_type(instance_ty),
Parameter::positional_only(Some(Name::new_static("default"))) Parameter::positional_only(Some(Name::new_static("key")))
.with_annotated_type(Type::TypeVar(t_default)), .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() { Some(if field.is_required() {
field.declared_ty field.declared_ty
} else { } else {
@ -2716,12 +2750,15 @@ impl<'db> ClassLiteral<'db> {
// Fallback overloads for unknown keys // Fallback overloads for unknown keys
.chain(std::iter::once({ .chain(std::iter::once({
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(KnownClass::Str.to_instance(db)), .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( Some(UnionType::from_elements(
db, db,
[Type::unknown(), Type::none(db)], [Type::unknown(), Type::none(db)],
@ -2734,14 +2771,17 @@ impl<'db> ClassLiteral<'db> {
Signature::new_generic( Signature::new_generic(
Some(GenericContext::from_typevar_instances(db, [t_default])), Some(GenericContext::from_typevar_instances(db, [t_default])),
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(KnownClass::Str.to_instance(db)), .with_annotated_type(instance_ty),
Parameter::positional_only(Some(Name::new_static("default"))) Parameter::positional_only(Some(Name::new_static("key")))
.with_annotated_type(Type::TypeVar(t_default)), .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( Some(UnionType::from_elements(
db, db,
[Type::unknown(), Type::TypeVar(t_default)], [Type::unknown(), Type::TypeVar(t_default)],
@ -2771,12 +2811,15 @@ impl<'db> ClassLiteral<'db> {
// `.pop()` without default // `.pop()` without default
let pop_sig = Signature::new( let pop_sig = Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(key_type), .with_annotated_type(instance_ty),
]), Parameter::positional_only(Some(Name::new_static("key")))
.with_annotated_type(key_type),
],
),
Some(field.declared_ty), Some(field.declared_ty),
); );
@ -2786,14 +2829,17 @@ impl<'db> ClassLiteral<'db> {
let pop_with_default_sig = Signature::new_generic( let pop_with_default_sig = Signature::new_generic(
Some(GenericContext::from_typevar_instances(db, [t_default])), Some(GenericContext::from_typevar_instances(db, [t_default])),
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(key_type), .with_annotated_type(instance_ty),
Parameter::positional_only(Some(Name::new_static("default"))) Parameter::positional_only(Some(Name::new_static("key")))
.with_annotated_type(Type::TypeVar(t_default)), .with_annotated_type(key_type),
]), Parameter::positional_only(Some(Name::new_static("default")))
.with_annotated_type(Type::TypeVar(t_default)),
],
),
Some(UnionType::from_elements( Some(UnionType::from_elements(
db, db,
[field.declared_ty, Type::TypeVar(t_default)], [field.declared_ty, Type::TypeVar(t_default)],
@ -2816,14 +2862,17 @@ impl<'db> ClassLiteral<'db> {
// `setdefault` always returns the field type // `setdefault` always returns the field type
Signature::new( Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::positional_only(Some(Name::new_static("key"))) Parameter::positional_only(Some(Name::new_static("self")))
.with_annotated_type(key_type), .with_annotated_type(instance_ty),
Parameter::positional_only(Some(Name::new_static("default"))) Parameter::positional_only(Some(Name::new_static("key")))
.with_annotated_type(field.declared_ty), .with_annotated_type(key_type),
]), Parameter::positional_only(Some(Name::new_static("default")))
.with_annotated_type(field.declared_ty),
],
),
Some(field.declared_ty), Some(field.declared_ty),
) )
}); });
@ -2837,12 +2886,15 @@ impl<'db> ClassLiteral<'db> {
(CodeGeneratorKind::TypedDict, "update") => { (CodeGeneratorKind::TypedDict, "update") => {
// TODO: synthesize a set of overloads with precise types // TODO: synthesize a set of overloads with precise types
let signature = Signature::new( let signature = Signature::new(
Parameters::new([ Parameters::new(
Parameter::positional_only(Some(Name::new_static("self"))) db,
.with_annotated_type(instance_ty), [
Parameter::variadic(Name::new_static("args")), Parameter::positional_only(Some(Name::new_static("self")))
Parameter::keyword_variadic(Name::new_static("kwargs")), .with_annotated_type(instance_ty),
]), Parameter::variadic(Name::new_static("args")),
Parameter::keyword_variadic(Name::new_static("kwargs")),
],
),
Some(Type::none(db)), Some(Type::none(db)),
); );

View File

@ -2302,21 +2302,21 @@ mod tests {
} }
fn display_signature<'db>( fn display_signature<'db>(
db: &dyn Db, db: &'db dyn Db,
parameters: impl IntoIterator<Item = Parameter<'db>>, parameters: impl IntoIterator<Item = Parameter<'db>>,
return_ty: Option<Type<'db>>, return_ty: Option<Type<'db>>,
) -> String { ) -> String {
Signature::new(Parameters::new(parameters), return_ty) Signature::new(Parameters::new(db, parameters), return_ty)
.display(db) .display(db)
.to_string() .to_string()
} }
fn display_signature_multiline<'db>( fn display_signature_multiline<'db>(
db: &dyn Db, db: &'db dyn Db,
parameters: impl IntoIterator<Item = Parameter<'db>>, parameters: impl IntoIterator<Item = Parameter<'db>>,
return_ty: Option<Type<'db>>, return_ty: Option<Type<'db>>,
) -> String { ) -> String {
Signature::new(Parameters::new(parameters), return_ty) Signature::new(Parameters::new(db, parameters), return_ty)
.display_with(db, super::DisplaySettings::default().multiline()) .display_with(db, super::DisplaySettings::default().multiline())
.to_string() .to_string()
} }

View File

@ -3361,9 +3361,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
// TODO: `Unpack` // TODO: `Unpack`
Parameters::todo() Parameters::todo()
} else { } else {
Parameters::new(parameter_types.iter().map(|param_type| { Parameters::new(
Parameter::positional_only(None).with_annotated_type(*param_type) 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)) 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())); .map(|param| Parameter::keyword_variadic(param.name().id.clone()));
Parameters::new( Parameters::new(
self.db(),
positional_only positional_only
.into_iter() .into_iter()
.chain(positional_or_keyword) .chain(positional_or_keyword)

View File

@ -1636,9 +1636,12 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
// TODO: `Unpack` // TODO: `Unpack`
Parameters::todo() Parameters::todo()
} else { } else {
Parameters::new(parameter_types.iter().map(|param_type| { Parameters::new(
Parameter::positional_only(None).with_annotated_type(*param_type) self.db(),
})) parameter_types.iter().map(|param_type| {
Parameter::positional_only(None).with_annotated_type(*param_type)
}),
)
}); });
} }
ast::Expr::Subscript(subscript) => { ast::Expr::Subscript(subscript) => {

View File

@ -76,24 +76,29 @@ impl CallableParams {
pub(crate) fn into_parameters(self, db: &TestDb) -> Parameters<'_> { pub(crate) fn into_parameters(self, db: &TestDb) -> Parameters<'_> {
match self { match self {
CallableParams::GradualForm => Parameters::gradual_form(), CallableParams::GradualForm => Parameters::gradual_form(),
CallableParams::List(params) => Parameters::new(params.into_iter().map(|param| { CallableParams::List(params) => Parameters::new(
let mut parameter = match param.kind { db,
ParamKind::PositionalOnly => Parameter::positional_only(param.name), params.into_iter().map(|param| {
ParamKind::PositionalOrKeyword => { let mut parameter = match param.kind {
Parameter::positional_or_keyword(param.name.unwrap()) 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()), if let Some(default_ty) = param.default_ty {
ParamKind::KeywordOnly => Parameter::keyword_only(param.name.unwrap()), parameter = parameter.with_default_type(default_ty.into_type(db));
ParamKind::KeywordVariadic => Parameter::keyword_variadic(param.name.unwrap()), }
}; parameter
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
})),
} }
} }
} }

View File

@ -212,7 +212,10 @@ impl<'db> ProtocolInterface<'db> {
// Synthesize a read-only property (one that has a getter but no setter) // Synthesize a read-only property (one that has a getter but no setter)
// which returns the specified type from its getter. // which returns the specified type from its getter.
let property_getter_signature = Signature::new( 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)), Some(ty.normalized(db)),
); );
let property_getter = Type::single_callable(db, property_getter_signature); let property_getter = Type::single_callable(db, property_getter_signature);

View File

@ -558,11 +558,12 @@ impl<'db> Signature<'db> {
// Discard the definition when normalizing, so that two equivalent signatures // Discard the definition when normalizing, so that two equivalent signatures
// with different `Definition`s share the same Salsa ID when normalized // with different `Definition`s share the same Salsa ID when normalized
definition: None, definition: None,
parameters: self parameters: Parameters::new(
.parameters db,
.iter() self.parameters
.map(|param| param.normalized_impl(db, visitor)) .iter()
.collect(), .map(|param| param.normalized_impl(db, visitor)),
),
return_ty: self return_ty: self
.return_ty .return_ty
.map(|return_ty| return_ty.normalized_impl(db, visitor)), .map(|return_ty| return_ty.normalized_impl(db, visitor)),
@ -587,14 +588,17 @@ impl<'db> Signature<'db> {
), ),
None => None, 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 { Some(Self {
generic_context: self.generic_context, generic_context: self.generic_context,
definition: self.definition, definition: self.definition,
parameters: self parameters,
.parameters
.iter()
.map(|param| param.recursive_type_normalized_impl(db, div, nested, visitor))
.collect::<Option<_>>()?,
return_ty, return_ty,
}) })
} }
@ -662,7 +666,7 @@ impl<'db> Signature<'db> {
parameters.next(); parameters.next();
} }
let mut parameters = Parameters::new(parameters); let mut parameters = Parameters::new(db, parameters);
let mut return_ty = self.return_ty; let mut return_ty = self.return_ty;
if let Some(self_type) = self_type { if let Some(self_type) = self_type {
parameters = parameters.apply_type_mapping_impl( parameters = parameters.apply_type_mapping_impl(
@ -1344,7 +1348,10 @@ pub(crate) struct Parameters<'db> {
} }
impl<'db> Parameters<'db> { impl<'db> Parameters<'db> {
pub(crate) fn new(parameters: impl IntoIterator<Item = Parameter<'db>>) -> Self { pub(crate) fn new(
_db: &'db dyn Db,
parameters: impl IntoIterator<Item = Parameter<'db>>,
) -> Self {
let value: Vec<Parameter<'db>> = parameters.into_iter().collect(); let value: Vec<Parameter<'db>> = parameters.into_iter().collect();
let is_gradual = value.len() == 2 let is_gradual = value.len() == 2
&& value && value
@ -1598,6 +1605,7 @@ impl<'db> Parameters<'db> {
}); });
Self::new( Self::new(
db,
positional_only positional_only
.into_iter() .into_iter()
.chain(positional_or_keyword) .chain(positional_or_keyword)
@ -1715,12 +1723,6 @@ impl<'db, 'a> IntoIterator for &'a Parameters<'db> {
} }
} }
impl<'db> FromIterator<Parameter<'db>> for Parameters<'db> {
fn from_iter<T: IntoIterator<Item = Parameter<'db>>>(iter: T) -> Self {
Self::new(iter)
}
}
impl<'db> std::ops::Index<usize> for Parameters<'db> { impl<'db> std::ops::Index<usize> for Parameters<'db> {
type Output = Parameter<'db>; type Output = Parameter<'db>;