CLN: core/dtypes/cast.py::maybe_downcast_to_dtype
State: closed

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


Labels:
name: Dtype Conversions
description: Unexpected or buggy dtype conversions
name: Clean

Comments:
thanks @arw2019 
@arw2019 this solution could be to revert this and re-open? The mypy errors are

```
pandas\core\dtypes\cast.py:173: error: Item "type" of "Union[ExtensionDtype, Any, Type[object]]" has no attribute "type"  [union-attr]
pandas\core\dtypes\cast.py:179: error: Item "ExtensionDtype" of "Union[ExtensionDtype, Any, Type[object]]" has no attribute "freq"  [union-attr]
pandas\core\dtypes\cast.py:179: error: Item "type" of "Union[ExtensionDtype, Any, Type[object]]" has no attribute "freq"  [union-attr]
```

so i'm a bit reluctant to ignore to get mypy to green.

The issue is related to https://github.com/pandas-dev/pandas/pull/37024#discussion_r503495620

so maybe only str and dtypeObj are accepted. so could also change to Union[str, DtypeObj] but this I _think_ is unusual and should normally be DtypeObj or Dtype.
> @arw2019 this solution could be to revert this and re-open? The mypy errors are
> 
> ```
> pandas\core\dtypes\cast.py:173: error: Item "type" of "Union[ExtensionDtype, Any, Type[object]]" has no attribute "type"  [union-attr]
> pandas\core\dtypes\cast.py:179: error: Item "ExtensionDtype" of "Union[ExtensionDtype, Any, Type[object]]" has no attribute "freq"  [union-attr]
> pandas\core\dtypes\cast.py:179: error: Item "type" of "Union[ExtensionDtype, Any, Type[object]]" has no attribute "freq"  [union-attr]
> ```
> 
> so i'm a bit reluctant to ignore to get mypy to green.

Your call since I'm still pretty new to the code base.

> The issue is related to [#37024 (comment)](https://github.com/pandas-dev/pandas/pull/37024#discussion_r503495620)
> 
> so maybe only str and dtypeObj are accepted. so could also change to Union[str, DtypeObj] but this I _think_ is unusual and should normally be DtypeObj or Dtype.

Again I'm fairly new but I think it's possible `Union[str, DtypeObj]` is right, at least based on the tests we've got for this:
```
(pandas-dev) root@27041e356f25:/workspaces/pandas-arw2019# grep -r -n maybe_downcast_to_dtype pandas/tests
pandas/tests/dtypes/cast/test_downcast.py:6:from pandas.core.dtypes.cast import maybe_downcast_to_dtype
pandas/tests/dtypes/cast/test_downcast.py:40:    result = maybe_downcast_to_dtype(arr, dtype)
pandas/tests/dtypes/cast/test_downcast.py:47:    result = maybe_downcast_to_dtype(ser, np.dtype(np.float64))
pandas/tests/dtypes/cast/test_downcast.py:58:    result = maybe_downcast_to_dtype(arr, "infer")
pandas/tests/dtypes/cast/test_downcast.py:69:    result = maybe_downcast_to_dtype(arr, "infer")
pandas/tests/dtypes/cast/test_downcast.py:76:    result = maybe_downcast_to_dtype(arr, "int64")
pandas/tests/dtypes/cast/test_downcast.py:86:    res = maybe_downcast_to_dtype(arr, dtype)
pandas/tests/dtypes/cast/test_downcast.py:97:    res = maybe_downcast_to_dtype(obj, exp.dtype)
```
For reference actually only use this method in a few places in the code:
```
pandas/core/frame.py:88:    maybe_downcast_to_dtype,
pandas/core/frame.py:6247:            arr = maybe_downcast_to_dtype(arr, this_dtype)
pandas/core/indexes/interval.py:22:    maybe_downcast_to_dtype,
pandas/core/indexes/interval.py:1301:            breaks = maybe_downcast_to_dtype(breaks, "int64")
pandas/core/tools/numeric.py:5:from pandas.core.dtypes.cast import maybe_downcast_to_dtype
pandas/core/tools/numeric.py:182:                    values = maybe_downcast_to_dtype(values, dtype)
pandas/core/reshape/pivot.py:18:from pandas.core.dtypes.cast import maybe_downcast_to_dtype
pandas/core/reshape/pivot.py:128:                agged[v] = maybe_downcast_to_dtype(agged[v], data[v].dtype)
pandas/core/reshape/pivot.py:275:                maybe_downcast_to_dtype, args=(dtype,)
pandas/core/dtypes/cast.py:122:def maybe_downcast_to_dtype(result, dtype):
pandas/core/dtypes/cast.py:189:    Subset of maybe_downcast_to_dtype restricted to numeric dtypes.
pandas/core/dtypes/cast.py:298:            result = maybe_downcast_to_dtype(result, dtype)
pandas/core/internals/blocks.py:24:    maybe_downcast_to_dtype,
pandas/core/internals/blocks.py:533:            nv = maybe_downcast_to_dtype(values, dtypes)
pandas/core/internals/blocks.py:550:            val = maybe_downcast_to_dtype(val, dtype="infer")
```
we'll go with the revert for now even though the fix is straightfoward to allow unpressured discussion on the topic. OK with reopening this PR?
> we'll go with the revert for now even though the fix is straightfoward to allow unpressured discussion on the topic. OK with reopening this PR?

Sounds good to me!