[ty] Disambiguate duplicate-looking overloaded callables in union display#23907
Merged
charliermarsh merged 2 commits intomainfrom Mar 12, 2026
Merged
[ty] Disambiguate duplicate-looking overloaded callables in union display#23907charliermarsh merged 2 commits intomainfrom
charliermarsh merged 2 commits intomainfrom
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 85.29%. The percentage of expected errors that received a diagnostic held steady at 78.13%. The number of fully passing files held steady at 64/132. |
|
Memory usage reportMemory usage unchanged ✅ |
92a1dc2 to
0335063
Compare
AlexWaygood
approved these changes
Mar 12, 2026
crates/ty_python_semantic/resources/mdtest/type_display/callable.md
Outdated
Show resolved
Hide resolved
Comment on lines
+2418
to
+2429
| fn duplicate_ambiguous_labels(element_labels: &[Option<String>]) -> FxHashSet<String> { | ||
| let mut counts = FxHashMap::default(); | ||
|
|
||
| for label in element_labels.iter().flatten() { | ||
| *counts.entry(label.clone()).or_insert(0usize) += 1; | ||
| } | ||
|
|
||
| counts | ||
| .into_iter() | ||
| .filter_map(|(label, count)| (count > 1).then_some(label)) | ||
| .collect() | ||
| } |
Member
There was a problem hiding this comment.
I think you can return an FxHashSet<&str> here and reduce the number of necessary clones:
diff --git a/crates/ty_python_semantic/src/types/display.rs b/crates/ty_python_semantic/src/types/display.rs
index 2ed4ea0cad..1f5ac7b757 100644
--- a/crates/ty_python_semantic/src/types/display.rs
+++ b/crates/ty_python_semantic/src/types/display.rs
@@ -2415,11 +2415,11 @@ impl<'db> FmtDetailed<'db> for DisplayUnionType<'_, 'db> {
element.display_with(db, settings.singleline()).to_string()
}
- fn duplicate_ambiguous_labels(element_labels: &[Option<String>]) -> FxHashSet<String> {
- let mut counts = FxHashMap::default();
+ fn duplicate_ambiguous_labels(element_labels: &[Option<String>]) -> FxHashSet<&str> {
+ let mut counts: FxHashMap<&str, usize> = FxHashMap::default();
for label in element_labels.iter().flatten() {
- *counts.entry(label.clone()).or_insert(0usize) += 1;
+ *counts.entry(&**label).or_default() += 1;
}
counts
@@ -2491,7 +2491,7 @@ impl<'db> FmtDetailed<'db> for DisplayUnionType<'_, 'db> {
} else {
displayed_entries += 1;
let settings = if label
- .as_ref()
+ .as_deref()
.is_some_and(|label| duplicate_ambiguous_labels.contains(label))
{
self.settings.singleline().force_signature_name()
Member
|
Thanks! |
0335063 to
7877cf5
Compare
AlexWaygood
reviewed
Mar 12, 2026
crates/ty_python_semantic/resources/mdtest/type_display/callable.md
Outdated
Show resolved
Hide resolved
…le.md Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adjust type display so unions that contain distinct overloaded callables with identical single-line renderings include callable names for any ambiguous elements... This avoids outputs like
A | Awhen the union actually contains two different function values (likestr.upper | str.lower).Closes astral-sh/ty#2919.