python / python/mypy

FOO and next(iter(FOO))

Open
#10,696 0 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

Python code

FOO = frozenset("abc")
BAR = FOO and next(iter(FOO))

Mypy reports and error

wtf.py:2: error: Argument 1 to "iter" has incompatible type "FrozenSet[str]"; expected "Iterable[FrozenSet[str]]"

Expected Behavior

Ideally, mypy should infer that type of BAR is "falsey or string".
If not that, then "empty set or string".
if not that, then "set or string".

Actual Behavior

mypy seems to be tripped by X op func(X), gets confused 😖
mypy complains about the argument to iter 🤷🏿

Note that expression like BAR = next(iter(FOO)) is understood correctly.

Your Environment

  • Mypy version used: 0.910
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.9.5
  • Operating system and version: macos, universal2

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 by reproducing the minimal Python example with mypy 0.910 and inspect how the type checker handles the boolean and expression around iter(FOO). Done means the example no longer reports an incompatible argument error and the inferred type of BAR matches one of the expected alternatives.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.