literals_interactions.py: do not mandate optional narrowing behaviour from type checkers#2209
Open
AlexWaygood wants to merge 1 commit intomainfrom
Open
literals_interactions.py: do not mandate optional narrowing behaviour from type checkers#2209AlexWaygood wants to merge 1 commit intomainfrom
literals_interactions.py: do not mandate optional narrowing behaviour from type checkers#2209AlexWaygood wants to merge 1 commit intomainfrom
Conversation
…ur from type checkers
carljm
approved these changes
Mar 7, 2026
JelleZijlstra
approved these changes
Mar 7, 2026
Member
JelleZijlstra
left a comment
There was a problem hiding this comment.
For usability reasons I think it is more practical to narrow in these cases, but you are technically correct it is unsound.
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.
X-ref #2208 (comment).
An example in this file mandates that type checkers should narrow an object
xof typestrtoLiteral["foo"]after anif x == "foo"check. This example appears to be testing adherence to the passage in the spec at https://typing.python.org/en/latest/spec/literal.html#interactions-with-narrowing. But the passage in the spec here clearly indicates that narrowingstrorLiteralStringtypes toLiteralstrings is optional:The narrowing examples given by the spec are also unsound; it's easy to create a
strsubclass (andstrsubclasses are common, due toStrEnumin the standard library) that has custom equality semantics and breaks the assumptions of narrowing like this:This PR keeps a version of the existing test (since it comes directly from the spec), but allows type checkers to optionally emit errors on certain lines if they do not implement the optional narrowing behaviour described by the spec. It also adds additional tests using
LiteralString, for which narrowing to aLiteralstring type via equality or containment is unambiguously sound.The effect of this PR is that mypy and ty now fully pass
literals_interactions.py. All other type checkers have their conformance score unchanged.