Skip to content

Reject type promotions into bytes Literal destinations - #12059

Draft
Neha Chinnasani (nehachinnasani23) wants to merge 1 commit into
microsoft:mainfrom
nehachinnasani23:fix/bytes-literal-assignability
Draft

Neha Chinnasani (nehachinnasani23) wants to merge 1 commit into
microsoft:mainfrom
nehachinnasani23:fix/bytes-literal-assignability

Conversation

@nehachinnasani23

@nehachinnasani23 Neha Chinnasani (nehachinnasani23) commented Nov 22, 2026 •

Copy link
Copy Markdown

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, bytearray and memoryview could be returned as Literal[b"a", b"b"] even with bytes promotions disabled. Promotions now apply only when the destination has no literal value; ordinary bytes promotions retain their configured behavior.

Fixes #12056.

Evidence and validation

  • Added focused regression coverage for the motivating behavior.
  • Checked nearby invalid cases and unaffected valid controls.
  • Compared the regression against the original production code.
  • Codex reviewed the patch for architecture, type precision, and bounded performance impact.
  • Codex ran the relevant tests and checks below.

Baseline: 2981f9f1ce41be5c15667da1f7a3cfda4f245089.
Fix: dda0b77.

Node 24.22.3, pnpm 11.21.0:

pnpm --dir packages/pyright-internal exec jest typeEvaluator1.test typeEvaluator2.test typeEvaluator3.test typeEvaluator4.test typeEvaluator6.test --runInBand --forceExit
pnpm --dir packages/pyright-internal run build
pnpm run check

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 reportReturnType diagnostic 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 and Literal[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

  • I personally reviewed and understand this entire pull request, including any AI-assisted work, ran the checks appropriate to the change, and take responsibility for the contribution and its follow-up.

Classification B: Pyright compatibility limitation; no typeshed change. Preserve literal constraints while retaining ordinary non-literal promotions.

This branch has not been deployed

No deployments
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.

bytearray/memoryview assignable to a multi-value bytes Literal

1 participant