Fix type annotations in pandas.core.indexes.datetimes
State: closed

- [x] closes #26366 
- [x] tests passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry

`pandas/core/indexes/timedeltas.py` taken care of as well since the errors were the same.

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

Comments:
Errors that still remain are:

    pandas\core\indexes\datetimes.py:90: error: Definition of "_data" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "Index"
    pandas\core\indexes\datetimes.py:90: error: Definition of "_data" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "DatetimelikeDelegateMixin"
    pandas\core\indexes\datetimes.py:90: error: Definition of "map" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "Index"
    pandas\core\indexes\datetimes.py:90: error: Definition of "_format_with_header" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "Index"
    pandas\core\indexes\datetimes.py:90: error: Definition of "isin" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "Index"

Needs some investigation to decide a single type to use consistently for these attributes across all class.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=h1) Report
> Merging [#26404](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/3b24fb678672ecb6ef24a1915c49f3cf815d5556?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/26404/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #26404      +/-   ##
==========================================
- Coverage   91.69%   91.68%   -0.01%     
==========================================
  Files         174      174              
  Lines       50743    50744       +1     
==========================================
- Hits        46527    46525       -2     
- Misses       4216     4219       +3
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.19% <100%> (ø)` | :arrow_up: |
| #single | `41.19% <100%> (-0.13%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/26404/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `96.86% <100%> (ø)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/26404/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `78.94% <0%> (-10.53%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/26404/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.02% <0%> (-0.12%)` | :arrow_down: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/26404/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `90.7% <0%> (+0.1%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=footer). Last update [3b24fb6...ca7e8eb](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=h1) Report
> Merging [#26404](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/44d5498d8697513b7a4638c62dd711b1539d2335?src=pr&el=desc) will **decrease** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/26404/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #26404      +/-   ##
==========================================
- Coverage   91.74%   91.74%   -0.01%     
==========================================
  Files         174      174              
  Lines       50763    50764       +1     
==========================================
- Hits        46575    46573       -2     
- Misses       4188     4191       +3
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.25% <100%> (ø)` | :arrow_up: |
| #single | `41.72% <83.33%> (-0.07%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/core/indexes/datetimes.py](https://codecov.io/gh/pandas-dev/pandas/pull/26404/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZXMucHk=) | `96.86% <100%> (ø)` | :arrow_up: |
| [pandas/core/indexes/datetimelike.py](https://codecov.io/gh/pandas-dev/pandas/pull/26404/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvaW5kZXhlcy9kYXRldGltZWxpa2UucHk=) | `98.13% <100%> (ø)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/26404/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2dicS5weQ==) | `78.94% <0%> (-10.53%)` | :arrow_down: |
| [pandas/core/frame.py](https://codecov.io/gh/pandas-dev/pandas/pull/26404/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97.02% <0%> (-0.12%)` | :arrow_down: |
| [pandas/util/testing.py](https://codecov.io/gh/pandas-dev/pandas/pull/26404/diff?src=pr&el=tree#diff-cGFuZGFzL3V0aWwvdGVzdGluZy5weQ==) | `90.7% <0%> (+0.1%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=footer). Last update [44d5498...c4a17b5](https://codecov.io/gh/pandas-dev/pandas/pull/26404?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@WillAyd some suggestion on what to do with these errors:

    pandas\core\indexes\datetimes.py:89: error: Definition of "_data" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "Index"
    pandas\core\indexes\datetimes.py:89: error: Definition of "_data" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "DatetimelikeDelegateMixin"
    pandas\core\indexes\datetimes.py:89: error: Definition of "map" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "Index"
    pandas\core\indexes\datetimes.py:89: error: Definition of "_format_with_header" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "Index"
    pandas\core\indexes\datetimes.py:89: error: Definition of "isin" in base class "DatetimeIndexOpsMixin" is incompatible with definition in base class "Index"

List of attributes with conflicting types:

1. _data

    Class | Type | Initial Value
    ----- | ----- | -------------
    DatetimeIndexOpsMixin |  DatetimeLikeArrayMixin | None
    DatetimelikeDelegateMixin | None (using reveal_type) | None
    Index | None (using reveal_type) | None

    I don't think we should use `DatetimeLikeArrayMixin` in all three classes. Should I remove the type comment from class `DatetimeIndexOpsMixin` too?  
    Making it `Optional[DatetimeLikeArrayMixin]` in class `DatetimeIndexOpsMixin` will not fix the error.

2. map

    Class | Defintion
    ----- | ----------
    DatetimeIndexOpsMixin | def map(self, f):
    Index | def map(self, mapper, na_action=None):

3. _format_with_header

    Class | Defintion
    ----- | ----------
    DatetimeIndexOpsMixin | def _format_with_header(self, header, **kwargs):
    Index | def _format_with_header(self, header, na_rep='NaN', **kwargs):

4. isin

    Class | Defintion
    ----- | ----------
    DatetimeIndexOpsMixin | def isin(self, values):
    Index | def isin(self, values, level=None):

I don't see any easy way of fixing the methods which have different parameters. Maybe we should just ignore these errors too.
Haven’t been able to deeply review but you can try aligning the signatures for the methods - seems like the difference is only optional keywords anyway
@WillAyd I can fix the last two but what about `def map(self, f)` and `def map(self, mapper, na_action=None)`. Is `f` and `mapper` the same thing?


I would think they are the same
this is ok, @vaibhavhrt you have some checks issue, can you fix up and ping on green
@WillAyd @jreback I have made all changes and tests are passing now. Please review.
@jreback please review and merge.
>    I don't think we should use `DatetimeLikeArrayMixin` in all three classes. Should I remove the type comment from class `DatetimeIndexOpsMixin` too?
>    Making it `Optional[DatetimeLikeArrayMixin]` in class `DatetimeIndexOpsMixin` will not fix the error.

I think `DatetimeLikeArrayMixin` for both `Datetime*` classes and `np.ndarray` for `Index` is also an option–it works, and it is the option that makes most sense to me. (Also because there is a comment that says https://github.com/pandas-dev/pandas/blob/65eac80dd4113ddbcee876029bfe03b4b3f11a3d/pandas/core/indexes/base.py#L522).

@plammens there are a million different classes that inherits from the `Index` class and all those child classes have some different type for data.
thanks @vaibhavhrt