Skip to content

6.0.0rc1 pytest.fixture mypy error: Value of type "object" is not indexable #7494

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

Closed
1 of 4 tasks
simonjayhawkins opened this issue Jul 14, 2020 · 4 comments · Fixed by #7496
Closed
1 of 4 tasks

6.0.0rc1 pytest.fixture mypy error: Value of type "object" is not indexable #7494

simonjayhawkins opened this issue Jul 14, 2020 · 4 comments · Fixed by #7496
Labels
topic: typing type-annotation issue

Comments

@simonjayhawkins
Copy link

simonjayhawkins commented Jul 14, 2020

  • a detailed description of the bug or suggestion
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible
# file: pytest-mypy.py
import pytest


@pytest.fixture(params=[(0, 0), (1, 1)], ids=lambda x: x[0])
def b(request):
    return request.param


def test_b(b):
    pass
$ pytest ~/pytest-mypy.py -vv
=========================================================================== test session starts ============================================================================
platform win32 -- Python 3.8.3, pytest-6.0.0rc1, py-1.9.0, pluggy-0.13.1 -- c:\users\simon\anaconda3\envs\pandas-dev\python.exe
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('C:\\Users\\simon\\pandas\\.hypothesis\\examples')
rootdir: C:\Users\simon
plugins: hypothesis-5.16.3, asyncio-0.12.0, cov-2.10.0, forked-1.1.2, xdist-1.32.0
collected 2 items

..\pytest-mypy.py::test_b[0] PASSED                                                                                                                                   [ 50%]
..\pytest-mypy.py::test_b[1] PASSED                                                                                                                                   [100%]

============================================================================ 2 passed in 0.17s =============================================================================
$ mypy ~/pytest-mypy.py
C:\Users\simon\pytest-mypy.py:4: error: Value of type "object" is not indexable
Found 1 error in 1 file (checked 1 source file)

the type annotation for ids in fixture is

    ids: Optional[
        Union[
            Iterable[Union[None, str, float, int, bool]],
            Callable[[object], Optional[object]],
        ]
    ] = None,

i believe that the Callable should accept Any.

@simonjayhawkins
Copy link
Author

This also affects pytest.mark.parametrize

@pytest.mark.parametrize("func", get_is_dtype_funcs(), ids=lambda x: x.__name__)
def test_get_dtype_error_catch(func):

gives error: "object" has no attribute "__name__"; maybe "__ne__" or "__new__"?

the type annotation for _ParametrizeMarkDecorator is also

            ids: Optional[
                Union[
                    Iterable[Union[None, str, float, int, bool]],
                    Callable[[object], Optional[object]],
                ]
            ] = ...,

@simonjayhawkins
Copy link
Author

from https://docs.pytest.org/en/stable/fixture.html#parametrizing-fixtures

The above shows how ids can be either a list of strings to use or a function which will be called with the fixture value and then has to return a string to use. In the latter case if the function return None then pytest’s auto-generated ID will be used.

so the return type should be Optional[str]

@simonjayhawkins
Copy link
Author

so the return type should be Optional[str]

although maybe safer to use Any (or keep as object), since OP returns int and works OK.

@bluetech
Copy link
Member

Yes, I think this should be Any. I sent #7496 for this.

@nicoddemus nicoddemus added the topic: typing type-annotation issue label Jul 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: typing type-annotation issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants