Skip to content

Commit 0847f8c

Browse files
committed
babby's first test
1 parent 87b32ba commit 0847f8c

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Creating a specialization from a constraint set
2+
3+
```toml
4+
[environment]
5+
python-version = "3.12"
6+
```
7+
8+
## initial tests
9+
10+
```py
11+
from ty_extensions import ConstraintSet, generic_context
12+
13+
def f[T]():
14+
# revealed: ty_extensions.Specialization[T@f = object]
15+
reveal_type(generic_context(f).specialize_constrained(ConstraintSet.always()))
16+
```

crates/ty_python_semantic/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8088,7 +8088,7 @@ impl<'db> KnownInstanceType<'db> {
80888088
write!(
80898089
f,
80908090
"ty_extensions.Specialization{}",
8091-
specialization.display(self.db)
8091+
specialization.display_full(self.db)
80928092
)
80938093
}
80948094
KnownInstanceType::UnionType(_) => f.write_str("types.UnionType"),

crates/ty_python_semantic/src/types/display.rs

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -991,37 +991,50 @@ impl Display for DisplayGenericContext<'_> {
991991
}
992992

993993
impl<'db> Specialization<'db> {
994-
pub fn display(&'db self, db: &'db dyn Db) -> DisplaySpecialization<'db> {
994+
pub fn display(self, db: &'db dyn Db) -> DisplaySpecialization<'db> {
995995
self.display_short(db, TupleSpecialization::No, DisplaySettings::default())
996996
}
997997

998+
pub(crate) fn display_full(self, db: &'db dyn Db) -> DisplaySpecialization<'db> {
999+
DisplaySpecialization {
1000+
specialization: self,
1001+
db,
1002+
tuple_specialization: TupleSpecialization::No,
1003+
settings: DisplaySettings::default(),
1004+
full: true,
1005+
}
1006+
}
1007+
9981008
/// Renders the specialization as it would appear in a subscript expression, e.g. `[int, str]`.
9991009
pub fn display_short(
1000-
&'db self,
1010+
self,
10011011
db: &'db dyn Db,
10021012
tuple_specialization: TupleSpecialization,
10031013
settings: DisplaySettings<'db>,
10041014
) -> DisplaySpecialization<'db> {
10051015
DisplaySpecialization {
1006-
types: self.types(db),
1016+
specialization: self,
10071017
db,
10081018
tuple_specialization,
10091019
settings,
1020+
full: false,
10101021
}
10111022
}
10121023
}
10131024

10141025
pub struct DisplaySpecialization<'db> {
1015-
types: &'db [Type<'db>],
1026+
specialization: Specialization<'db>,
10161027
db: &'db dyn Db,
10171028
tuple_specialization: TupleSpecialization,
10181029
settings: DisplaySettings<'db>,
1030+
full: bool,
10191031
}
10201032

1021-
impl Display for DisplaySpecialization<'_> {
1022-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1033+
impl DisplaySpecialization<'_> {
1034+
fn fmt_normal(&self, f: &mut Formatter<'_>) -> fmt::Result {
10231035
f.write_char('[')?;
1024-
for (idx, ty) in self.types.iter().enumerate() {
1036+
let types = self.specialization.types(self.db);
1037+
for (idx, ty) in types.iter().enumerate() {
10251038
if idx > 0 {
10261039
f.write_str(", ")?;
10271040
}
@@ -1032,6 +1045,37 @@ impl Display for DisplaySpecialization<'_> {
10321045
}
10331046
f.write_char(']')
10341047
}
1048+
1049+
fn fmt_full(&self, f: &mut Formatter<'_>) -> fmt::Result {
1050+
f.write_char('[')?;
1051+
let variables = self
1052+
.specialization
1053+
.generic_context(self.db)
1054+
.variables(self.db);
1055+
let types = self.specialization.types(self.db);
1056+
for (idx, (bound_typevar, ty)) in variables.zip(types).enumerate() {
1057+
if idx > 0 {
1058+
f.write_str(", ")?;
1059+
}
1060+
write!(
1061+
f,
1062+
"{} = {}",
1063+
bound_typevar.identity(self.db).display(self.db),
1064+
ty.display_with(self.db, self.settings.clone()),
1065+
)?;
1066+
}
1067+
f.write_char(']')
1068+
}
1069+
}
1070+
1071+
impl Display for DisplaySpecialization<'_> {
1072+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
1073+
if self.full {
1074+
self.fmt_full(f)
1075+
} else {
1076+
self.fmt_normal(f)
1077+
}
1078+
}
10351079
}
10361080

10371081
#[derive(Debug, Clone, Copy, PartialEq, Eq)]

0 commit comments

Comments
 (0)