about Iterable[T] and str
Open
Nobody has claimed this yet.
bug
topic-type-narrowing
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
from typing import *
T = TypeVar("T")
def f(it: Iterable[T]) -> list[T]:
if isinstance(it, str):
ret = it.split(",") # <nothing> has no attribute "split"
else:
ret = list(it)
return ret
It looks ok, but I get a problem in mypy: <nothing> has no attribute "split".
Is it the only way to solve this problem that I use # type ignore?
Your Environment
- Mypy version used: 0.991
- Python version used: 3.9
- os: win11
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
No source file or test is named. Start by reproducing the snippet with mypy 0.991 on Python 3.9, then inspect how type narrowing handles isinstance(it, str) for Iterable[T]. Done means establishing the supported behavior or workaround and, if this is confirmed as a bug, adding a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100