Skip to content

Commit 3a043f2

Browse files
authored
TYP: io.json._json, util._decorators (pandas-dev#36903)
1 parent 6e75d8c commit 3a043f2

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

pandas/io/json/_json.py

+33-14
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import BytesIO, StringIO
44
from itertools import islice
55
import os
6-
from typing import IO, Any, Callable, List, Optional, Type
6+
from typing import IO, Any, Callable, List, Optional, Tuple, Type
77

88
import numpy as np
99

@@ -111,6 +111,8 @@ def to_json(
111111

112112

113113
class Writer:
114+
_default_orient: str
115+
114116
def __init__(
115117
self,
116118
obj,
@@ -126,8 +128,7 @@ def __init__(
126128
self.obj = obj
127129

128130
if orient is None:
129-
# error: "Writer" has no attribute "_default_orient"
130-
orient = self._default_orient # type: ignore[attr-defined]
131+
orient = self._default_orient
131132

132133
self.orient = orient
133134
self.date_format = date_format
@@ -777,8 +778,8 @@ def read(self):
777778
obj = self._get_object_parser(lines_json)
778779
else:
779780
data = ensure_str(self.data)
780-
data = data.split("\n")
781-
obj = self._get_object_parser(self._combine_lines(data))
781+
data_lines = data.split("\n")
782+
obj = self._get_object_parser(self._combine_lines(data_lines))
782783
else:
783784
obj = self._get_object_parser(self.data)
784785
self.close()
@@ -848,6 +849,8 @@ def __next__(self):
848849

849850

850851
class Parser:
852+
_split_keys: Tuple[str, ...]
853+
_default_orient: str
851854

852855
_STAMP_UNITS = ("s", "ms", "us", "ns")
853856
_MIN_STAMPS = {
@@ -873,6 +876,7 @@ def __init__(
873876

874877
if orient is None:
875878
orient = self._default_orient
879+
876880
self.orient = orient
877881

878882
self.dtype = dtype
@@ -902,8 +906,8 @@ def check_keys_split(self, decoded):
902906
"""
903907
bad_keys = set(decoded.keys()).difference(set(self._split_keys))
904908
if bad_keys:
905-
bad_keys = ", ".join(bad_keys)
906-
raise ValueError(f"JSON data had unexpected key(s): {bad_keys}")
909+
bad_keys_joined = ", ".join(bad_keys)
910+
raise ValueError(f"JSON data had unexpected key(s): {bad_keys_joined}")
907911

908912
def parse(self):
909913

@@ -922,14 +926,22 @@ def parse(self):
922926
self._try_convert_types()
923927
return self.obj
924928

929+
def _parse_numpy(self):
930+
raise AbstractMethodError(self)
931+
932+
def _parse_no_numpy(self):
933+
raise AbstractMethodError(self)
934+
925935
def _convert_axes(self):
926936
"""
927937
Try to convert axes.
928938
"""
929-
for axis_name in self.obj._AXIS_ORDERS:
939+
obj = self.obj
940+
assert obj is not None # for mypy
941+
for axis_name in obj._AXIS_ORDERS:
930942
new_axis, result = self._try_convert_data(
931943
name=axis_name,
932-
data=self.obj._get_axis(axis_name),
944+
data=obj._get_axis(axis_name),
933945
use_dtypes=False,
934946
convert_dates=True,
935947
)
@@ -1083,7 +1095,11 @@ def _parse_numpy(self):
10831095
self.check_keys_split(decoded)
10841096
self.obj = create_series_with_explicit_dtype(**decoded)
10851097
elif self.orient in ["columns", "index"]:
1086-
self.obj = create_series_with_explicit_dtype(*data, dtype_if_empty=object)
1098+
# error: "create_series_with_explicit_dtype"
1099+
# gets multiple values for keyword argument "dtype_if_empty
1100+
self.obj = create_series_with_explicit_dtype(
1101+
*data, dtype_if_empty=object
1102+
) # type:ignore[misc]
10871103
else:
10881104
self.obj = create_series_with_explicit_dtype(data, dtype_if_empty=object)
10891105

@@ -1175,9 +1191,12 @@ def _process_converter(self, f, filt=None):
11751191
if filt is None:
11761192
filt = lambda col, c: True
11771193

1194+
obj = self.obj
1195+
assert obj is not None # for mypy
1196+
11781197
needs_new_obj = False
11791198
new_obj = dict()
1180-
for i, (col, c) in enumerate(self.obj.items()):
1199+
for i, (col, c) in enumerate(obj.items()):
11811200
if filt(col, c):
11821201
new_data, result = f(col, c)
11831202
if result:
@@ -1188,9 +1207,9 @@ def _process_converter(self, f, filt=None):
11881207
if needs_new_obj:
11891208

11901209
# possibly handle dup columns
1191-
new_obj = DataFrame(new_obj, index=self.obj.index)
1192-
new_obj.columns = self.obj.columns
1193-
self.obj = new_obj
1210+
new_frame = DataFrame(new_obj, index=obj.index)
1211+
new_frame.columns = obj.columns
1212+
self.obj = new_frame
11941213

11951214
def _try_convert_types(self):
11961215
if self.obj is None:

pandas/plotting/_matplotlib/converter.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import datetime as pydt
33
from datetime import datetime, timedelta, tzinfo
44
import functools
5-
from typing import Any, List, Optional, Tuple
5+
from typing import Any, Dict, List, Optional, Tuple
66

77
from dateutil.relativedelta import relativedelta
88
import matplotlib.dates as dates
@@ -1002,7 +1002,7 @@ def __init__(
10021002
self.format = None
10031003
self.freq = freq
10041004
self.locs: List[Any] = [] # unused, for matplotlib compat
1005-
self.formatdict = None
1005+
self.formatdict: Optional[Dict[Any, Any]] = None
10061006
self.isminor = minor_locator
10071007
self.isdynamic = dynamic_mode
10081008
self.offset = 0

pandas/util/_decorators.py

+3
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ def decorate(func):
278278
allow_args = allowed_args
279279
else:
280280
spec = inspect.getfullargspec(func)
281+
282+
# We must have some defaults if we are deprecating default-less
283+
assert spec.defaults is not None # for mypy
281284
allow_args = spec.args[: -len(spec.defaults)]
282285

283286
@wraps(func)

setup.cfg

-6
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,6 @@ check_untyped_defs=False
238238
[mypy-pandas.io.formats.style]
239239
check_untyped_defs=False
240240

241-
[mypy-pandas.io.json._json]
242-
check_untyped_defs=False
243-
244241
[mypy-pandas.io.parsers]
245242
check_untyped_defs=False
246243

@@ -264,6 +261,3 @@ check_untyped_defs=False
264261

265262
[mypy-pandas.plotting._misc]
266263
check_untyped_defs=False
267-
268-
[mypy-pandas.util._decorators]
269-
check_untyped_defs=False

0 commit comments

Comments
 (0)