python / python/mypy

Bug with filter

Open
#8,704 1 comment 2 reactions 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

from typing import Optional
from typing import Sequence

from more_itertools import first


class A:
    def some_method(self):
        pass


def f(inp: Sequence[A]) -> Optional[A]:
    return first(filter(lambda a: a.some_method(), inp), None)

Output

test.py:13: error: Item "None" of "Optional[A]" has no attribute "some_method"

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 diagnostic with the Python snippet in the issue, then trace how mypy infers the callback and result types for filter and more_itertools.first. The fix is complete when this example no longer reports that the value may be None, while genuinely optional values remain checked correctly.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.