Skip to content

Commit 1b28fc1

Browse files
authored
[ty] Add more random TypeDetails and tests (astral-sh#21546)
1 parent 290a572 commit 1b28fc1

File tree

2 files changed

+428
-74
lines changed

2 files changed

+428
-74
lines changed

crates/ty_ide/src/inlay_hints.rs

Lines changed: 295 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,24 @@ mod tests {
971971
w[: tuple[int, str]] = z
972972
973973
---------------------------------------------
974+
info[inlay-hint-location]: Inlay Hint Target
975+
--> stdlib/builtins.pyi:2695:7
976+
|
977+
2694 | @disjoint_base
978+
2695 | class tuple(Sequence[_T_co]):
979+
| ^^^^^
980+
2696 | """Built-in immutable sequence.
981+
|
982+
info: Source
983+
--> main2.py:8:5
984+
|
985+
7 | x = (1, 'abc')
986+
8 | y[: tuple[Literal[1], Literal["abc"]]] = x
987+
| ^^^^^
988+
9 | z[: tuple[int, str]] = (i(1), s('abc'))
989+
10 | w[: tuple[int, str]] = z
990+
|
991+
974992
info[inlay-hint-location]: Inlay Hint Target
975993
--> stdlib/typing.pyi:351:1
976994
|
@@ -1009,6 +1027,24 @@ mod tests {
10091027
10 | w[: tuple[int, str]] = z
10101028
|
10111029
1030+
info[inlay-hint-location]: Inlay Hint Target
1031+
--> stdlib/builtins.pyi:2695:7
1032+
|
1033+
2694 | @disjoint_base
1034+
2695 | class tuple(Sequence[_T_co]):
1035+
| ^^^^^
1036+
2696 | """Built-in immutable sequence.
1037+
|
1038+
info: Source
1039+
--> main2.py:9:5
1040+
|
1041+
7 | x = (1, 'abc')
1042+
8 | y[: tuple[Literal[1], Literal["abc"]]] = x
1043+
9 | z[: tuple[int, str]] = (i(1), s('abc'))
1044+
| ^^^^^
1045+
10 | w[: tuple[int, str]] = z
1046+
|
1047+
10121048
info[inlay-hint-location]: Inlay Hint Target
10131049
--> stdlib/builtins.pyi:348:7
10141050
|
@@ -1047,6 +1083,23 @@ mod tests {
10471083
10 | w[: tuple[int, str]] = z
10481084
|
10491085
1086+
info[inlay-hint-location]: Inlay Hint Target
1087+
--> stdlib/builtins.pyi:2695:7
1088+
|
1089+
2694 | @disjoint_base
1090+
2695 | class tuple(Sequence[_T_co]):
1091+
| ^^^^^
1092+
2696 | """Built-in immutable sequence.
1093+
|
1094+
info: Source
1095+
--> main2.py:10:5
1096+
|
1097+
8 | y[: tuple[Literal[1], Literal["abc"]]] = x
1098+
9 | z[: tuple[int, str]] = (i(1), s('abc'))
1099+
10 | w[: tuple[int, str]] = z
1100+
| ^^^^^
1101+
|
1102+
10501103
info[inlay-hint-location]: Inlay Hint Target
10511104
--> stdlib/builtins.pyi:348:7
10521105
|
@@ -2343,7 +2396,7 @@ mod tests {
23432396
"#,
23442397
);
23452398

2346-
assert_snapshot!(test.inlay_hints(), @r"
2399+
assert_snapshot!(test.inlay_hints(), @r#"
23472400
class MyClass:
23482401
def __init__(self):
23492402
self.x: int = 1
@@ -2373,6 +2426,24 @@ mod tests {
23732426
8 | a[: MyClass], b[: MyClass] = MyClass(), MyClass()
23742427
|
23752428
2429+
info[inlay-hint-location]: Inlay Hint Target
2430+
--> stdlib/builtins.pyi:2695:7
2431+
|
2432+
2694 | @disjoint_base
2433+
2695 | class tuple(Sequence[_T_co]):
2434+
| ^^^^^
2435+
2696 | """Built-in immutable sequence.
2436+
|
2437+
info: Source
2438+
--> main2.py:7:5
2439+
|
2440+
6 | x[: MyClass] = MyClass()
2441+
7 | y[: tuple[MyClass, MyClass]] = (MyClass(), MyClass())
2442+
| ^^^^^
2443+
8 | a[: MyClass], b[: MyClass] = MyClass(), MyClass()
2444+
9 | c[: MyClass], d[: MyClass] = (MyClass(), MyClass())
2445+
|
2446+
23762447
info[inlay-hint-location]: Inlay Hint Target
23772448
--> main.py:2:7
23782449
|
@@ -2478,7 +2549,7 @@ mod tests {
24782549
9 | c[: MyClass], d[: MyClass] = (MyClass(), MyClass())
24792550
| ^^^^^^^
24802551
|
2481-
");
2552+
"#);
24822553
}
24832554

24842555
#[test]
@@ -2527,6 +2598,25 @@ mod tests {
25272598
5 | self.y[: tuple[U@MyClass, U@MyClass]] = y
25282599
|
25292600
2601+
info[inlay-hint-location]: Inlay Hint Target
2602+
--> stdlib/builtins.pyi:2695:7
2603+
|
2604+
2694 | @disjoint_base
2605+
2695 | class tuple(Sequence[_T_co]):
2606+
| ^^^^^
2607+
2696 | """Built-in immutable sequence.
2608+
|
2609+
info: Source
2610+
--> main2.py:5:18
2611+
|
2612+
3 | def __init__(self, x: list[T], y: tuple[U, U]):
2613+
4 | self.x[: list[T@MyClass]] = x
2614+
5 | self.y[: tuple[U@MyClass, U@MyClass]] = y
2615+
| ^^^^^
2616+
6 |
2617+
7 | x[: MyClass[Unknown | int, str]] = MyClass([x=][42], [y=]("a", "b"))
2618+
|
2619+
25302620
info[inlay-hint-location]: Inlay Hint Target
25312621
--> main.py:2:7
25322622
|
@@ -2646,6 +2736,24 @@ mod tests {
26462736
9 | a[: MyClass[Unknown | int, str]], b[: MyClass[Unknown | int, str]] = MyClass([x=][42], [y=]("a", "b")), MyClass([x=][42], [y=]("a", "b…
26472737
|
26482738
2739+
info[inlay-hint-location]: Inlay Hint Target
2740+
--> stdlib/builtins.pyi:2695:7
2741+
|
2742+
2694 | @disjoint_base
2743+
2695 | class tuple(Sequence[_T_co]):
2744+
| ^^^^^
2745+
2696 | """Built-in immutable sequence.
2746+
|
2747+
info: Source
2748+
--> main2.py:8:5
2749+
|
2750+
7 | x[: MyClass[Unknown | int, str]] = MyClass([x=][42], [y=]("a", "b"))
2751+
8 | y[: tuple[MyClass[Unknown | int, str], MyClass[Unknown | int, str]]] = (MyClass([x=][42], [y=]("a", "b")), MyClass([x=][42], [y=]("a",…
2752+
| ^^^^^
2753+
9 | a[: MyClass[Unknown | int, str]], b[: MyClass[Unknown | int, str]] = MyClass([x=][42], [y=]("a", "b")), MyClass([x=][42], [y=]("a", "b…
2754+
10 | c[: MyClass[Unknown | int, str]], d[: MyClass[Unknown | int, str]] = (MyClass([x=][42], [y=]("a", "b")), MyClass([x=][42], [y=]("a", "…
2755+
|
2756+
26492757
info[inlay-hint-location]: Inlay Hint Target
26502758
--> main.py:2:7
26512759
|
@@ -4830,6 +4938,191 @@ mod tests {
48304938
"#);
48314939
}
48324940

4941+
#[test]
4942+
fn test_literal_group() {
4943+
let mut test = inlay_hint_test(
4944+
r#"
4945+
def branch(cond: int):
4946+
if cond < 10:
4947+
x = 1
4948+
elif cond < 20:
4949+
x = 2
4950+
elif cond < 30:
4951+
x = 3
4952+
elif cond < 40:
4953+
x = "hello"
4954+
else:
4955+
x = None
4956+
y = x"#,
4957+
);
4958+
4959+
assert_snapshot!(test.inlay_hints(), @r#"
4960+
def branch(cond: int):
4961+
if cond < 10:
4962+
x = 1
4963+
elif cond < 20:
4964+
x = 2
4965+
elif cond < 30:
4966+
x = 3
4967+
elif cond < 40:
4968+
x = "hello"
4969+
else:
4970+
x = None
4971+
y[: Literal[1, 2, 3, "hello"] | None] = x
4972+
---------------------------------------------
4973+
info[inlay-hint-location]: Inlay Hint Target
4974+
--> stdlib/typing.pyi:351:1
4975+
|
4976+
349 | Final: _SpecialForm
4977+
350 |
4978+
351 | Literal: _SpecialForm
4979+
| ^^^^^^^
4980+
352 | TypedDict: _SpecialForm
4981+
|
4982+
info: Source
4983+
--> main2.py:13:9
4984+
|
4985+
11 | else:
4986+
12 | x = None
4987+
13 | y[: Literal[1, 2, 3, "hello"] | None] = x
4988+
| ^^^^^^^
4989+
|
4990+
4991+
info[inlay-hint-location]: Inlay Hint Target
4992+
--> stdlib/types.pyi:950:11
4993+
|
4994+
948 | if sys.version_info >= (3, 10):
4995+
949 | @final
4996+
950 | class NoneType:
4997+
| ^^^^^^^^
4998+
951 | """The type of the None singleton."""
4999+
|
5000+
info: Source
5001+
--> main2.py:13:37
5002+
|
5003+
11 | else:
5004+
12 | x = None
5005+
13 | y[: Literal[1, 2, 3, "hello"] | None] = x
5006+
| ^^^^
5007+
|
5008+
"#);
5009+
}
5010+
5011+
#[test]
5012+
fn test_generic_alias() {
5013+
let mut test = inlay_hint_test(
5014+
r"
5015+
class Foo[T]: ...
5016+
5017+
a = Foo[int]",
5018+
);
5019+
5020+
assert_snapshot!(test.inlay_hints(), @r#"
5021+
class Foo[T]: ...
5022+
5023+
a[: <class 'Foo[int]'>] = Foo[int]
5024+
---------------------------------------------
5025+
info[inlay-hint-location]: Inlay Hint Target
5026+
--> main.py:2:7
5027+
|
5028+
2 | class Foo[T]: ...
5029+
| ^^^
5030+
3 |
5031+
4 | a = Foo[int]
5032+
|
5033+
info: Source
5034+
--> main2.py:4:13
5035+
|
5036+
2 | class Foo[T]: ...
5037+
3 |
5038+
4 | a[: <class 'Foo[int]'>] = Foo[int]
5039+
| ^^^
5040+
|
5041+
5042+
info[inlay-hint-location]: Inlay Hint Target
5043+
--> stdlib/builtins.pyi:348:7
5044+
|
5045+
347 | @disjoint_base
5046+
348 | class int:
5047+
| ^^^
5048+
349 | """int([x]) -> integer
5049+
350 | int(x, base=10) -> integer
5050+
|
5051+
info: Source
5052+
--> main2.py:4:17
5053+
|
5054+
2 | class Foo[T]: ...
5055+
3 |
5056+
4 | a[: <class 'Foo[int]'>] = Foo[int]
5057+
| ^^^
5058+
|
5059+
"#);
5060+
}
5061+
5062+
#[test]
5063+
fn test_subclass_type() {
5064+
let mut test = inlay_hint_test(
5065+
r"
5066+
def f(x: list[str]):
5067+
y = type(x)",
5068+
);
5069+
5070+
assert_snapshot!(test.inlay_hints(), @r#"
5071+
def f(x: list[str]):
5072+
y[: type[list[str]]] = type(x)
5073+
---------------------------------------------
5074+
info[inlay-hint-location]: Inlay Hint Target
5075+
--> stdlib/builtins.pyi:247:7
5076+
|
5077+
246 | @disjoint_base
5078+
247 | class type:
5079+
| ^^^^
5080+
248 | """type(object) -> the object's type
5081+
249 | type(name, bases, dict, **kwds) -> a new type
5082+
|
5083+
info: Source
5084+
--> main2.py:3:9
5085+
|
5086+
2 | def f(x: list[str]):
5087+
3 | y[: type[list[str]]] = type(x)
5088+
| ^^^^
5089+
|
5090+
5091+
info[inlay-hint-location]: Inlay Hint Target
5092+
--> stdlib/builtins.pyi:2802:7
5093+
|
5094+
2801 | @disjoint_base
5095+
2802 | class list(MutableSequence[_T]):
5096+
| ^^^^
5097+
2803 | """Built-in mutable sequence.
5098+
|
5099+
info: Source
5100+
--> main2.py:3:14
5101+
|
5102+
2 | def f(x: list[str]):
5103+
3 | y[: type[list[str]]] = type(x)
5104+
| ^^^^
5105+
|
5106+
5107+
info[inlay-hint-location]: Inlay Hint Target
5108+
--> stdlib/builtins.pyi:915:7
5109+
|
5110+
914 | @disjoint_base
5111+
915 | class str(Sequence[str]):
5112+
| ^^^
5113+
916 | """str(object='') -> str
5114+
917 | str(bytes_or_buffer[, encoding[, errors]]) -> str
5115+
|
5116+
info: Source
5117+
--> main2.py:3:19
5118+
|
5119+
2 | def f(x: list[str]):
5120+
3 | y[: type[list[str]]] = type(x)
5121+
| ^^^
5122+
|
5123+
"#);
5124+
}
5125+
48335126
#[test]
48345127
fn test_complex_parameter_combinations() {
48355128
let mut test = inlay_hint_test(

0 commit comments

Comments
 (0)