str not compatible with StrOrLiteralStr
Nobody has claimed this yet.
- 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"
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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