python / python/mypy

str not compatible with StrOrLiteralStr

Open
#15,848 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

mypy doesn't accept str for StrOrLiteralStr

To Reproduce

from string import Formatter
from typing import Iterable, Tuple

class MyFormatter(Formatter):
    def parse(self, s: str) -> Iterable[Tuple[str, str, str, str]]:
        for text, field, spec, conversion in super().parse(s):
            yield "a", "b", "c", "d"

(playground)

Actual Behavior

error: Return type "Iterable[tuple[str, str, str, str]]" of "parse" incompatible with return type "Iterable[tuple[StrOrLiteralStr, StrOrLiteralStr | None, StrOrLiteralStr | None, StrOrLiteralStr | None]]" in supertype "Formatter" [override]

Details

I've asked a question on SO about this and didn't get an answer (yet). As indicated in the comments:

this code passes type checking in mypy 0.971, and fails in mypy 0.981 through 1.5.0

Perhaps it's related to mypy lack of support for LiteralStr #12554

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

Start with the Formatter.parse override in the Python reproducer and run it in the linked mypy playground, comparing the behavior across the versions mentioned. Trace how mypy handles StrOrLiteralStr and the override return type; done means the reproducer is accepted without incorrectly rejecting str.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.