Skip to content

False positive on function call with walrus operator #17377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
mixilchenko opened this issue Jun 13, 2024 · 0 comments
Open

False positive on function call with walrus operator #17377

mixilchenko opened this issue Jun 13, 2024 · 0 comments
Labels
bug mypy got something wrong topic-pep-572 PEP 572 (walrus operator)

Comments

@mixilchenko
Copy link

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
@mixilchenko mixilchenko added the bug mypy got something wrong label Jun 13, 2024
@JelleZijlstra JelleZijlstra added the topic-pep-572 PEP 572 (walrus operator) label Jun 13, 2024
it176131 added a commit to it176131/pandas that referenced this issue Apr 23, 2025
	- Removed walrus operator declarations in parametrized marker for test :meth:TestPivotTable.test_pivot_table_values_as_two_params`. Appears related to this mypy issue -> python/mypy#17377.
mroeschke pushed a commit to pandas-dev/pandas that referenced this issue Apr 23, 2025
* modified:   pandas/tests/reshape/test_pivot_multilevel.py
	- Added two tests, :func:`test_pivot_table_values_in_columns` and :func:`test_pivot_table_values_in_index`, to ensure that the `values` param is still used when the argument is shared between the `columns` and `values` params, and `index` and `values` params.

* modified:   pandas/core/reshape/pivot.py
	- Added condition to :func:`__internal_pivot_table` to aggregate `values` explicitly if `values` were passed, otherwise aggregate all remaining columns. This allows the tests :func:`test_pivot_table_values_in_columns` and :func:`test_pivot_table_values_in_index` in test_pivot_multilevel.py to pass.

* modified:   pandas/tests/reshape/test_pivot.py
	- Added test :func:`test_pivot_table_values_as_two_params` to test that the updates in pivot.py result in expected results, satisfying GH issue #57876.

* modified:   pandas/tests/reshape/test_pivot.py
	- Added GH issue comment to test :func:`test_pivot_table_values_as_two_params`.

* modified:   pandas/tests/reshape/test_pivot_multilevel.py
	- Combined tests :func:`test_pivot_table_values_in_columns` and :func:`test_pivot_table_values_in_index` into a single parametrized test, :func:`test_pivot_table_multiindex_values_as_two_params` to reduce duplicate setup code.

* modified:   pandas/tests/reshape/test_pivot_multilevel.py
	- Added GH issue #61292 as comment to test :func:`test_pivot_table_multiindex_values_as_two_params`.

* modified:   pandas/core/reshape/pivot.py
	- Simplified proposed logic in :func:`__internal_pivot_table`.

* modified:   pandas/core/reshape/pivot.py
	- Added GH issue numbers to new logic in :func:`__internal_pivot_table`.

* modified:   pandas/core/reshape/pivot.py
	- Added ignore-comment to silence mypy error in :func:`__internal_pivot_table`.
	- Added TODO-comment stating that the :meth:`DataFrameGroupBy.__getitem__` should be overloaded to match the pandas-stubs type declarations, informing mypy that the type is correct given `values` is a list.

* modified:   doc/source/whatsnew/v3.0.0.rst
	- Added pivot_table bug to Bugs/Reshaping section referencing issues #57876 and #61292.

* modified:   pandas/core/reshape/pivot.py
	- Moved and simplified mypy comment per feedback.

* modified:   pandas/core/reshape/pivot.py
	- Removed comment about explicit aggregation per feedback.

* modified:   pandas/tests/reshape/test_pivot.py
	- Removed param names and updated `argnames` arg per feedback in parametrized marker.

* modified:   pandas/tests/reshape/test_pivot.py
	- Removed param names in favor of implicit args per feedback.

* modified:   pandas/tests/reshape/test_pivot_multilevel.py
	- Removed param names and updated arg for `argnames` in parametrized marker per feedback.

* modified:   pandas/tests/reshape/test_pivot_multilevel.py
	- Reduced `expected` assignments from two to one per feedback.

* modified:   pandas/tests/reshape/test_pivot_multilevel.py
	- Removed param names in favor of implicit args per feedback.

* modified:   pandas/tests/reshape/test_pivot_multilevel.py
	- Moved e_data, e_index, and e_cols to parametrized marker instead of declaring inside the test :func:`test_pivot_table_multiindex_values_as_two_params`.

* modified:   pandas/tests/reshape/test_pivot.py
	- Moved `expected` setup to parametrized marker instead of in the test :meth:`TestPivotTable.test_pivot_table_values_as_two_params`.

* modified:   pandas/tests/reshape/test_pivot.py
	- Removed walrus operator declarations in parametrized marker for test :meth:TestPivotTable.test_pivot_table_values_as_two_params`. Appears related to this mypy issue -> python/mypy#17377.

* modified:   pandas/tests/reshape/test_pivot_multilevel.py
	- Removed walrus operator declarations as I'm sure mypy would raise an issue with it given that it did in test_pivot.py (see commit 52cf560).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-pep-572 PEP 572 (walrus operator)
Projects
None yet
Development

No branches or pull requests

2 participants