Skip to content

Inconsistent type analysis in lambdas for redundant-expr #18807

@sk-

Description

@sk-

Bug Report
I found a weird issue related to redundant-expr that only happens when the code is defined as a lambda. The same code works fine when defined inside a regular function.

To Reproduce

from typing import Any

import pandas as pd

lambda val: {} if pd.isna(val) else val


def func(val: Any) -> Any:
    return {} if pd.isna(val) else val

Expected Behavior
None of the pd.isna calls are flagged as redundant
Actual Behavior

test_mypy.py:5: error: If condition is always false  [redundant-expr]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.15.0 (compiled: yes)
  • Mypy command-line flags: mypy --enable-error-code redundant-expr test_mypy.py
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.12.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions