Skip to content

Commit 8406319

Browse files
committed
describe tuple specs better
1 parent 3cf5944 commit 8406319

7 files changed

Lines changed: 159 additions & 120 deletions

File tree

crates/ty_python_semantic/src/types.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use crate::types::infer::infer_unpack_types;
5151
use crate::types::mro::{Mro, MroError, MroIterator};
5252
pub(crate) use crate::types::narrow::infer_narrowing_constraint;
5353
use crate::types::signatures::{Parameter, ParameterForm, Parameters};
54-
use crate::types::tuple::{Tuple, TupleType};
54+
use crate::types::tuple::{TupleSpec, TupleType};
5555
pub use crate::util::diagnostics::add_inferred_python_version_hint_to_diagnostic;
5656
use crate::{Db, FxOrderSet, Module, Program};
5757
pub(crate) use class::{ClassLiteral, ClassType, GenericAlias, KnownClass};
@@ -3486,8 +3486,8 @@ impl<'db> Type<'db> {
34863486
Type::BytesLiteral(bytes) => Some(bytes.python_len(db)),
34873487
Type::StringLiteral(string) => Some(string.python_len(db)),
34883488
Type::Tuple(tuple) => match tuple.tuple(db) {
3489-
Tuple::Fixed(tuple) => Some(tuple.len()),
3490-
Tuple::Variable(_) => None,
3489+
TupleSpec::Fixed(tuple) => Some(tuple.len()),
3490+
TupleSpec::Variable(_) => None,
34913491
},
34923492

34933493
_ => None,

crates/ty_python_semantic/src/types/call/arguments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use itertools::{Either, Itertools};
55

66
use crate::Db;
77
use crate::types::KnownClass;
8-
use crate::types::tuple::{Tuple, TupleType};
8+
use crate::types::tuple::{TupleSpec, TupleType};
99

1010
use super::Type;
1111

@@ -215,7 +215,7 @@ fn expand_type<'db>(db: &'db dyn Db, ty: Type<'db>) -> Option<Vec<Type<'db>>> {
215215
// Note: This should only account for tuples of known length, i.e., `tuple[bool, ...]`
216216
// should not be expanded here.
217217
let tuple = tuple_type.tuple(db);
218-
if !matches!(tuple, Tuple::Fixed(_)) {
218+
if !matches!(tuple, TupleSpec::Fixed(_)) {
219219
return None;
220220
}
221221
let expanded = tuple

crates/ty_python_semantic/src/types/display.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::types::class::{ClassLiteral, ClassType, GenericAlias};
1010
use crate::types::function::{FunctionType, OverloadLiteral};
1111
use crate::types::generics::{GenericContext, Specialization};
1212
use crate::types::signatures::{CallableSignature, Parameter, Parameters, Signature};
13-
use crate::types::tuple::Tuple;
13+
use crate::types::tuple::TupleSpec;
1414
use crate::types::{
1515
CallableType, IntersectionType, KnownClass, MethodWrapperKind, Protocol, StringLiteralType,
1616
SubclassOfInner, Type, TypeVarBoundOrConstraints, TypeVarInstance, UnionType,
@@ -216,22 +216,22 @@ impl Display for DisplayRepresentation<'_> {
216216
}
217217
}
218218

219-
impl<'db> Tuple<'db> {
219+
impl<'db> TupleSpec<'db> {
220220
pub(crate) fn display(&'db self, db: &'db dyn Db) -> DisplayTuple<'db> {
221221
DisplayTuple { tuple: self, db }
222222
}
223223
}
224224

225225
pub(crate) struct DisplayTuple<'db> {
226-
tuple: &'db Tuple<'db>,
226+
tuple: &'db TupleSpec<'db>,
227227
db: &'db dyn Db,
228228
}
229229

230230
impl Display for DisplayTuple<'_> {
231231
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
232232
f.write_str("tuple[")?;
233233
match self.tuple {
234-
Tuple::Fixed(tuple) => {
234+
TupleSpec::Fixed(tuple) => {
235235
let elements = tuple.elements_slice();
236236
if elements.is_empty() {
237237
f.write_str("()")?;
@@ -254,7 +254,7 @@ impl Display for DisplayTuple<'_> {
254254
// above only an S is included only if there's a suffix; anything about both a P and an
255255
// S is included if there is either a prefix or a suffix. The initial `tuple[` and
256256
// trailing `]` are printed elsewhere. The `yyy, ...` is printed no matter what.)
257-
Tuple::Variable(tuple) => {
257+
TupleSpec::Variable(tuple) => {
258258
if !tuple.prefix.is_empty() {
259259
tuple.prefix.display(self.db).fmt(f)?;
260260
f.write_str(", ")?;

crates/ty_python_semantic/src/types/generics.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::types::class::ClassType;
88
use crate::types::class_base::ClassBase;
99
use crate::types::instance::{NominalInstanceType, Protocol, ProtocolInstanceType};
1010
use crate::types::signatures::{Parameter, Parameters, Signature};
11-
use crate::types::tuple::{Tuple, TupleType};
11+
use crate::types::tuple::{TupleSpec, TupleType};
1212
use crate::types::{
1313
KnownInstanceType, Type, TypeMapping, TypeRelation, TypeVarBoundOrConstraints, TypeVarInstance,
1414
TypeVarVariance, UnionType, declaration_type,
@@ -278,14 +278,14 @@ pub struct Specialization<'db> {
278278

279279
impl<'db> Specialization<'db> {
280280
/// Returns the tuple spec for a specialization of the `tuple` class.
281-
pub(crate) fn tuple(self, db: &'db dyn Db) -> &'db Tuple<'db> {
281+
pub(crate) fn tuple(self, db: &'db dyn Db) -> &'db TupleSpec<'db> {
282282
if let Some(tuple) = self.tuple_inner(db).map(|tuple_type| tuple_type.tuple(db)) {
283283
return tuple;
284284
}
285285
if let [element_type] = self.types(db) {
286-
return TupleType::new(db, Tuple::homogeneous(*element_type)).tuple(db);
286+
return TupleType::new(db, TupleSpec::homogeneous(*element_type)).tuple(db);
287287
}
288-
TupleType::new(db, Tuple::homogeneous(Type::unknown())).tuple(db)
288+
TupleType::new(db, TupleSpec::homogeneous(Type::unknown())).tuple(db)
289289
}
290290

291291
/// Returns the type that a typevar is mapped to, or None if the typevar isn't part of this
@@ -671,7 +671,7 @@ impl<'db> SpecializationBuilder<'db> {
671671
let formal_tuple = formal_tuple.tuple(self.db);
672672
let actual_tuple = actual_tuple.tuple(self.db);
673673
match (formal_tuple, actual_tuple) {
674-
(Tuple::Fixed(formal_tuple), Tuple::Fixed(actual_tuple)) => {
674+
(TupleSpec::Fixed(formal_tuple), TupleSpec::Fixed(actual_tuple)) => {
675675
if formal_tuple.len() == actual_tuple.len() {
676676
for (formal_element, actual_element) in formal_tuple.elements().zip(actual_tuple.elements()) {
677677
self.infer(formal_element, actual_element)?;
@@ -680,7 +680,7 @@ impl<'db> SpecializationBuilder<'db> {
680680
}
681681

682682
// TODO: Infer specializations of variable-length tuples
683-
(Tuple::Variable(_), _) | (_, Tuple::Variable(_)) => {}
683+
(TupleSpec::Variable(_), _) | (_, TupleSpec::Variable(_)) => {}
684684
}
685685
}
686686

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use crate::types::function::{
9595
use crate::types::generics::GenericContext;
9696
use crate::types::mro::MroErrorKind;
9797
use crate::types::signatures::{CallableSignature, Signature};
98-
use crate::types::tuple::{Tuple, TupleType};
98+
use crate::types::tuple::{TupleSpec, TupleType};
9999
use crate::types::unpacker::{UnpackResult, Unpacker};
100100
use crate::types::{
101101
BareTypeAliasType, CallDunderError, CallableType, ClassLiteral, ClassType, DataclassParams,
@@ -7862,16 +7862,16 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
78627862
/// see `<https://github.com/python/cpython/blob/9d6366b60d01305fc5e45100e0cd13e358aa397d/Objects/tupleobject.c#L637>`
78637863
fn infer_tuple_rich_comparison(
78647864
&mut self,
7865-
left: &Tuple<'db>,
7865+
left: &TupleSpec<'db>,
78667866
op: RichCompareOperator,
7867-
right: &Tuple<'db>,
7867+
right: &TupleSpec<'db>,
78687868
range: TextRange,
78697869
) -> Result<Type<'db>, CompareUnsupportedError<'db>> {
78707870
// If either tuple is variable length, we can make no assumptions about the relative
78717871
// lengths of the tuples, and therefore neither about how they compare lexicographically.
78727872
// TODO: Consider comparing the prefixes of the tuples, since that could give a comparison
78737873
// result regardless of how long the variable-length tuple is.
7874-
let (Tuple::Fixed(left), Tuple::Fixed(right)) = (left, right) else {
7874+
let (TupleSpec::Fixed(left), TupleSpec::Fixed(right)) = (left, right) else {
78757875
return Ok(Type::unknown());
78767876
};
78777877

@@ -8116,7 +8116,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
81168116
}
81178117
// Ex) Given `("a", 1, Null)[0:2]`, return `("a", 1)`
81188118
(Type::Tuple(tuple_ty), _, Some(SliceLiteral { start, stop, step })) => {
8119-
let Tuple::Fixed(tuple) = tuple_ty.tuple(self.db()) else {
8119+
let TupleSpec::Fixed(tuple) = tuple_ty.tuple(self.db()) else {
81208120
return todo_type!("slice into variable-length tuple");
81218121
};
81228122

@@ -8210,7 +8210,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
82108210
Type::IntLiteral(i64::from(bool)),
82118211
),
82128212
(Type::SpecialForm(SpecialFormType::Protocol), Type::Tuple(typevars), _) => {
8213-
let Tuple::Fixed(typevars) = typevars.tuple(self.db()) else {
8213+
let TupleSpec::Fixed(typevars) = typevars.tuple(self.db()) else {
82148214
// TODO: emit a diagnostic
82158215
return Type::unknown();
82168216
};
@@ -8235,7 +8235,7 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
82358235
todo_type!("doubly-specialized typing.Protocol")
82368236
}
82378237
(Type::SpecialForm(SpecialFormType::Generic), Type::Tuple(typevars), _) => {
8238-
let Tuple::Fixed(typevars) = typevars.tuple(self.db()) else {
8238+
let TupleSpec::Fixed(typevars) = typevars.tuple(self.db()) else {
82398239
// TODO: emit a diagnostic
82408240
return Type::unknown();
82418241
};
@@ -9229,7 +9229,7 @@ impl<'db> TypeInferenceBuilder<'db, '_> {
92299229
return result;
92309230
}
92319231

9232-
let mut element_types = Tuple::with_capacity(elements.len());
9232+
let mut element_types = TupleSpec::with_capacity(elements.len());
92339233

92349234
// Whether to infer `Todo` for the whole tuple
92359235
// (see docstring for `element_could_alter_type_of_whole_tuple`)

0 commit comments

Comments
 (0)