false positive on map with a conditional expression
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 15.6k
- Forks
- 1.8k
- Avg merge
- 12h 13m
- Merged PRs (30d)
- 52
Description
**Describe the bug**
a map with a conditional expression where both branches result in `str` is deduced to be of type `map[str | _T1@__new__]` instead of `map[str]`
**Code or Screenshots**
```python
from typing import Iterable
def print_branch_versions(branches: Iterable[str], branch_versions: Iterable[str]):
for [branch, version] in zip(branches, branch_versions):
print(f'{branch}: {version}')
branches: list[str] = []
branch_versions = map(lambda branch: (
f'{branch}-1.0'
if branch.startswith("versioned")
else branch
), branches)
print_branch_versions(branches=branches, branch_versions=branch_versions)
```
Pyright's output:
```
Argument of type "map[str | _T1@__new__]" cannot be assigned to parameter "branch_versions" of type "Iterable[str]" in function "print_branch_versions"
"map[str | _T1@__new__]" is incompatible with protocol "Iterable[str]"
"__iter__" is an incompatible type
Type "() -> map[str | _T1@__new__]" is not assignable to type "() -> Iterator[_T_co@Iterable]"
Function return type "map[str | _T1@__new__]" is incompatible with type "Iterator[_T_co@Iterable]"
"map[str | _T1@__new__]" is incompatible with protocol "Iterator[_T_co@Iterable]" (reportArgumentType)
```
Code sample in [pyright playground](https://pyright-play.net/?code=GYJw9gtgBALgngBwJYDsDmUkQWEMoCSMApiAIYBGANsQFC0AmxwUCIqMA%2BheSgMYALTgDdSAZyRgUYgBQ8y-AcTEAuQiXLViAbTEwQAXQA0UeYpHjJ0tUVKUau-QYCUK2lA9RguKNrOCTURAJKQNMFCgALyQEOV5BZRN-ISCQ6Vd3Tyy2DhlgAHIAb2SAXzVC1KsS-Od6ZOU1KiQ9R0MoAF5fA1pki2CrMQ6oCDJYqjIICgYyU3iBNRlMjwLiuZKAWgBGADoABnylzBZk7b0yPDEAdyQYARkAIkqpYgZ72qziKjFiWYVBWmcSTmylqORQXF6T2kcT%2BSjE7XqYiBsL6aXhkMsUjEtSAA)
Mypy is happy with this code: https://mypy-play.net/?mypy=latest&python=3.12&gist=86baf62660db59c5c4af554a531ffed5
Explicitly casting the else branch to `str` resolves the error.
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
Verify the diagnostic in the linked Pyright playground, then trace inference for the lambda's conditional expression and map construction. Done when the example no longer reports an argument-type error and the inferred result is compatible with Iterable[str].
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 48/100