Removed ABCs from pandas._typing
State: closed

- [X] closes #27146
- [ ] tests added / passed
- [ ] passes `black pandas`
- [ ] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry

This reveals a few new typing issues so won't pass CI but submitting as draft

Here's one way I think that works to avoid import cycles. Basically any internal imports in pandas._typing need to be set in a TYPE_CHECKING block to prevent their runtime evaluation from causing import cycles.

To illustrate the effectiveness like this if you put something like this at the end of pandas._typing:

```python
def func(obj: FrameOrSeries) -> None:
    reveal_type(obj)
```

It would previously give you

```sh
pandas/_typing.py:43: note: Revealed type is 'Any'
```

Whereas now gives:

```sh
pandas/_typing.py:44: note: Revealed type is 'pandas.core.series.Series*'
pandas/_typing.py:44: note: Revealed type is 'pandas.core.frame.DataFrame*'
```

@topper-123 any way to check VSCode to see if this works with code completion?

In the long run this may not be needed with PEP 563:

https://www.python.org/dev/peps/pep-0563/

But I think this hack gets us there in the meantime

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

Comments:
So the mypy complaints may be pointing to unexpected behavior with ExtensionArrays since `casting` is not part of their signature. To illustrate:

```python
>>> import pandas as pd
>>> from pandas.core.dtypes.common import ensure_int_or_float

>>> ensure_int_or_float(pd.Series(range(3), dtype='Int64'))
0    0
1    1
2    2
dtype: int64

# Directly against PandasArray now
>>> ensure_int_or_float(pd.Series(range(3), dtype='Int64').array)
array([0., 1., 2.])  # returns float
```

Can ignore for now but bringing up in case it yields any insights @jreback @TomAugspurger 

The IntervalIndex complaint may or may not be a bug as well @jschendel 

I've tried it using reveal_type on on ``klass`` in pandas/groupby/generic.py, and it works.

PyCharm doesn't give code completions on this though, but that could possibly be that TypeVars are not implemented in PyCharm, maybe. I haven't myself gotten TypeVars to work in PyCharm, and I've tried several versions.

I don't use VSCode, so I don't if it gives completions there
Yea no problem - makes sense

Sent from my iPhone

> On Jul 20, 2019, at 1:05 PM, Jeff Reback <notifications@github.com> wrote:
> 
> @jreback commented on this pull request.
> 
> In pandas/core/dtypes/common.py:
> 
> > @@ -168,11 +168,11 @@ def ensure_int_or_float(arr: ArrayLike, copy=False) -> np.array:
>      will remain unchanged.
>      """
>      try:
> -        return arr.astype("int64", copy=copy, casting="safe")
> +        return arr.astype("int64", copy=copy, casting="safe")  # type: ignore
> ok, so mypy is failing on this then? I agree might be a problem, but hiding it is not the answer (or if you do then pls open an issue and point to that issue so can address elsewhere).
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub, or mute the thread.

Hello @WillAyd! 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-07-24 15:11:11 UTC
@WillAyd 

taken a quick look as requested in https://github.com/pandas-dev/pandas/pull/27512#issuecomment-513605539

> There's some failures showing up in there in the io.formats file so may uncover some others here as well since that's a central module getting touched

there is only one mypy error `pandas/core/window.py:266: error: Incompatible return value type (got "Series", expected "DataFrame")` and that can be fixed by using a Union instead of TypeVar.


@simonjayhawkins thanks for checking these. Actually was getting an error locally in io.formats which is why I pinged you on this but doesn't appear on CI here so might have just been a local merge issue. I'll push an update to the issue here today to see how it looks
@WillAyd can you rebase
@WillAyd lgtm. can you rebase once more (as merged bunch of things). looks good to merge (I know you had some open quesitons, but if ok can address in followup)
Thanks for review @jreback and @simonjayhawkins