Skip to content

False positive on function call with walrus operator #17377

@mixilchenko

Description

@mixilchenko

Bug Report

In our codebase we chose specific way of working with pytest.mark.parametrize.
It is hard to look at tests which names are autogenerated from parameters list. To improve this we use parametrize(ids=) argument.
Unfortunately maintainability/readability of tests decreases if test cases and ids are written separately.
To resolve this issue we add some description to test cases and reuse it in ids with walrus operator.
For instance

class TestA:
    @pytest.mark.parametrize(
        'desc,a,b',
        _test_cases := (('a != b', 1, 2),), 
        ids=[desc for desc, *_ in _test_cases],
    )
    def test_func(self, desc: str, a: int, b: int) -> None:
        assert a == b, desc

Mypy says here that _test_cases does not exist which is incorrect

To Reproduce

I have created simple playground without pytest dependency to reproduce the error
https://mypy-play.net/?mypy=master&python=3.12&flags=check-untyped-defs&gist=8f98cbc918fbf8053a14f58c8011e375

Expected Behavior

Success: no issues found in 1 source file

Actual Behavior

main.py:11: error: Name "my_call_vars" is not defined [name-defined]

Your Environment

  • Mypy version used: 1.10.0 and master
  • Mypy command-line flags: no specific flags needed
  • Mypy configuration options from mypy.ini (and other config files): no specific configurations
  • Python version used: 3.10, 3.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-pep-572PEP 572 (walrus operator)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions