Skip to content

Commit cc66653

Browse files
committed
remove details from TypeVar display
1 parent a7c4f1c commit cc66653

File tree

6 files changed

+27
-125
lines changed

6 files changed

+27
-125
lines changed

crates/ty_python_semantic/resources/mdtest/generics/legacy/variables.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ from typing import TypeVar
2020

2121
T = TypeVar("T")
2222
reveal_type(type(T)) # revealed: <class 'TypeVar'>
23-
reveal_type(T) # revealed: typing.TypeVar("T")
23+
reveal_type(T) # revealed: typing.TypeVar
2424
reveal_type(T.__name__) # revealed: Literal["T"]
2525
```
2626

@@ -80,7 +80,7 @@ from typing import TypeVar
8080

8181
T = TypeVar("T", default=int)
8282
reveal_type(type(T)) # revealed: <class 'TypeVar'>
83-
reveal_type(T) # revealed: typing.TypeVar("T", default=int)
83+
reveal_type(T) # revealed: typing.TypeVar
8484
reveal_type(T.__default__) # revealed: int
8585
reveal_type(T.__bound__) # revealed: None
8686
reveal_type(T.__constraints__) # revealed: tuple[()]
@@ -116,7 +116,7 @@ from typing import TypeVar
116116

117117
T = TypeVar("T", bound=int)
118118
reveal_type(type(T)) # revealed: <class 'TypeVar'>
119-
reveal_type(T) # revealed: typing.TypeVar("T", bound=int)
119+
reveal_type(T) # revealed: typing.TypeVar
120120
reveal_type(T.__bound__) # revealed: int
121121
reveal_type(T.__constraints__) # revealed: tuple[()]
122122

@@ -131,7 +131,7 @@ from typing import TypeVar
131131

132132
T = TypeVar("T", int, str)
133133
reveal_type(type(T)) # revealed: <class 'TypeVar'>
134-
reveal_type(T) # revealed: typing.TypeVar("T", int, str)
134+
reveal_type(T) # revealed: typing.TypeVar
135135
reveal_type(T.__constraints__) # revealed: tuple[int, str]
136136

137137
S = TypeVar("S")

crates/ty_python_semantic/resources/mdtest/generics/pep695/variables.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ instances of `typing.TypeVar`, just like legacy type variables.
1717
```py
1818
def f[T]():
1919
reveal_type(type(T)) # revealed: <class 'TypeVar'>
20-
reveal_type(T) # revealed: typing.TypeVar("T")
20+
reveal_type(T) # revealed: typing.TypeVar
2121
reveal_type(T.__name__) # revealed: Literal["T"]
2222
```
2323

@@ -33,7 +33,7 @@ python-version = "3.13"
3333
```py
3434
def f[T = int]():
3535
reveal_type(type(T)) # revealed: <class 'TypeVar'>
36-
reveal_type(T) # revealed: typing.TypeVar("T", default=int)
36+
reveal_type(T) # revealed: typing.TypeVar
3737
reveal_type(T.__default__) # revealed: int
3838
reveal_type(T.__bound__) # revealed: None
3939
reveal_type(T.__constraints__) # revealed: tuple[()]
@@ -66,7 +66,7 @@ class Invalid[S = T]: ...
6666
```py
6767
def f[T: int]():
6868
reveal_type(type(T)) # revealed: <class 'TypeVar'>
69-
reveal_type(T) # revealed: typing.TypeVar("T", bound=int)
69+
reveal_type(T) # revealed: typing.TypeVar
7070
reveal_type(T.__bound__) # revealed: int
7171
reveal_type(T.__constraints__) # revealed: tuple[()]
7272

@@ -79,7 +79,7 @@ def g[S]():
7979
```py
8080
def f[T: (int, str)]():
8181
reveal_type(type(T)) # revealed: <class 'TypeVar'>
82-
reveal_type(T) # revealed: typing.TypeVar("T", int, str)
82+
reveal_type(T) # revealed: typing.TypeVar
8383
reveal_type(T.__constraints__) # revealed: tuple[int, str]
8484
reveal_type(T.__bound__) # revealed: None
8585

crates/ty_python_semantic/resources/mdtest/snapshots/union_call.md_-_Calling_a_union_of_f…_-_Try_to_cover_all_pos…_-_Cover_non-keyword_re…_(707b284610419a54).snap

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ error[call-non-callable]: Object of type `Literal[5]` is not callable
8686
| ^^^^
8787
|
8888
info: Union variant `Literal[5]` is incompatible with this call site
89-
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T: str](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
89+
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
9090
info: rule `call-non-callable` is enabled by default
9191

9292
```
@@ -101,7 +101,7 @@ error[call-non-callable]: Object of type `PossiblyNotCallable` is not callable (
101101
| ^^^^
102102
|
103103
info: Union variant `PossiblyNotCallable` is incompatible with this call site
104-
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T: str](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
104+
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
105105
info: rule `call-non-callable` is enabled by default
106106

107107
```
@@ -116,7 +116,7 @@ error[missing-argument]: No argument provided for required parameter `b` of func
116116
| ^^^^
117117
|
118118
info: Union variant `def f3(a: int, b: int) -> int` is incompatible with this call site
119-
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T: str](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
119+
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
120120
info: rule `missing-argument` is enabled by default
121121

122122
```
@@ -152,7 +152,7 @@ info: Overload implementation defined here
152152
28 | return x + y if x and y else None
153153
|
154154
info: Union variant `Overload[() -> None, (x: str, y: str) -> str]` is incompatible with this call site
155-
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T: str](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
155+
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
156156
info: rule `no-matching-overload` is enabled by default
157157

158158
```
@@ -176,7 +176,7 @@ info: Function defined here
176176
8 | return 0
177177
|
178178
info: Union variant `def f2(name: str) -> int` is incompatible with this call site
179-
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T: str](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
179+
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
180180
info: rule `invalid-argument-type` is enabled by default
181181

182182
```
@@ -199,8 +199,8 @@ info: Type variable defined here
199199
| ^^^^^^
200200
14 | return 0
201201
|
202-
info: Union variant `def f4[T: str](x: T@f4) -> int` is incompatible with this call site
203-
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T: str](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
202+
info: Union variant `def f4[T](x: T@f4) -> int` is incompatible with this call site
203+
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
204204
info: rule `invalid-argument-type` is enabled by default
205205

206206
```
@@ -227,7 +227,7 @@ info: Matching overload defined here
227227
info: Non-matching overloads for function `f5`:
228228
info: () -> None
229229
info: Union variant `Overload[() -> None, (x: str) -> str]` is incompatible with this call site
230-
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T: str](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
230+
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
231231
info: rule `invalid-argument-type` is enabled by default
232232

233233
```
@@ -242,7 +242,7 @@ error[too-many-positional-arguments]: Too many positional arguments to function
242242
| ^
243243
|
244244
info: Union variant `def f1() -> int` is incompatible with this call site
245-
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T: str](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
245+
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4[T](x: T@f4) -> int) | Literal[5] | (Overload[() -> None, (x: str) -> str]) | (Overload[() -> None, (x: str, y: str) -> str]) | PossiblyNotCallable`
246246
info: rule `too-many-positional-arguments` is enabled by default
247247

248248
```

crates/ty_python_semantic/src/types.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6423,9 +6423,7 @@ impl<'db> KnownInstanceType<'db> {
64236423
// This is a legacy `TypeVar` _outside_ of any generic class or function, so we render
64246424
// it as an instance of `typing.TypeVar`. Inside of a generic class or function, we'll
64256425
// have a `Type::TypeVar(_)`, which is rendered as the typevar's name.
6426-
KnownInstanceType::TypeVar(typevar) => {
6427-
write!(f, "typing.TypeVar({})", typevar.display(self.db))
6428-
}
6426+
KnownInstanceType::TypeVar(_) => f.write_str("typing.TypeVar"),
64296427
KnownInstanceType::Deprecated(_) => f.write_str("warnings.deprecated"),
64306428
KnownInstanceType::Field(field) => {
64316429
f.write_str("dataclasses.Field[")?;

crates/ty_python_semantic/src/types/display.rs

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ use crate::types::generics::{GenericContext, Specialization};
1414
use crate::types::signatures::{CallableSignature, Parameter, Parameters, Signature};
1515
use crate::types::tuple::TupleSpec;
1616
use crate::types::{
17-
BoundTypeVarInstance, CallableType, IntersectionType, KnownClass, MethodWrapperKind, Protocol,
18-
StringLiteralType, SubclassOfInner, Type, TypeVarBoundOrConstraints, TypeVarInstance,
19-
UnionType, WrapperDescriptorKind,
17+
CallableType, IntersectionType, KnownClass, MethodWrapperKind, Protocol, StringLiteralType,
18+
SubclassOfInner, Type, UnionType, WrapperDescriptorKind,
2019
};
2120

2221
impl<'db> Type<'db> {
@@ -417,83 +416,6 @@ impl Display for DisplayGenericAlias<'_> {
417416
}
418417
}
419418

420-
impl<'db> TypeVarInstance<'db> {
421-
pub(crate) fn display(self, db: &'db dyn Db) -> DisplayTypeVarInstance<'db> {
422-
DisplayTypeVarInstance { typevar: self, db }
423-
}
424-
}
425-
426-
pub(crate) struct DisplayTypeVarInstance<'db> {
427-
typevar: TypeVarInstance<'db>,
428-
db: &'db dyn Db,
429-
}
430-
431-
impl Display for DisplayTypeVarInstance<'_> {
432-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
433-
display_quoted_string(self.typevar.name(self.db)).fmt(f)?;
434-
match self.typevar.bound_or_constraints(self.db) {
435-
Some(TypeVarBoundOrConstraints::UpperBound(bound)) => {
436-
write!(f, ", bound={}", bound.display(self.db))?;
437-
}
438-
Some(TypeVarBoundOrConstraints::Constraints(constraints)) => {
439-
for constraint in constraints.iter(self.db) {
440-
write!(f, ", {}", constraint.display(self.db))?;
441-
}
442-
}
443-
None => {}
444-
}
445-
if let Some(default_type) = self.typevar.default_type(self.db) {
446-
write!(f, ", default={}", default_type.display(self.db))?;
447-
}
448-
Ok(())
449-
}
450-
}
451-
452-
impl<'db> BoundTypeVarInstance<'db> {
453-
pub(crate) fn display(self, db: &'db dyn Db) -> DisplayBoundTypeVarInstance<'db> {
454-
DisplayBoundTypeVarInstance {
455-
bound_typevar: self,
456-
db,
457-
}
458-
}
459-
}
460-
461-
pub(crate) struct DisplayBoundTypeVarInstance<'db> {
462-
bound_typevar: BoundTypeVarInstance<'db>,
463-
db: &'db dyn Db,
464-
}
465-
466-
impl Display for DisplayBoundTypeVarInstance<'_> {
467-
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
468-
// This looks very much like DisplayTypeVarInstance::fmt, but note that we have typevar
469-
// default values in a subtly different way: if the default value contains other typevars,
470-
// here those must be bound as well, whereas in DisplayTypeVarInstance they should not. See
471-
// BoundTypeVarInstance::default_ty for more details.
472-
let typevar = self.bound_typevar.typevar(self.db);
473-
f.write_str(typevar.name(self.db))?;
474-
match typevar.bound_or_constraints(self.db) {
475-
Some(TypeVarBoundOrConstraints::UpperBound(bound)) => {
476-
write!(f, ": {}", bound.display(self.db))?;
477-
}
478-
Some(TypeVarBoundOrConstraints::Constraints(constraints)) => {
479-
f.write_str(": (")?;
480-
for (idx, constraint) in constraints.iter(self.db).enumerate() {
481-
if idx > 0 {
482-
f.write_str(", ")?;
483-
}
484-
constraint.display(self.db).fmt(f)?;
485-
}
486-
f.write_char(')')?;
487-
}
488-
None => {}
489-
}
490-
if let Some(default_type) = self.bound_typevar.default_type(self.db) {
491-
write!(f, " = {}", default_type.display(self.db))?;
492-
}
493-
Ok(())
494-
}
495-
}
496-
497419
impl<'db> GenericContext<'db> {
498420
pub fn display(&'db self, db: &'db dyn Db) -> DisplayGenericContext<'db> {
499421
DisplayGenericContext {
@@ -545,7 +467,7 @@ impl Display for DisplayGenericContext<'_> {
545467
if idx > 0 {
546468
f.write_str(", ")?;
547469
}
548-
bound_typevar.display(self.db).fmt(f)?;
470+
f.write_str(bound_typevar.typevar(self.db).name(self.db))?;
549471
}
550472
f.write_char(']')
551473
}

crates/ty_python_semantic/src/types/infer.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11565,32 +11565,14 @@ mod tests {
1156511565
);
1156611566
};
1156711567

11568-
check_typevar("T", "typing.TypeVar(\"T\")", None, None, None);
11569-
check_typevar("U", "typing.TypeVar(\"U\", bound=A)", Some("A"), None, None);
11570-
check_typevar(
11571-
"V",
11572-
"typing.TypeVar(\"V\", A, B)",
11573-
None,
11574-
Some(&["A", "B"]),
11575-
None,
11576-
);
11577-
check_typevar(
11578-
"W",
11579-
"typing.TypeVar(\"W\", default=A)",
11580-
None,
11581-
None,
11582-
Some("A"),
11583-
);
11584-
check_typevar(
11585-
"X",
11586-
"typing.TypeVar(\"X\", bound=A, default=A1)",
11587-
Some("A"),
11588-
None,
11589-
Some("A1"),
11590-
);
11568+
check_typevar("T", "typing.TypeVar", None, None, None);
11569+
check_typevar("U", "typing.TypeVar", Some("A"), None, None);
11570+
check_typevar("V", "typing.TypeVar", None, Some(&["A", "B"]), None);
11571+
check_typevar("W", "typing.TypeVar", None, None, Some("A"));
11572+
check_typevar("X", "typing.TypeVar", Some("A"), None, Some("A1"));
1159111573

1159211574
// a typevar with less than two constraints is treated as unconstrained
11593-
check_typevar("Y", "typing.TypeVar(\"Y\")", None, None, None);
11575+
check_typevar("Y", "typing.TypeVar", None, None, None);
1159411576
}
1159511577

1159611578
/// Test that a symbol known to be unbound in a scope does not still trigger cycle-causing

0 commit comments

Comments
 (0)