python / python/mypy

Inline any(filter(...)) false-positive errors

Open
#16,277 0 comments 1 reaction 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

Looks like mypy loses information about filter parameters only when put directly (inline) into any function with list of objects.

  • It works fine without any function for example using list: list(filter(...)).
  • It works fine with list of primitive types like integers: any(filter(lambda x: x == 1, [1, 2, 3])).
  • Works fine when filter object is saved on variable before applying any.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=2a5a544f2e2ae02f57066ffe5adfa8b7

Expected Behavior

No errors

Actual Behavior

error: Argument 1 to "filter" has incompatible type "Callable[[Any], Any]"; expected "Callable[[Foo], TypeGuard[object]]"  [arg-type]
error: "object" has no attribute "value"  [attr-defined]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.5.1/1.6.0
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): not allowed to share but nothing special, just some import ignores
  • Python version used: 3.11.0rc1

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 with the linked mypy-play reproduction and compare the inline any(filter(...)) case with the working list, primitive, and saved-variable examples. Trace the reported filter argument and object attribute errors, then verify that the reproduction produces no errors without regressing the working cases.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.