Skip to content

Commit 13e8d45

Browse files
stroxlermeta-codesync[bot]
authored andcommitted
Delete the disconnected ShapedArray type representation
Summary: Remove `ShapedArrayType` and `Type::ShapedArray` after retiring their construction and dispatch paths. Preserve `IntTuple` storage, normal class shape formatting, shared shape comparisons/conversions, and annotation-driven indexing. mypy_primer: +0/-0 under both default and strict presets on the matched corpus for this diff (Sandcastle job 36028800104034918). No type-checking differences were observed, so there are no added/fixed errors to classify. ___ landed-with-radar-review Differential Revision: D128206161 fbshipit-source-id: 3d1ee088db1410c316981ccfa67e225bc06306a2
1 parent a168c6e commit 13e8d45

17 files changed

Lines changed: 8 additions & 359 deletions

File tree

‎crates/pyrefly_types/src/display.rs‎

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ use crate::quantified::QuantifiedIdentity;
4242
use crate::quantified::QuantifiedKind;
4343
use crate::shaped_array::IntTuple;
4444
use crate::shaped_array::IntTupleView;
45-
use crate::shaped_array::ShapedArrayType;
4645
use crate::shaped_array::is_tuple_carrier_shape_middle;
4746
use crate::stdlib::Stdlib;
4847
use crate::tuple::Tuple;
@@ -205,9 +204,6 @@ impl<'a> TypeDisplayContext<'a> {
205204

206205
pub fn add(&mut self, t: &'a Type) {
207206
t.universe(&mut |t| {
208-
if let Type::ShapedArray(shaped_array) = t {
209-
self.add_qname(shaped_array.base_class.qname());
210-
}
211207
if let Type::DataFrame(schema) = t {
212208
self.add_qname(schema.underlying.qname());
213209
}
@@ -452,63 +448,6 @@ impl<'a> TypeDisplayContext<'a> {
452448
output.write_str("]")
453449
}
454450

455-
fn fmt_shaped_array(
456-
&self,
457-
shaped_array: &ShapedArrayType,
458-
output: &mut impl TypeOutput,
459-
) -> fmt::Result {
460-
let shape_idx = match shaped_array.tuple_carrier_shape_arg_index() {
461-
Some(index) => index,
462-
None => {
463-
output.write_qname(shaped_array.base_class.qname())?;
464-
let shape = shaped_array.shape();
465-
if !shape.is_shapeless() {
466-
output.write_str("[")?;
467-
output.write_str(&shape.to_string())?;
468-
output.write_str("]")?;
469-
}
470-
return Ok(());
471-
}
472-
};
473-
self.fmt_shaped_array_as_class(shaped_array, shape_idx, output)
474-
}
475-
476-
fn fmt_shaped_array_as_class(
477-
&self,
478-
shaped_array: &ShapedArrayType,
479-
shape_idx: usize,
480-
output: &mut impl TypeOutput,
481-
) -> fmt::Result {
482-
let targs = shaped_array.base_class.targs();
483-
let args = targs.as_slice();
484-
args.get(shape_idx)
485-
.expect("shape argument index should point to a class type argument");
486-
487-
output.write_qname(shaped_array.base_class.qname())?;
488-
let shape = shaped_array.shape();
489-
let display_count = targs.display_count().max(if shape.is_shapeless() {
490-
0
491-
} else {
492-
shape_idx + 1
493-
});
494-
if display_count == 0 {
495-
return Ok(());
496-
}
497-
498-
output.write_str("[")?;
499-
for (i, (param, arg)) in targs.iter_paired().take(display_count).enumerate() {
500-
if i > 0 {
501-
output.write_str(", ")?;
502-
}
503-
if i == shape_idx {
504-
self.fmt_shape_as_tuple_carrier(&shape, output)?;
505-
} else {
506-
self.fmt_targ(param, arg, output)?;
507-
}
508-
}
509-
output.write_str("]")
510-
}
511-
512451
fn fmt_shape_as_tuple_carrier(
513452
&self,
514453
shape: &IntTuple,
@@ -1103,7 +1042,6 @@ impl<'a> TypeDisplayContext<'a> {
11031042
}
11041043
output.write_str("]")
11051044
}
1106-
Type::ShapedArray(shaped_array) => self.fmt_shaped_array(shaped_array, output),
11071045
Type::IntTuple(int_tuple) => {
11081046
if int_tuple.is_shapeless() {
11091047
output.write_str("IntTuple")

‎crates/pyrefly_types/src/heap.rs‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use crate::param_spec::ParamSpec;
4040
use crate::quantified::Quantified;
4141
use crate::sentinel::Sentinel;
4242
use crate::shaped_array::IntTuple;
43-
use crate::shaped_array::ShapedArrayType;
4443
use crate::special_form::SpecialForm;
4544
use crate::tuple::Tuple;
4645
use crate::type_alias::TypeAliasData;
@@ -436,11 +435,6 @@ impl TypeHeap {
436435
Type::Int(symint)
437436
}
438437

439-
/// Create a `Type::ShapedArray` from a ShapedArrayType.
440-
pub fn mk_shaped_array(&self, shaped_array: ShapedArrayType) -> Type {
441-
Type::ShapedArray(Box::new(shaped_array))
442-
}
443-
444438
/// Create a `Type::IntTuple` from a IntTuple.
445439
pub fn mk_int_tuple(&self, int_tuple: IntTuple) -> Type {
446440
Type::IntTuple(Box::new(int_tuple))

‎crates/pyrefly_types/src/shaped_array.rs‎

Lines changed: 1 addition & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use pyrefly_derive::Visit;
1313
use pyrefly_derive::VisitMut;
1414
use pyrefly_util::display::commas_iter;
1515

16-
use crate::class::ClassType;
1716
use crate::dimension::Int;
1817
use crate::dimension::ShapeError;
1918
use crate::dimension::canonicalize;
@@ -28,140 +27,7 @@ use crate::tuple::Tuple;
2827
use crate::type_level_dsl::TypeShapeDslDomain;
2928
use crate::types::Type;
3029

31-
// ============================================================================
32-
// Shaped Array Types
33-
// ============================================================================
34-
35-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
36-
#[derive(Visit, VisitMut, TypeEq)]
37-
enum ShapedArrayShapeStorage {
38-
Inline(IntTuple),
39-
TupleCarrier { index: usize },
40-
}
41-
42-
/// A class instance with shape information.
43-
/// Example: Tensor[[2, 3]] represents a 2x3 tensor
44-
/// Example: Tensor (no brackets) represents a shapeless tensor (`IntTuple`)
45-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
46-
#[derive(Visit, VisitMut, TypeEq)]
47-
pub struct ShapedArrayType {
48-
/// Base shaped-array class (e.g., torch.Tensor)
49-
pub base_class: ClassType,
50-
shape: ShapedArrayShapeStorage,
51-
}
52-
53-
impl ShapedArrayType {
54-
/// Create a shaped-array type with shape information.
55-
pub fn new(base_class: ClassType, shape: IntTuple) -> Self {
56-
Self {
57-
base_class,
58-
shape: ShapedArrayShapeStorage::Inline(shape),
59-
}
60-
}
61-
62-
/// Create a shapeless shaped-array type (compatible with any shape).
63-
pub fn shapeless(base_class: ClassType) -> Self {
64-
Self {
65-
base_class,
66-
shape: ShapedArrayShapeStorage::Inline(IntTuple::shapeless()),
67-
}
68-
}
69-
70-
pub fn with_tuple_carrier_shape_arg(mut self, index: usize) -> Self {
71-
self.shape = ShapedArrayShapeStorage::TupleCarrier { index };
72-
self
73-
}
74-
75-
pub fn to_type(self) -> Type {
76-
Type::ShapedArray(Box::new(self))
77-
}
78-
79-
pub fn tuple_carrier_shape_arg_index(&self) -> Option<usize> {
80-
match self.shape {
81-
ShapedArrayShapeStorage::Inline(_) => None,
82-
ShapedArrayShapeStorage::TupleCarrier { index } => Some(index),
83-
}
84-
}
85-
86-
pub fn set_tuple_carrier_shape_arg(&mut self, index: usize) {
87-
self.shape = ShapedArrayShapeStorage::TupleCarrier { index };
88-
}
89-
90-
pub fn shape(&self) -> IntTuple {
91-
match &self.shape {
92-
ShapedArrayShapeStorage::Inline(shape) => shape.clone(),
93-
ShapedArrayShapeStorage::TupleCarrier { index } => {
94-
let shape_arg = self
95-
.base_class
96-
.targs()
97-
.as_slice()
98-
.get(*index)
99-
.expect("shape argument index should point to a class type argument");
100-
IntTuple::from_shape_arg_or_tuple_carrier(shape_arg)
101-
.expect("registered shaped-array shape argument should project to IntTuple")
102-
}
103-
}
104-
}
105-
106-
pub fn set_shape(&mut self, shape: IntTuple) {
107-
match &mut self.shape {
108-
ShapedArrayShapeStorage::Inline(stored_shape) => *stored_shape = shape,
109-
ShapedArrayShapeStorage::TupleCarrier { index } => {
110-
let shape_arg = self
111-
.base_class
112-
.targs_mut()
113-
.as_mut()
114-
.get_mut(*index)
115-
.expect("shape argument index should point to a class type argument");
116-
*shape_arg = shape.to_shape_arg_type();
117-
}
118-
}
119-
}
120-
121-
/// Returns rank if shape is concrete, None for variadic/shapeless
122-
pub fn rank(&self) -> Option<usize> {
123-
match self.shape().view() {
124-
IntTupleView::Concrete(dims) => Some(dims.len()),
125-
IntTupleView::Gradual | IntTupleView::Unpacked { .. } => None,
126-
}
127-
}
128-
129-
/// Returns true if the shaped array has no shape information.
130-
/// (represented as a gradual `IntTuple`)
131-
pub fn is_shapeless(&self) -> bool {
132-
is_shapeless(&self.shape())
133-
}
134-
135-
/// Materialize gradual dimensions of an inline shape (see `Int::materialize`).
136-
/// `TupleCarrier` shapes store their dimensions as `Type` arguments on
137-
/// `base_class`, so they are materialized by the generic `Type::materialize`
138-
/// traversal instead.
139-
pub fn materialize_inline_shape(&mut self) {
140-
if let ShapedArrayShapeStorage::Inline(shape) = &mut self.shape {
141-
shape.materialize();
142-
}
143-
}
144-
}
145-
146-
impl Display for ShapedArrayType {
147-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148-
let shape = self.shape();
149-
if is_shapeless(&shape) {
150-
write!(f, "{}", self.base_class.name())
151-
} else if self.tuple_carrier_shape_arg_index().is_some() {
152-
write!(
153-
f,
154-
"{}[{}]",
155-
self.base_class.name(),
156-
fmt_tuple_carrier(&shape)
157-
)
158-
} else {
159-
write!(f, "{}[{}]", self.base_class.name(), shape)
160-
}
161-
}
162-
}
163-
164-
/// Shape of a shaped array.
30+
/// An integer tuple describing an array shape.
16531
///
16632
/// The storage is deliberately not a `Tuple`: fixed dimensions are always
16733
/// canonical `Int`s, while variadic middles carry the original tuple/type
@@ -729,32 +595,6 @@ fn fmt_unpacked_middle(middle: &Type) -> String {
729595
}
730596
}
731597

732-
fn fmt_tuple_carrier(shape: &IntTuple) -> String {
733-
match shape.view() {
734-
IntTupleView::Concrete(dims) => {
735-
format!("[{}]", commas_iter(|| dims.iter()))
736-
}
737-
IntTupleView::Gradual => {
738-
// No unbounded shape reaches here: the only caller (`Display`) handles
739-
// the shapeless `IntTuple` case before calling this.
740-
unreachable!("shapeless IntTuple is handled before fmt_tuple_carrier")
741-
}
742-
IntTupleView::Unpacked {
743-
prefix,
744-
middle,
745-
suffix,
746-
} => {
747-
if prefix.is_empty() && suffix.is_empty() && is_tuple_carrier_shape_middle(middle) {
748-
return middle.to_string();
749-
}
750-
let mut parts: Vec<String> = prefix.iter().map(|d| d.to_string()).collect();
751-
parts.push(format!("*{}", fmt_unpacked_middle(middle)));
752-
parts.extend(suffix.iter().map(|d| d.to_string()));
753-
format!("[{}]", parts.join(", "))
754-
}
755-
}
756-
}
757-
758598
// ============================================================================
759599
// Tuple-carrier conversion
760600
// ============================================================================

‎crates/pyrefly_types/src/types.rs‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ use crate::quantified::Quantified;
6262
use crate::sentinel::Sentinel;
6363
use crate::series::SeriesSchema;
6464
use crate::shaped_array::IntTuple;
65-
use crate::shaped_array::ShapedArrayType;
6665
use crate::simplify::unions;
6766
use crate::special_form::SpecialForm;
6867
use crate::stdlib::Stdlib;
@@ -902,9 +901,6 @@ pub enum Type {
902901
/// For a TypedDict type `C`, `Partial[C]` represents an object with any subset of read-write
903902
/// keys from `C`, where each present key has the same value type as in `C`.
904903
PartialTypedDict(TypedDict),
905-
/// Shaped-array type with shape information.
906-
/// Example: Tensor[2, 3] represents a 2x3 tensor
907-
ShapedArray(Box<ShapedArrayType>),
908904
/// First-class tensor shape tuple.
909905
IntTuple(Box<IntTuple>),
910906
/// Named integer values captured by the experimental shape extensions.
@@ -1031,7 +1027,6 @@ impl Visit for Type {
10311027
Type::ClassType(x) => x.visit(f),
10321028
Type::TypedDict(x) => x.visit(f),
10331029
Type::PartialTypedDict(x) => x.visit(f),
1034-
Type::ShapedArray(x) => x.visit(f),
10351030
Type::IntTuple(x) => x.visit(f),
10361031
Type::NamedInts(x) => x.visit(f),
10371032
Type::DataFrame(x) => x.visit(f),
@@ -1095,7 +1090,6 @@ impl VisitMut for Type {
10951090
Type::ClassType(x) => x.visit_mut(f),
10961091
Type::TypedDict(x) => x.visit_mut(f),
10971092
Type::PartialTypedDict(x) => x.visit_mut(f),
1098-
Type::ShapedArray(x) => x.visit_mut(f),
10991093
Type::IntTuple(x) => x.visit_mut(f),
11001094
Type::NamedInts(x) => x.visit_mut(f),
11011095
Type::DataFrame(x) => x.visit_mut(f),
@@ -2465,7 +2459,6 @@ impl Type {
24652459
| Type::ClassType(_)
24662460
| Type::TypedDict(_)
24672461
| Type::PartialTypedDict(_)
2468-
| Type::ShapedArray(_)
24692462
| Type::IntTuple(_)
24702463
| Type::NamedInts(_)
24712464
| Type::DataFrame(_)
@@ -2551,7 +2544,6 @@ impl Type {
25512544
match self {
25522545
Type::Int(dim) => dim.materialize(),
25532546
Type::IntTuple(tuple) => tuple.materialize(),
2554-
Type::ShapedArray(shaped) => shaped.materialize_inline_shape(),
25552547
_ => {}
25562548
}
25572549
self.transform_toplevel_callable_signatures(|callable: &mut Callable, _| {

‎pyrefly/lib/alt/attr.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2656,7 +2656,6 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
26562656
value_ty,
26572657
)))
26582658
}
2659-
Type::ShapedArray(tensor) => acc.push(AttributeBase1::ClassInstance(tensor.base_class)),
26602659
Type::DataFrame(schema) => self.as_attribute_base1(schema.underlying_type(), acc),
26612660
Type::Series(schema) => self.as_attribute_base1(schema.underlying_type(), acc),
26622661
Type::Int(_) => {

‎pyrefly/lib/alt/class/class_metadata.rs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,15 +1757,6 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
17571757
}
17581758
match self.untype_opt(ty.clone(), x.range(), &errors) {
17591759
None => BaseClassParseResult::InvalidType(ty, x.range()),
1760-
Some(Type::ShapedArray(array))
1761-
if self
1762-
.bindings()
1763-
.shape_declarations
1764-
.commented_bases
1765-
.contains_key(&x.range()) =>
1766-
{
1767-
parse_base_class_type(self.heap.mk_class_type(array.base_class))
1768-
}
17691760
Some(ty) => parse_base_class_type(ty),
17701761
}
17711762
}

0 commit comments

Comments
 (0)