[ty] Support literal types in comparison methods#23100
Merged
Conversation
Typing conformance resultsNo changes detected ✅ |
|
… methods When a class defines a comparison method like `__gt__(self, other: Literal[0])`, using the comparison operator `m > 0` would fail with "Unsupported `>` operation" even though the direct method call `m.__gt__(0)` worked correctly. The issue was in `infer_binary_type_comparison`: when comparing a `NominalInstance` with a literal type (`IntLiteral`, `StringLiteral`, or `BytesLiteral`), the code unconditionally widened the literal to its base type (`int`, `str`, `bytes`) before attempting dunder method lookup. This meant that a method expecting `Literal[0]` would receive `int` instead, causing the type check to fail. The fix modifies the literal + NominalInstance cases to first try the dunder call with the original literal type, then fall back to widening only if that fails. This allows methods annotated with literal types to work correctly while maintaining backward compatibility with methods expecting the base types. Also removes unnecessary catch-all patterns for StringLiteral, BytesLiteral, and LiteralString that were not exercised by any tests. https://claude.ai/code/session_01YMYPi46ZrejvcYnNoqB759
a22cf27 to
02bb5f3
Compare
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
unsupported-operator |
0 | 1 | 40 |
possibly-missing-attribute |
21 | 0 | 0 |
invalid-argument-type |
5 | 1 | 1 |
invalid-assignment |
0 | 0 | 1 |
no-matching-overload |
1 | 0 | 0 |
| Total | 27 | 2 | 42 |
carljm
commented
Feb 5, 2026
AlexWaygood
reviewed
Feb 5, 2026
AlexWaygood
reviewed
Feb 5, 2026
crates/ty_python_semantic/resources/mdtest/comparison/instances/rich_comparison.md
Outdated
Show resolved
Hide resolved
AlexWaygood
approved these changes
Feb 6, 2026
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.
Fixes astral-sh/ty#2727
Summary
This change improves type inference for comparison operations involving literal types (int, str, bytes) with nominal instances. Previously, when comparing a literal type with a class instance, we would immediately widen the literal to its base type (e.g.,
Literal[0]→int). This prevented proper type checking of comparison methods that explicitly accept specific literal types.Test Plan
Added mdtests.