GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improved `Literal?` types by randolf-scholz · Pull Request #20444 · python/mypy

Improved Literal? types #20444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
randolf-scholz wants to merge 17 commits into python:master
base: master
Loading
Loading
from randolf-scholz:literals_as_anyof_types

Uh oh!

There was an error while loading. Please reload this page.

Open

Improved Literal? types #20444

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
0e09a61
better support for literal? types
randolf-scholz Aug 7, 2025
6101a57
Merge branch 'master' into literals_as_anyof_types
randolf-scholz Jan 16, 2026
76eb7ce
Merge branch 'master' into literals_as_anyof_types
randolf-scholz Feb 2, 2026
0260762
Merge branch 'master' into literals_as_anyof_types
randolf-scholz Feb 3, 2026
d7fe600
updated test-data
randolf-scholz Feb 3, 2026
80d2bed
fixed incompatibility with FlipFlop test-case
randolf-scholz Feb 3, 2026
96da68d
added type hint for mypyc
randolf-scholz Feb 3, 2026
377a0af
cleanup + fix meet with NamedTuple
randolf-scholz Feb 3, 2026
cea2af9
rewrote ternary statement as separate returns for mypyc
randolf-scholz Feb 3, 2026
ef22b69
Merge branch 'master' into literals_as_anyof_types
randolf-scholz Feb 3, 2026
f2b855c
explicit is None check for mypyc
randolf-scholz Feb 3, 2026
8d53181
Merge branch 'master' into literals_as_anyof_types
randolf-scholz Feb 26, 2026
8dfa9d6
Merge branch 'master' into literals_as_anyof_types
randolf-scholz Apr 7, 2026
da5727b
Merge branch 'master' into literals_as_anyof_types
hauntsaninja Apr 15, 2026
45ccb3a
Merge branch 'master' into literals_as_anyof_types
randolf-scholz Aug 25, 2026
3497ec1
Update type reveal comments in tests for Literal types
randolf-scholz Aug 26, 2026
748c834
Merge branch 'master' into literals_as_anyof_types
randolf-scholz Nov 4, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
updated test-data
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@randolf-scholz
randolf-scholz committed Feb 3, 2026
commit d7fe6001e6d0e5e26de5d34ceff3d1857522c22c

There are no files selected for viewing

23 changes: 12 additions & 11 deletions 23 test-data/unit/check-narrowing.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -708,20 +708,21 @@ class FlipFlopStr:

def test1(switch: FlipFlopStr) -> None:
# Naively, we might assume the 'assert' here would narrow the type to
# Literal["state-1"]. However, doing this ends up breaking a fair number of real-world
# a strong literal 'Literal["state-1"]'.
# However, doing this ends up breaking a fair number of real-world
# code (usually test cases) that looks similar to this function: e.g. checks
# to make sure a field was mutated to some particular value.
#
# And since mypy can't really reason about state mutation, we take a conservative
# approach and avoid narrowing anything here.
# approach and only narrow to a weak literal 'Literal["state-1"]?'

assert switch.state == "state-1"
reveal_type(switch.state) # N: Revealed type is "builtins.str"
reveal_type(switch.state) # N: Revealed type is "Literal['state-1']?"

switch.mutate()

assert switch.state == "state-2"
reveal_type(switch.state) # N: Revealed type is "builtins.str"
reveal_type(switch.state) # N: Revealed type is "Literal['state-2']?"

def test2(switch: FlipFlopEnum) -> None:
# This is the same thing as 'test1', except we use enums, which we allow to be narrowed
Expand Down Expand Up @@ -755,18 +756,18 @@ from typing import Final, Literal
A_final: Final = "A"
A_literal: Literal["A"]

# Neither the LHS nor the RHS are explicit literals, so regrettably nothing
# is narrowed here -- see 'testNarrowingEqualityFlipFlop' for an example of
# why more precise inference here is problematic.
# narrow only to a weak literal 'Literal["A"]?', as the value may mutate to a different
# string later - see 'testNarrowingEqualityFlipFlop' for an example of
# why we cannot narrow to a strong literal 'Literal["A"]'
x_str: str
if x_str == "A":
reveal_type(x_str) # N: Revealed type is "builtins.str"
reveal_type(x_str) # N: Revealed type is "Literal['A']?"
else:
reveal_type(x_str) # N: Revealed type is "builtins.str"
reveal_type(x_str) # N: Revealed type is "builtins.str"

if x_str == A_final:
reveal_type(x_str) # N: Revealed type is "builtins.str"
reveal_type(x_str) # N: Revealed type is "Literal['A']?"
else:
reveal_type(x_str) # N: Revealed type is "builtins.str"
reveal_type(x_str) # N: Revealed type is "builtins.str"
Expand Down Expand Up @@ -2464,7 +2465,7 @@ def foo(location: ParameterLocation):
# https://github.com/python/mypy/issues/17864
def f(x: str | int) -> None:
if x == "x":
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(x) # N: Revealed type is "Literal['x']?"
y = x

if x in ["x"]:
Expand Down Expand Up @@ -3572,7 +3573,7 @@ def foo(x: str | array) -> str:
from __future__ import annotations
def f(x: bytes | None):
if x == b"asdf":
reveal_type(x) # N: Revealed type is "builtins.bytes"
reveal_type(x) # N: Revealed type is "Literal[b'asdf']?"
else:
reveal_type(x) # N: Revealed type is "builtins.bytes | None"
[builtins fixtures/primitives.pyi]
Expand Down
8 changes: 4 additions & 4 deletions 8 test-data/unit/check-optional.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ from typing import Optional

def main(x: Optional[str]):
if x == '<string>':
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(x) # N: Revealed type is "Literal['<string>']?"
else:
reveal_type(x) # N: Revealed type is "builtins.str | None"
if x is '<string>':
Expand All @@ -461,7 +461,7 @@ def main(x: Optional[str]):
from typing import Union
def main(x: Union[str, int, None]):
if x == '<string>':
reveal_type(x) # N: Revealed type is "builtins.str"
reveal_type(x) # N: Revealed type is "Literal['<string>']?"
else:
reveal_type(x) # N: Revealed type is "builtins.str | builtins.int | None"
if x is '<string>':
Expand Down Expand Up @@ -521,8 +521,8 @@ from typing import Optional

def main(x: Optional[int], y: Optional[int]):
if x == y == 1:
reveal_type(x) # N: Revealed type is "builtins.int"
reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(x) # N: Revealed type is "Literal[1]?"
reveal_type(y) # N: Revealed type is "Literal[1]?"
else:
reveal_type(x) # N: Revealed type is "builtins.int | None"
reveal_type(y) # N: Revealed type is "builtins.int | None"
Expand Down
2 changes: 1 addition & 1 deletion 2 test-data/unit/check-python38.test
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def check_narrow(x: Optional[int], s: List[int]) -> None:
reveal_type(y) # N: Revealed type is "builtins.int"

if (y := x) == 10:
reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(y) # N: Revealed type is "Literal[10]?"

if (y := x) in s:
reveal_type(y) # N: Revealed type is "builtins.int"
Expand Down
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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.