Skip to content

Conversation

@randolf-scholz
Copy link
Contributor

@randolf-scholz randolf-scholz commented Dec 20, 2025

Fixes #20445

Fixes a regression in conditional_type when given a union of Any | Protocol

proposed_type = make_simplified_union(proposed_items)

if isinstance(proper_type, AnyType):
return proposed_type, current_type
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, would it be technically more correct to return AnyType(TypeOfAny.from_another_any, source_any=proper_type) for the else branch, rather than proper_type itself?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, I've lost track of what all TypeOfAny is used for. My instinct is that it is correct as is, e.g. if you had a ternary expression that you're narrowing, feels like you might want to preserve the original Any

return default, UninhabitedType()
elif not is_overlapping_types(current_type, proposed_type, ignore_promotions=True):
return default, remainder
if not is_overlapping_types(current_type, proposed_type, ignore_promotions=True):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted the elif to if because the if not any(type_range.is_upper_bound for type_range in proposed_type_ranges): is not guaranteed to return anymore.

)
):
# Expression is always of one of the types in proposed_type_ranges
return default, UninhabitedType()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return was problematic in the structural case when current_type=Any | Proto, restricting Proto away should leave us with Any and not Never.

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you for the quick fix and for the original PR :-)

@hauntsaninja hauntsaninja changed the title hotfix for 20445 Fix conditional_types remainder for structural types Jan 5, 2026
@hauntsaninja hauntsaninja merged commit 56c33f2 into python:master Jan 5, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[1.19 regression] Ternary expression involving a union with Any evaluated as redundant

2 participants