Skip to content

CLN: Add typing for dtype argument in io directory (GH38808) #38814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pandas/io/excel/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from pandas._config import config

from pandas._libs.parsers import STR_NA_VALUES
from pandas._typing import Buffer, FilePathOrBuffer, StorageOptions
from pandas._typing import Buffer, DtypeArg, FilePathOrBuffer, StorageOptions
from pandas.compat._optional import import_optional_dependency
from pandas.errors import EmptyDataError
from pandas.util._decorators import Appender, deprecate_nonkeyword_arguments, doc
Expand Down Expand Up @@ -309,7 +309,7 @@ def read_excel(
index_col=None,
usecols=None,
squeeze=False,
dtype=None,
dtype: Optional[DtypeArg] = None,
engine=None,
converters=None,
true_values=None,
Expand Down Expand Up @@ -433,7 +433,7 @@ def parse(
index_col=None,
usecols=None,
squeeze=False,
dtype=None,
dtype: Optional[DtypeArg] = None,
true_values=None,
false_values=None,
skiprows=None,
Expand Down
8 changes: 5 additions & 3 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from pandas._libs.tslibs import iNaT
from pandas._typing import (
CompressionOptions,
DtypeArg,
FrameOrSeriesUnion,
IndexLabel,
JSONSerializable,
StorageOptions,
Expand Down Expand Up @@ -296,7 +298,7 @@ def read_json(
path_or_buf=None,
orient=None,
typ="frame",
dtype=None,
dtype: Optional[DtypeArg] = None,
convert_axes=None,
convert_dates=True,
keep_default_dates: bool = True,
Expand Down Expand Up @@ -775,7 +777,7 @@ def __init__(
self,
json,
orient,
dtype=None,
dtype: Optional[DtypeArg] = None,
convert_axes=True,
convert_dates=True,
keep_default_dates=False,
Expand Down Expand Up @@ -809,7 +811,7 @@ def __init__(
self.convert_dates = convert_dates
self.date_unit = date_unit
self.keep_default_dates = keep_default_dates
self.obj = None
self.obj: Optional[FrameOrSeriesUnion] = None

def check_keys_split(self, decoded):
"""
Expand Down
13 changes: 8 additions & 5 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import pandas._libs.parsers as parsers
from pandas._libs.parsers import STR_NA_VALUES
from pandas._libs.tslibs import parsing
from pandas._typing import FilePathOrBuffer, StorageOptions, Union
from pandas._typing import DtypeArg, FilePathOrBuffer, StorageOptions, Union
from pandas.errors import (
AbstractMethodError,
EmptyDataError,
Expand Down Expand Up @@ -546,7 +546,7 @@ def read_csv(
prefix=None,
mangle_dupe_cols=True,
# General Parsing Configuration
dtype=None,
dtype: Optional[DtypeArg] = None,
engine=None,
converters=None,
true_values=None,
Expand Down Expand Up @@ -626,7 +626,7 @@ def read_table(
prefix=None,
mangle_dupe_cols=True,
# General Parsing Configuration
dtype=None,
dtype: Optional[DtypeArg] = None,
engine=None,
converters=None,
true_values=None,
Expand Down Expand Up @@ -3502,20 +3502,23 @@ def _clean_index_names(columns, index_col, unnamed_cols):
return index_names, columns, index_col


def _get_empty_meta(columns, index_col, index_names, dtype=None):
def _get_empty_meta(columns, index_col, index_names, dtype: Optional[DtypeArg] = None):
columns = list(columns)

# Convert `dtype` to a defaultdict of some kind.
# This will enable us to write `dtype[col_name]`
# without worrying about KeyError issues later on.
if not isinstance(dtype, dict):
if not is_dict_like(dtype):
# if dtype == None, default will be object.
default_dtype = dtype or object
dtype = defaultdict(lambda: default_dtype)
else:
# Save a copy of the dictionary.
dtype = cast(dict, dtype)
_dtype = dtype.copy()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L3518 seems odd

Copy link
Contributor Author

@avinashpancham avinashpancham Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rewrote the code such that we don't have to make a copy of the dict


dtype = defaultdict(lambda: object)
dtype = cast(dict, dtype)

# Convert column indexes to column names.
for k, v in _dtype.items():
Expand Down
11 changes: 9 additions & 2 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@

from pandas._libs import lib, writers as libwriters
from pandas._libs.tslibs import timezones
from pandas._typing import ArrayLike, FrameOrSeries, FrameOrSeriesUnion, Label, Shape
from pandas._typing import (
ArrayLike,
DtypeArg,
FrameOrSeries,
FrameOrSeriesUnion,
Label,
Shape,
)
from pandas.compat._optional import import_optional_dependency
from pandas.compat.pickle_compat import patch_pickle
from pandas.errors import PerformanceWarning
Expand Down Expand Up @@ -2259,7 +2266,7 @@ def __init__(
table=None,
meta=None,
metadata=None,
dtype=None,
dtype: Optional[DtypeArg] = None,
data=None,
):
super().__init__(
Expand Down