Fix mypy error in pandas/tests.indexes.test_base.py
State: closed

- [x] relates to #28926 
- [x] tests added / passed
- [x] passes `black pandas`
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Labels:
name: Typing
description: type annotations, mypy/pyright type checking

Comments:
What error does this give?
Hello @WillAyd 
Thanks for responding.
It was giving error: Need type annotation for 'x'.
I have merged it again. Please review the changes.
@WillAyd  @simonjayhawkins  
mypy checks are failing see https://dev.azure.com/pandas-dev/pandas/_build/results?buildId=19636
I have started working on them.
Hello @simonjayhawkins @WillAyd 
It is giving the error: Slice index must be an integer or None.
This error is coming from mypy packages.  And I think there is a mypy version conflict. Because this bug is solved with (https://github.com/python/mypy/issues/2410)
The lines giving this error are (https://github.com/RushabhVasani/pandas/blob/fcddcfcda2de3306191067afe19bc86859665091/pandas/tests/indexes/test_base.py#L1741-L1756)
So I tried to run the same code on my local machine. And it all works fine. The code I run was,

 

```
@pytest.mark.parametrize( 
       "in_slice,expected",
        [
            (pd.IndexSlice[::-1], "yxdcb"),
            (pd.IndexSlice["b":"y":-1], ""),
            (pd.IndexSlice["b"::-1], "b"),
            (pd.IndexSlice[:"b":-1], "yxdcb"),
            (pd.IndexSlice[:"y":-1], "y"),
            (pd.IndexSlice["y"::-1], "yxdcb"),
            (pd.IndexSlice["y"::-4], "yb"),
            # absent labels
            (pd.IndexSlice[:"a":-1], "yxdcb"),
            (pd.IndexSlice[:"a":-2], "ydb"),
            (pd.IndexSlice["z"::-1], "yxdcb"),
            (pd.IndexSlice["z"::-3], "yc"),
            (pd.IndexSlice["m"::-1], "dcb"),
            (pd.IndexSlice[:"m":-1], "yx"),
            (pd.IndexSlice["a":"a":-1], ""),
            (pd.IndexSlice["z":"z":-1], ""),
            (pd.IndexSlice["m":"m":-1], ""),
        ],
    )
```
And the output it gave was,

`MarkDecorator(mark=Mark(name='parametrize', args=('in_slice,expected', [(slice(None, None, -1), 'yxdcb'), (slice('b', 'y', -1), ''), (slice('b', None, -1), 'b'), (slice(None, 'b', -1), 'yxdcb'), (slice(None, 'y', -1), 'y'), (slice('y', None, -1), 'yxdcb'), (slice('y', None, -4), 'yb'), (slice(None, 'a', -1), 'yxdcb'), (slice(None, 'a', -2), 'ydb'), (slice('z', None, -1), 'yxdcb'), (slice('z', None, -3), 'yc'), (slice('m', None, -1), 'dcb'), (slice(None, 'm', -1), 'yx'), (slice('a', 'a', -1), ''), (slice('z', 'z', -1), ''), (slice('m', 'm', -1), '')]), kwargs={}))`

So I think there is a mypy version conflict.

> So I think there is a mypy version conflict.

yes these errors are not reported in 0.730. It was a typeshed fix though, see https://github.com/python/typeshed/pull/3024 which was pulled into mypy for version 0.730

we are currently pinned to 0.720 on ci.

 >we are currently pinned to 0.720 on ci.

Okay, then what should be done now.
> > we are currently pinned to 0.720 on ci.
> 
> Okay, then what should be done now.

I guess two options.

1. seperate PR to bump mypy to 0.730 as a precursor to this
2. add `# type: ignore` with comments for error message and link to typeshed issue. 

eg.

https://github.com/pandas-dev/pandas/blob/2ca216101d03af76be3f5a4d6447571f0628fbb1/pandas/io/formats/printing.py#L490-L494

@WillAyd any preference?
@rushabh-v can you merge master. mypy has been bumped to 0.730 on ci.
It is giving the same error(Slice index must be an integer or None) again.
@simonjayhawkins  
Yea I see the same locally - can you investigate further @rushabh-v ?
Yes, I am looking into it.
I am sorry, I mistakenly committed directly to master.
It is giving this error for every version of mypy on my local machine. Even in 0.740.
>I guess two options.
>1. seperate PR to bump mypy to 0.730 as a precursor to this
>>2. add # type: ignore with comments for error message and link to typeshed issue.

Should we go with the 2nd option? @simonjayhawkins @WillAyd
I have checked locally adding, #type: ignore works fine on mypy 0.730.
replacing `(pd.IndexSlice["b":"y":-1], ""),` with `(pd.IndexSlice[slice("b", "y", -1)], ""),` silences mypy. so it appears that the current mypy errors are not directly related to/ been solved by https://github.com/python/typeshed/pull/3024

the mypy issue, https://github.com/python/mypy/issues/2410 is still open, so a `# type: ignore` with a comment referencing the mypy issue is probably the most practical solution here.


Hello @rushabh-v! Thanks for updating this PR. We checked the lines you've touched for [PEP 8](https://www.python.org/dev/peps/pep-0008) issues, and found:



There are currently no PEP 8 issues detected in this Pull Request. Cheers! :beers: 

##### Comment last updated at 2019-11-27 12:05:49 UTC
Thanks @rushabh-v