Skip to content

Commit 55adcb8

Browse files
TYP: check_untyped_defs pandas.io.json._table_schema (#34695)
Co-authored-by: William Ayd <william.ayd@icloud.com>
1 parent a79ac36 commit 55adcb8

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

pandas/io/json/_table_schema.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
44
https://specs.frictionlessdata.io/json-table-schema/
55
"""
6+
from typing import TYPE_CHECKING, Any, Dict, Optional, cast
67
import warnings
78

89
import pandas._libs.json as json
9-
from pandas._typing import DtypeObj
10+
from pandas._typing import DtypeObj, FrameOrSeries, JSONSerializable
1011

1112
from pandas.core.dtypes.common import (
1213
is_bool_dtype,
@@ -24,6 +25,9 @@
2425
from pandas import DataFrame
2526
import pandas.core.common as com
2627

28+
if TYPE_CHECKING:
29+
from pandas.core.indexes.multi import MultiIndex # noqa: F401
30+
2731
loads = json.loads
2832

2933

@@ -103,7 +107,10 @@ def convert_pandas_type_to_json_field(arr):
103107
name = "values"
104108
else:
105109
name = arr.name
106-
field = {"name": name, "type": as_json_table_type(dtype)}
110+
field: Dict[str, JSONSerializable] = {
111+
"name": name,
112+
"type": as_json_table_type(dtype),
113+
}
107114

108115
if is_categorical_dtype(dtype):
109116
cats = dtype.categories
@@ -182,7 +189,12 @@ def convert_json_field_to_pandas_type(field):
182189
raise ValueError(f"Unsupported or invalid field type: {typ}")
183190

184191

185-
def build_table_schema(data, index=True, primary_key=None, version=True):
192+
def build_table_schema(
193+
data: FrameOrSeries,
194+
index: bool = True,
195+
primary_key: Optional[bool] = None,
196+
version: bool = True,
197+
) -> Dict[str, JSONSerializable]:
186198
"""
187199
Create a Table schema from ``data``.
188200
@@ -233,11 +245,12 @@ def build_table_schema(data, index=True, primary_key=None, version=True):
233245
if index is True:
234246
data = set_default_names(data)
235247

236-
schema = {}
248+
schema: Dict[str, Any] = {}
237249
fields = []
238250

239251
if index:
240252
if data.index.nlevels > 1:
253+
data.index = cast("MultiIndex", data.index)
241254
for level, name in zip(data.index.levels, data.index.names):
242255
new_field = convert_pandas_type_to_json_field(level)
243256
new_field["name"] = name

setup.cfg

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,6 @@ check_untyped_defs=False
268268
[mypy-pandas.io.json._json]
269269
check_untyped_defs=False
270270

271-
[mypy-pandas.io.json._table_schema]
272-
check_untyped_defs=False
273-
274271
[mypy-pandas.io.parsers]
275272
check_untyped_defs=False
276273

0 commit comments

Comments
 (0)