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 — Narrowing types to `Literal` using `in` syntax · Issue #12535 · python/mypy

Narrowing types to Literal using in syntax #12535

New issue
Copy link
New issue
Copy link
Open
#20444
Open
Narrowing types to Literal using in syntax#12535
#20444
Copy link
Labels
featuretopic-literal-typestopic-type-narrowingConditional type narrowing / binder

Description

@tusharsadhwani
tusharsadhwani
opened on Apr 6, 2022
Issue body actions

Take this example code:

x = "a"
reveal_type(x)
assert x in ("a", "b", "c")
reveal_type(x)

y = 20
reveal_type(y)
assert y == 10 or y == 20 or y == 50
reveal_type(y)

The current output of this is:

$ mypy x.py 
x.py:2: note: Revealed type is "builtins.str"
x.py:4: note: Revealed type is "builtins.str"
x.py:7: note: Revealed type is "builtins.int"
x.py:9: note: Revealed type is "builtins.int"

If mypy were to support type narrowing using chained or statements and in statements, the output would look like:

$ mypy x.py 
x.py:4: note: Revealed type is "builtins.str"
x.py:6: note: Revealed type is "Union[Literal['a'], Literal['b'], Literal['c']]"
x.py:9: note: Revealed type is "builtins.int"
x.py:11: note: Revealed type is "Union[Literal[10], Literal[20], Literal[50]]"

I think doing simple narrowing here would be relatively easy and very useful.

Reacted by Michael Joseph Rosenthal, Marc Mueller, Brenton Partridge, Huon Wilson, Alex Waygood, Matan Gover, Gideon, Marti Raudsepp, Stanislav Terliakov, Justin Black and 23 more

Activity

  1. tusharsadhwani
    added
    feature
    on Apr 6, 2022
  2. JelleZijlstra
    added
    topic-type-narrowingConditional type narrowing / binder
    on Apr 6, 2022
  3. AlexWaygood
    added
    topic-literal-types
    on Apr 6, 2022
  4. tmke8 commented on Apr 7, 2022

    @tmke8
    tmke8
    on Apr 7, 2022
    Contributor
    More actions

    Related to (but maybe not quite a duplicate of) #9718.

  5. tusharsadhwani commented on Apr 7, 2022

    @tusharsadhwani
    tusharsadhwani
    on Apr 7, 2022
    ContributorAuthor
    More actions

    It's a superset of #9718. I'll be looking into implementing this then, starting with the literal narrowing case.

  6. AlexWaygood commented on Apr 7, 2022

    @AlexWaygood
    AlexWaygood
    on Apr 7, 2022
    Member
    More actions

    A superset of #9718, and a subset of #10977 :)

    Reacted by Tushar Sadhwani
  7. bluenote10 commented on Aug 14, 2023

    @bluenote10
    bluenote10
    on Aug 14, 2023
    Contributor
    More actions

    This issue seems to address narrowing both for in but also for ==. Should these two features be tracked independently (if their implementations would be rather decoupled) or is tracking them together preferred (if implementing one would likely solve the other)?

    I couldn't find an issue specifically for narrowing literals in == equality comparisons, and I would have expected this to be implemented first, because it even feels more basic than the in operator:

    from typing import Literal
    
    def takes_foo(s: Literal["foo"]) -> None:
        ...
    
    def f(s: str) -> None:
        if s == "foo":
            takes_foo(s)
    error: Argument 1 to "takes_foo" has incompatible type "str"; expected "Literal['foo']"  [arg-type]
    

    Pyright seems to understand this kind of narrowing well.

    Reacted by siddhantwahal and Pieter Gijsbers
  8. asottile-sentry commented on Jun 6, 2024

    @asottile-sentry
    asottile-sentry
    on Jun 6, 2024
    More actions

    I opted for a clunky workaround using TypeIs though it would be nice if this were automatic

    MyLiteral = Literal["a", "b"]
    
    def is_my_literal(s: str) -> TypeIs[MyLiteral]:
        return s in ("a", "b")
    Reacted by rduo1009, Christian Bourjau and nakoo
  9. JelleZijlstra commented on Jun 7, 2024

    @JelleZijlstra
    JelleZijlstra
    on Jun 7, 2024
    Member
    More actions

    This is similar to #3229 though that one doesn't ask for narrowing to Literal, possibly because the issue predates literal types.

  10. The-Compiler
    added a commit that references this issue on Jan 6, 2025

    Fix mypy issues after 1.14 upgrade

    7d1179e
  11. chadkouse
    added a commit that references this issue on Jul 31, 2025

    Fix mypy issues after 1.14 upgrade

    3fb9022
  12. brianschubert
    marked mypy gives diagnostic on code Literal typing code accepted by pyright, pyrefly #19747 as a duplicate of this issue on Aug 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featuretopic-literal-typestopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402775 — 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.