Skip to content

Fix native parser for quoted None types - #22091

Closed
daleselaji-dev wants to merge 1 commit into
python:masterfrom
daleselaji-dev:daleselaji-dev/fix-native-parser-none-cast
Closed

daleselaji-dev wants to merge 1 commit into
python:masterfrom
daleselaji-dev:daleselaji-dev/fix-native-parser-none-cast

Conversation

@daleselaji-dev

Copy link
Copy Markdown

Fixes #22090

Problem

With the native parser enabled, cast("None", value) is rejected with Invalid type comment or annotation, although the same quoted forward reference is accepted by the legacy parser and was accepted before the ast-serialize 0.12.0 upgrade.

Root Cause

The native type-string parser represents the literal None as a RawExpressionType instead of the UnboundType("None") produced by the legacy parser. Semantic analysis therefore treats the cast target as an invalid string expression.

Solution

Normalize the native parser's exact None result to UnboundType("None"), preserving the existing forward-reference behavior without changing other raw expression handling.

Changes

  • Normalize the native parser result for the exact None type string.
  • Add a regression case covering cast("None", value).

Testing

  • Baseline: check-newsemanal.test — 226 passed.
  • Focused regression: check-newsemanal.test — 227 passed.
  • Native parser suite: mypy/test/test_nativeparse.py — 257 passed.
  • Native and legacy CLI repros: both report success for cast("None", value).
  • pre-commit run --files mypy/nativeparse.py test-data/unit/check-newsemanal.test — passed (Black, Ruff, codespell and repository hooks).
  • python -m mypy --config-file mypy_self_check.ini -p mypy — 196 source files passed.
  • git diff --check — passed.
  • Full test suite and multi-platform CI matrix — not run locally.

Compatibility/Risk

The change only affects the native parser's handling of the exact quoted None type expression. Other raw expression types and the legacy parser are unchanged.

Notes for Reviewer

The regression is exposed by the ast-serialize 0.12.0 parser output. The fix keeps the compatibility behavior already provided by the legacy parser and is limited to the parser boundary.

AI assistance was used during investigation and implementation; the final diff and test results are documented above.

Linked Issue

Fixes #22090

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

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

@ilevkivskyi ilevkivskyi closed this Oct 3, 2026
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.

[2.4 regression] valid-type error for cast("None", _) -- "Invalid type comment or annotation"

2 participants