Repository navigation
Reject type promotions into bytes Literal destinations - #12059
Draft
Neha Chinnasani (nehachinnasani23) wants to merge 1 commit into
Draft
Neha Chinnasani (nehachinnasani23) wants to merge 1 commit into
Neha Chinnasani (nehachinnasani23) wants to merge 1 commit into
Conversation
Classification B: Pyright compatibility limitation; no typeshed change. Preserve literal constraints while retaining ordinary non-literal promotions.
This branch has not been deployed
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
AI-assisted draft: Codex prepared the implementation, this description, and the validation below. This draft is for human review; the author-responsibility declaration remains unchecked. The human author should add their own explanation and complete the required personal checks before requesting review.
Prevent the type-promotion shortcut from accepting non-literal values for a literal destination. Previously,
bytearrayandmemoryviewcould be returned asLiteral[b"a", b"b"]even with bytes promotions disabled. Promotions now apply only when the destination has no literal value; ordinarybytespromotions retain their configured behavior.Fixes #12056.
Evidence and validation
Baseline:
2981f9f1ce41be5c15667da1f7a3cfda4f245089.Fix:
dda0b77.Node 24.22.3, pnpm 11.21.0:
All 871 selected tests, the TypeScript build, ESLint, formatting, and dependency checks passed.
The regression covers bytearray, memoryview, and plain bytes against single, multiple, explicit-union, and mixed Literal destinations. It checks exact
reportReturnTypediagnostic lines under both bytes-promotion settings. On the baseline, the disabled/enabled configurations produce only 10/4 errors instead of the expected 14/12; the fix produces all expected errors. Valid literal returns andLiteral[b"a", b"b"]revealed types remain unchanged. Ordinary bytes assignments are still accepted when promotion is enabled.Mypy 2.4.0 independently rejects all 14 invalid returns under its default strict-bytes behavior.
Limits: The full repository suite and a performance benchmark were not run.
Type precision and performance
This is a compatibility bug (classification B), not a typeshed change. The guard prevents an unsound acceptance without widening inferred types. It adds one short-circuited field comparison to the existing promotion shortcut, with no new traversal or cache state.
The typing specification makes multi-value Literal forms equivalent to unions of individual literals, and general non-literal types cannot satisfy Literal destinations.
Author responsibility