python / python/mypy

Loop Variable over Literal List Not Recognised as Having Literal Type

Open
#18,826 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-context
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

When a for-loop is used to iterate over a list-literal (or other collection-literal) which contains only scalar literals, then MyPy should be smart enough to recognize that the implicit type of the loop variable is not merely the type of the values in the list but the specific values in that list.

To Reproduce

import typing as _tp

MyLiteral: _tp.TypeAlias = _tp.Literal["foo", "bar", "baz"]

def func(a1: MyLiteral):
    ...

def func2() -> None:
    a: MyLiteral
    a = "foo"
    func(a)
    a = "bar"
    func(a)

    b: MyLiteral
    for b in ["foo", "bar"]:
        func(b)

    for c in ["foo", "bar"]:
        func(c)

Expected Behavior

  • At a minimum, there should be no warnings emitted on the first loop: MyPy should be smart enough to identify that all the values the loop is assigning to b are compatible with its explicit type of MyLiteral, the same way it identifies that the values being assigned to a on lines 10 & 12 are legal.
  • Ideally, there should also be no warnings emitted on the second loop: it would be nice if MyPy correctly identified the implicit type of c as Literal["foo", "bar"], which is compatible with the first argument of func()

Actual Behavior

$ mypy [REDACTED]/tmp.py
[REDACTED]\tmp.py:16: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['foo', 'bar', 'baz']")  [assignment]
[REDACTED]\tmp.py:20: error: Argument 1 to "func" has incompatible type "str"; expected "Literal['foo', 'bar', 'baz']"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.5.1 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.11.2

Related Issues

  • #9230: Issue complaining about the same problem, in the specific case where the loop variable is being used to access members of TypedDict. A fix was applied which solved the problem, but only for the special-case of accessing members of TypedDict, not for other, similar cases such as using the loop variable as a function argument as in the above example

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named. Start by running the reported Python reproduction with mypy and inspect existing literal-type and loop-inference tests; done means the explicit and implicit loop-variable cases produce no errors while preserving the expected Literal behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.