Convert Unions to TypeVar
State: closed

- [x] closes #26453 
- [x] tests passed
- [x] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`

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

Comments:
@WillAyd take a look, there are some more Union but they error, I will look at them later.
Looks good at first glance. FilePathOrBuffer probably makes sense to keep a Union if that’s what is causing issues as we don’t really use those in containers and typically doesn’t care to maintain their type in generic functions (I.e. we intentionally convert from say strings to buffers)
@WillAyd got some failing tests of travis, even with just these changes. It doesn't makes much sense to why it's failing, you can check it here: https://travis-ci.com/vaibhavhrt/pandas/jobs/204447635
Might have to merge master to resolve but yes unrelated to this PR
lgtm. this is a draft PR, can you update; ping on green (and merge master).
@jreback I still have a few things to do in this PR. I mark is ready once I am done.
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26588?src=pr&el=h1) Report
> Merging [#26588](https://codecov.io/gh/pandas-dev/pandas/pull/26588?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/c9c6c221865f2988c0f52dcada6b033afaf23a8a?src=pr&el=desc) will **decrease** coverage by `50.1%`.
> The diff coverage is `100%`.

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

```diff
@@             Coverage Diff             @@
##           master   #26588       +/-   ##
===========================================
- Coverage   91.88%   41.78%   -50.11%     
===========================================
  Files         174      174               
  Lines       50694    50661       -33     
===========================================
- Hits        46581    21167    -25414     
- Misses       4113    29494    +25381
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `?` | |
| #single | `41.78% <100%> (-0.21%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26588?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/\_typing.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL190eXBpbmcucHk=) | `100% <100%> (ø)` | :arrow_up: |
| [pandas/io/formats/latex.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvbGF0ZXgucHk=) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/io/sas/sas\_constants.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3Nhcy9zYXNfY29uc3RhbnRzLnB5) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/core/groupby/categorical.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZ3JvdXBieS9jYXRlZ29yaWNhbC5weQ==) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/tseries/plotting.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL3RzZXJpZXMvcGxvdHRpbmcucHk=) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/tseries/converter.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL3RzZXJpZXMvY29udmVydGVyLnB5) | `0% <0%> (-100%)` | :arrow_down: |
| [pandas/io/formats/html.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL2Zvcm1hdHMvaHRtbC5weQ==) | `0% <0%> (-99.37%)` | :arrow_down: |
| [pandas/io/sas/sas7bdat.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3Nhcy9zYXM3YmRhdC5weQ==) | `0% <0%> (-91.16%)` | :arrow_down: |
| [pandas/io/sas/sas\_xport.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL2lvL3Nhcy9zYXNfeHBvcnQucHk=) | `0% <0%> (-90.1%)` | :arrow_down: |
| [pandas/core/sparse/scipy\_sparse.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvc3BhcnNlL3NjaXB5X3NwYXJzZS5weQ==) | `10.14% <0%> (-89.86%)` | :arrow_down: |
| ... and [129 more](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26588?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/26588?src=pr&el=footer). Last update [c9c6c22...077c7c2](https://codecov.io/gh/pandas-dev/pandas/pull/26588?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/26588?src=pr&el=h1) Report
> Merging [#26588](https://codecov.io/gh/pandas-dev/pandas/pull/26588?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/c9c6c221865f2988c0f52dcada6b033afaf23a8a?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/26588/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/26588?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master   #26588      +/-   ##
==========================================
- Coverage   91.88%   91.87%   -0.01%     
==========================================
  Files         174      174              
  Lines       50694    50694              
==========================================
- Hits        46581    46577       -4     
- Misses       4113     4117       +4
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.41% <100%> (ø)` | :arrow_up: |
| #single | `41.92% <100%> (-0.06%)` | :arrow_down: |

| [Impacted Files](https://codecov.io/gh/pandas-dev/pandas/pull/26588?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pandas/\_typing.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/diff?src=pr&el=tree#diff-cGFuZGFzL190eXBpbmcucHk=) | `100% <100%> (ø)` | :arrow_up: |
| [pandas/io/gbq.py](https://codecov.io/gh/pandas-dev/pandas/pull/26588/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/26588/diff?src=pr&el=tree#diff-cGFuZGFzL2NvcmUvZnJhbWUucHk=) | `97% <0%> (-0.12%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/26588?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/26588?src=pr&el=footer). Last update [c9c6c22...2d3376a](https://codecov.io/gh/pandas-dev/pandas/pull/26588?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

@jreback you can review now. Commits got little messed up when I was trying to delete one of my commits, but I guess that should not be problem.
I think Dtype is fine as a Union - we use all of those interchangeably. 

I think it’s a mistake for DatetimeLikeScalar to be a Type - maybe should make that a TypeVar instead and Update annotation where used to be Type[DatetimeLikeScalar]

Sent from my iPhone

> On Jun 6, 2019, at 1:58 PM, Vaibhav Vishal <notifications@github.com> wrote:
> 
> @vaibhavhrt commented on this pull request.
> 
> In pandas/_typing.py:
> 
> > @@ -11,12 +11,13 @@
>  from pandas.core.dtypes.generic import (
>      ABCExtensionArray, ABCIndexClass, ABCSeries, ABCSparseSeries)
>  
> -AnyArrayLike = Union[ABCExtensionArray,
> -                     ABCIndexClass,
> -                     ABCSeries,
> -                     ABCSparseSeries,
> -                     np.ndarray]
> -ArrayLike = Union[ABCExtensionArray, np.ndarray]
> +AnyArrayLike = TypeVar('AnyArrayLike',
> OK, Remaining types are:
> 
> DatetimeLikeScalar = Type[Union[Period, Timestamp, Timedelta]]
> Dtype = Union[str, np.dtype, ExtensionDtype]
> FilePathOrBuffer = Union[str, Path, IO[AnyStr]]
> Union in DatetimeLikeScalar is nested don't know if it should be converted to TypeVar
> Converting Dtype to TypeVar starts giving errors.
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub, or mute the thread.

@WillAyd  I will do that. You mean this  👇 , right?

    DatetimeLikeScalar = TypeVar(...)

@jreback hold on merging for now.
thanks @vaibhavhrt