Skip to content

Commit 7c62516

Browse files
yangdanny97meta-codesync[bot]
authored andcommitted
display overloads & bound method overloads on multiple lines if it's a toplevel type
Summary: for #1219 Reviewed By: rchen152 Differential Revision: D90149768 fbshipit-source-id: 7b5bca99c449a997b7723d2b75936cf23d296433
1 parent ab7ce84 commit 7c62516

File tree

4 files changed

+50
-16
lines changed

4 files changed

+50
-16
lines changed

conformance/third_party/conformance.exp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,7 +2563,7 @@
25632563
"code": -2,
25642564
"column": 21,
25652565
"concise_description": "`Overloaded9` is not assignable to `FloatArg9`",
2566-
"description": "`Overloaded9` is not assignable to `FloatArg9`\n `Overloaded9.__call__` has type `BoundMethod[Overloaded9, Overload[(self: Overloaded9, x: int) -> int, (self: Overloaded9, x: str) -> str]]`, which is not assignable to `BoundMethod[Overloaded9, (self: Overloaded9, x: float) -> float]`, the type of `FloatArg9.__call__`",
2566+
"description": "`Overloaded9` is not assignable to `FloatArg9`\n `Overloaded9.__call__` has type `BoundMethod[Overloaded9, Overload[\n (self: Overloaded9, x: int) -> int\n (self: Overloaded9, x: str) -> str\n]]`, which is not assignable to `BoundMethod[Overloaded9, (self: Overloaded9, x: float) -> float]`, the type of `FloatArg9.__call__`",
25672567
"line": 273,
25682568
"name": "bad-assignment",
25692569
"severity": "error",
@@ -2574,7 +2574,7 @@
25742574
"code": -2,
25752575
"column": 24,
25762576
"concise_description": "`StrArg10` is not assignable to `Overloaded10`",
2577-
"description": "`StrArg10` is not assignable to `Overloaded10`\n `StrArg10.__call__` has type `BoundMethod[StrArg10, (self: StrArg10, x: str) -> complex]`, which is not assignable to `BoundMethod[StrArg10, Overload[(self: StrArg10, x: int, y: str) -> float, (self: StrArg10, x: str) -> complex]]`, the type of `Overloaded10.__call__`",
2577+
"description": "`StrArg10` is not assignable to `Overloaded10`\n `StrArg10.__call__` has type `BoundMethod[StrArg10, (self: StrArg10, x: str) -> complex]`, which is not assignable to `BoundMethod[StrArg10, Overload[\n (self: StrArg10, x: int, y: str) -> float\n (self: StrArg10, x: str) -> complex\n]]`, the type of `Overloaded10.__call__`",
25782578
"line": 297,
25792579
"name": "bad-assignment",
25802580
"severity": "error",

crates/pyrefly_types/src/display.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -435,13 +435,25 @@ impl<'a> TypeDisplayContext<'a> {
435435
}
436436
Ok(())
437437
} else {
438-
output.write_str("Overload[")?;
438+
if is_toplevel {
439+
output.write_str("Overload[\n ")?;
440+
} else {
441+
output.write_str("Overload[")?;
442+
}
439443
self.fmt_helper_generic(&overload.signatures.first().as_type(), false, output)?;
440444
for sig in overload.signatures.iter().skip(1) {
441-
output.write_str(", ")?;
445+
if is_toplevel {
446+
output.write_str("\n ")?;
447+
} else {
448+
output.write_str(", ")?;
449+
}
442450
self.fmt_helper_generic(&sig.as_type(), false, output)?;
443451
}
444-
output.write_str("]")
452+
if is_toplevel {
453+
output.write_str("\n]")
454+
} else {
455+
output.write_str("]")
456+
}
445457
}
446458
}
447459
Type::ParamSpecValue(x) => {
@@ -519,7 +531,7 @@ impl<'a> TypeDisplayContext<'a> {
519531
output.write_str("BoundMethod[")?;
520532
self.fmt_helper_generic(obj, false, output)?;
521533
output.write_str(", ")?;
522-
self.fmt_helper_generic(&func.clone().as_type(), false, output)?;
534+
self.fmt_helper_generic(&func.clone().as_type(), is_toplevel, output)?;
523535
output.write_str("]")
524536
}
525537
}
@@ -1652,11 +1664,19 @@ pub mod tests {
16521664
metadata: Box::new(sig1.metadata.clone()),
16531665
});
16541666

1655-
// Test compact display mode (non-hover)
1667+
// Test compact display mode as toplevel type (non-hover)
16561668
let ctx = TypeDisplayContext::new(&[&overload]);
16571669
assert_eq!(
16581670
ctx.display(&overload).to_string(),
1659-
"Overload[(x: Any) -> None, [T](x: Any, y: Any) -> None]"
1671+
"Overload[\n (x: Any) -> None\n [T](x: Any, y: Any) -> None\n]"
1672+
);
1673+
1674+
// Test compact display mode as non-toplevel type (non-hover)
1675+
let type_form_of_overload = Type::type_form(overload.clone());
1676+
let ctx = TypeDisplayContext::new(&[&type_form_of_overload]);
1677+
assert_eq!(
1678+
ctx.display(&type_form_of_overload).to_string(),
1679+
"type[Overload[(x: Any) -> None, [T](x: Any, y: Any) -> None]]"
16601680
);
16611681

16621682
// Test hover display mode (with @overload decorators)
@@ -1691,11 +1711,19 @@ def overloaded_func[T](
16911711
}),
16921712
}));
16931713

1694-
// Test compact display mode (non-hover)
1714+
// Test compact display mode as toplevel type (non-hover)
16951715
let ctx = TypeDisplayContext::new(&[&bound_method_overload]);
16961716
assert_eq!(
16971717
ctx.display(&bound_method_overload).to_string(),
1698-
"BoundMethod[Any, Overload[(x: Any) -> None, [T](x: Any, y: Any) -> None]]"
1718+
"BoundMethod[Any, Overload[\n (x: Any) -> None\n [T](x: Any, y: Any) -> None\n]]"
1719+
);
1720+
1721+
// Test compact display mode as non-toplevel type (non-hover)
1722+
let type_form_of_bound_method_overload = Type::type_form(bound_method_overload.clone());
1723+
let ctx = TypeDisplayContext::new(&[&type_form_of_bound_method_overload]);
1724+
assert_eq!(
1725+
ctx.display(&type_form_of_bound_method_overload).to_string(),
1726+
"type[BoundMethod[Any, Overload[(x: Any) -> None, [T](x: Any, y: Any) -> None]]]"
16991727
);
17001728

17011729
// Test hover display mode (with @overload decorators)

pyrefly/lib/test/attributes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ class A[T]:
14111411
@overload
14121412
def f(self, x: T | None) -> T: ...
14131413
def f(self, x=None) -> Any: ...
1414-
reveal_type(A.f) # E: revealed type: Overload[[T](self: A[T]) -> T, [T](self: A[T], x: T | None) -> T]
1414+
reveal_type(A.f) # E: revealed type: Overload[\n [T](self: A[T]) -> T\n [T](self: A[T], x: T | None) -> T\n]
14151415
assert_type(A.f(A[int]()), int)
14161416
"#,
14171417
);
@@ -1429,7 +1429,7 @@ class A[T]:
14291429
def f(x: T) -> T: ...
14301430
@staticmethod
14311431
def f(x = None) -> Any: ...
1432-
reveal_type(A.f) # E: revealed type: Overload[(x: None = ...) -> None, [T](x: T) -> T]
1432+
reveal_type(A.f) # E: revealed type: Overload[\n (x: None = ...) -> None\n [T](x: T) -> T\n]
14331433
assert_type(A.f(), None)
14341434
assert_type(A.f(0), int)
14351435
"#,
@@ -1738,7 +1738,7 @@ def get_type_t[T]() -> type[T]:
17381738
return cast(type[T], 0)
17391739
def foo[T](x: type[T]):
17401740
# mypy reveals the same thing we do (the type of `type.__new__`), while pyright reveals `Unknown`.
1741-
reveal_type(get_type_t().__new__) # E: Overload[[Self@type](cls: type[Self@type], o: object, /) -> type, [Self](cls: type[Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], /, **kwds: Any) -> Self]
1741+
reveal_type(get_type_t().__new__) # E: Overload[\n [Self@type](cls: type[Self@type], o: object, /) -> type\n [Self](cls: type[Self], name: str, bases: tuple[type, ...], namespace: dict[str, Any], /, **kwds: Any) -> Self\n]
17421742
"#,
17431743
);
17441744

pyrefly/lib/test/lsp/hover_type.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ async def test(vals: dict[int, str]) -> None:
130130
131131
for k in vals.keys(): # 2
132132
k
133-
133+
134134
for v in vals.values(): # 3
135135
v
136136
"#;
@@ -444,7 +444,10 @@ Hover Result: `(a: int, b: bool) -> str`
444444
445445
15 | overloaded_func(False)
446446
^
447-
Hover Result: `Overload[(a: str) -> bool, (a: int, b: bool) -> str]`
447+
Hover Result: `Overload[
448+
(a: str) -> bool
449+
(a: int, b: bool) -> str
450+
]`
448451
"#
449452
.trim(),
450453
report.trim(),
@@ -486,7 +489,10 @@ Hover Result: `(self: Foo, a: int, b: bool) -> str`
486489
487490
17 | foo.overloaded_meth(False)
488491
^
489-
Hover Result: `BoundMethod[Foo, Overload[(self: Foo, a: str) -> bool, (self: Foo, a: int, b: bool) -> str]]`
492+
Hover Result: `BoundMethod[Foo, Overload[
493+
(self: Foo, a: str) -> bool
494+
(self: Foo, a: int, b: bool) -> str
495+
]]`
490496
"#
491497
.trim(),
492498
report.trim(),

0 commit comments

Comments
 (0)