Skip to content

CLN: replace Dict with Mapping to annotate arguments #29155

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 2 commits into from
Oct 22, 2019
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
13 changes: 12 additions & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from pathlib import Path
from typing import IO, TYPE_CHECKING, AnyStr, Iterable, Optional, TypeVar, Union
from typing import (
IO,
TYPE_CHECKING,
AnyStr,
Iterable,
Mapping,
Optional,
Sequence,
TypeVar,
Union,
)

import numpy as np

Expand All @@ -25,6 +35,7 @@
Scalar = Union[str, int, float, bool]
Axis = Union[str, int]
Ordered = Optional[bool]
Serializable = Union[Scalar, Sequence, Mapping]
Copy link
Member

Choose a reason for hiding this comment

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

Sorry comment might have gotten lost? OK with this move but can you change this to JSONSerializable? I think specific that that piece of IO (others may impose their own requirements)

Also I think this should stay with Dict and List for now; the C extension isn't as flexible as you would hope. Using other types will surface buggy behavior that MyPy can't detect

>>> import pandas._libs.json as json
>>> json.dumps(range(3))
'{"start":0,"stop":3}'  # Probably shouldn't output as a dictionary
>>> json.dumps(UserDict({"a": "b"}))
'{"data":{"a":"b"}}'  # Adds a "data" key the user probably didn't want

FWIW the standard module doesn't allow those to be serialized:

>>> import json
>>> json.dumps(range(3))
TypeError: Object of type range is not JSON serializable
>>> json.dumps(UserDict({"a": "b"}))
TypeError: Object of type UserDict is not JSON serializable

So maybe something can be borrowed from stdlib or typeshed for this alias

Copy link
Member Author

Choose a reason for hiding this comment

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

So maybe something can be borrowed from stdlib or typeshed for this alias

typeshed just uses Any

Also I think this should stay with Dict and List for now

agreed.

OK with this move but can you change this to JSONSerializable

sure.


# use Collection after we drop support for py35
Axes = Iterable
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" define extension dtypes """
import re
from typing import Any, Dict, List, Optional, Tuple, Type, Union, cast
from typing import Any, Dict, List, MutableMapping, Optional, Tuple, Type, Union, cast
import warnings

import numpy as np
Expand Down Expand Up @@ -351,7 +351,7 @@ def _finalize(self, categories, ordered: Ordered, fastpath: bool = False) -> Non
self._ordered = ordered if ordered is not ordered_sentinel else None
self._ordered_from_sentinel = ordered is ordered_sentinel

def __setstate__(self, state: Dict[str_type, Any]) -> None:
def __setstate__(self, state: MutableMapping[str_type, Any]) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

This is really nice; gives indication that the argument is mutated

# for pickle compat. __get_state__ is defined in the
# PandasExtensionDtype superclass and uses the public properties to
# pickle -> need to set the settable private ones here (see GH26067)
Expand Down
7 changes: 4 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
FrozenSet,
Hashable,
List,
Mapping,
Optional,
Sequence,
Set,
Expand Down Expand Up @@ -65,7 +66,7 @@
from pandas.core.dtypes.missing import isna, notna

import pandas as pd
from pandas._typing import Dtype, FilePathOrBuffer, Scalar
from pandas._typing import Dtype, FilePathOrBuffer, Serializable
from pandas.core import missing, nanops
import pandas.core.algorithms as algos
from pandas.core.base import PandasObject, SelectionMixin
Expand Down Expand Up @@ -2268,7 +2269,7 @@ def to_json(
double_precision: int = 10,
force_ascii: bool_t = True,
date_unit: str = "ms",
default_handler: Optional[Callable[[Any], Union[Scalar, List, Dict]]] = None,
default_handler: Optional[Callable[[Any], Serializable]] = None,
lines: bool_t = False,
compression: Optional[str] = "infer",
index: bool_t = True,
Expand Down Expand Up @@ -3133,7 +3134,7 @@ def to_csv(
index_label: Optional[Union[bool_t, str, Sequence[Hashable]]] = None,
mode: str = "w",
encoding: Optional[str] = None,
compression: Optional[Union[str, Dict[str, str]]] = "infer",
compression: Optional[Union[str, Mapping[str, str]]] = "infer",
quoting: Optional[int] = None,
quotechar: str = '"',
line_terminator: Optional[str] = None,
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/ops/array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ExtensionArrays.
"""
import operator
from typing import Any, Dict, Union
from typing import Any, Mapping, Union

import numpy as np

Expand Down Expand Up @@ -161,7 +161,7 @@ def arithmetic_op(
right: Any,
op,
str_rep: str,
eval_kwargs: Dict[str, bool],
eval_kwargs: Mapping[str, bool],
):
"""
Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ...
Expand Down
20 changes: 10 additions & 10 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
BinaryIO,
Dict,
List,
Mapping,
Optional,
TextIO,
Tuple,
Expand Down Expand Up @@ -276,16 +277,16 @@ def file_path_to_url(path: str) -> str:


def _get_compression_method(
compression: Optional[Union[str, Dict[str, str]]]
compression: Optional[Union[str, Mapping[str, str]]]
) -> Tuple[Optional[str], Dict[str, str]]:
"""
Simplifies a compression argument to a compression method string and
a dict containing additional arguments.
a mapping containing additional arguments.

Parameters
----------
compression : str or dict
If string, specifies the compression method. If dict, value at key
compression : str or mapping
If string, specifies the compression method. If mapping, value at key
'method' specifies compression method.

Returns
Expand All @@ -295,15 +296,14 @@ def _get_compression_method(

Raises
------
ValueError on dict missing 'method' key
ValueError on mapping missing 'method' key
"""
# Handle dict
if isinstance(compression, dict):
compression_args = compression.copy()
if isinstance(compression, Mapping):
compression_args = dict(compression)
try:
compression = compression_args.pop("method")
except KeyError:
raise ValueError("If dict, compression must have key 'method'")
raise ValueError("If mapping, compression must have key 'method'")
else:
compression_args = {}
return compression, compression_args
Expand Down Expand Up @@ -368,7 +368,7 @@ def _get_handle(
path_or_buf,
mode: str,
encoding=None,
compression: Optional[Union[str, Dict[str, Any]]] = None,
compression: Optional[Union[str, Mapping[str, Any]]] = None,
memory_map: bool = False,
is_text: bool = True,
):
Expand Down
3 changes: 2 additions & 1 deletion pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Dict,
Iterable,
List,
Mapping,
Optional,
Sequence,
Tuple,
Expand Down Expand Up @@ -78,7 +79,7 @@
from pandas import Series, DataFrame, Categorical

formatters_type = Union[
List[Callable], Tuple[Callable, ...], Dict[Union[str, int], Callable]
List[Callable], Tuple[Callable, ...], Mapping[Union[str, int], Callable]
]
float_format_type = Union[str, Callable, "EngFormatter"]

Expand Down
6 changes: 3 additions & 3 deletions pandas/io/formats/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from collections import OrderedDict
from textwrap import dedent
from typing import IO, Any, Dict, Iterable, List, Optional, Tuple, Union, cast
from typing import IO, Any, Dict, Iterable, List, Mapping, Optional, Tuple, Union, cast

from pandas._config import get_option

Expand Down Expand Up @@ -394,7 +394,7 @@ def _write_body(self, indent: int) -> None:
self.write("</tbody>", indent)

def _write_regular_rows(
self, fmt_values: Dict[int, List[str]], indent: int
self, fmt_values: Mapping[int, List[str]], indent: int
) -> None:
truncate_h = self.fmt.truncate_h
truncate_v = self.fmt.truncate_v
Expand Down Expand Up @@ -440,7 +440,7 @@ def _write_regular_rows(
)

def _write_hierarchical_rows(
self, fmt_values: Dict[int, List[str]], indent: int
self, fmt_values: Mapping[int, List[str]], indent: int
) -> None:
template = 'rowspan="{span}" valign="top"'

Expand Down
16 changes: 13 additions & 3 deletions pandas/io/formats/printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
"""

import sys
from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple, Union
from typing import (
Any,
Callable,
Iterable,
List,
Mapping,
Optional,
Sequence,
Tuple,
Union,
)

from pandas._config import get_option

from pandas.core.dtypes.inference import is_sequence

EscapeChars = Union[Dict[str, str], Iterable[str]]
EscapeChars = Union[Mapping[str, str], Iterable[str]]


def adjoin(space: int, *lists: List[str], **kwargs) -> str:
Expand Down Expand Up @@ -119,7 +129,7 @@ def _pprint_seq(


def _pprint_dict(
seq: Dict, _nest_lvl: int = 0, max_seq_items: Optional[int] = None, **kwds
seq: Mapping, _nest_lvl: int = 0, max_seq_items: Optional[int] = None, **kwds
) -> str:
"""
internal. pprinter for iterables. you should probably use pprint_thing()
Expand Down
6 changes: 2 additions & 4 deletions pandas/io/json/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from io import StringIO
from itertools import islice
import os
from typing import Any, Callable, Dict, List, Optional, Type, Union
from typing import Any, Callable, Optional, Type

import numpy as np

Expand All @@ -13,7 +13,7 @@
from pandas.core.dtypes.common import ensure_str, is_period_dtype

from pandas import DataFrame, MultiIndex, Series, compat, isna, to_datetime
from pandas._typing import Scalar
from pandas._typing import Serializable
from pandas.core.reshape.concat import concat

from pandas.io.common import (
Expand All @@ -34,8 +34,6 @@

TABLE_SCHEMA_VERSION = "0.20.0"

Serializable = Union[Scalar, List, Dict]


# interface to/from
def to_json(
Expand Down
4 changes: 2 additions & 2 deletions pandas/util/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from typing import (
Any,
Callable,
Dict,
List,
Mapping,
Optional,
Tuple,
Type,
Expand Down Expand Up @@ -104,7 +104,7 @@ def wrapper(*args, **kwargs) -> Callable[..., Any]:
def deprecate_kwarg(
old_arg_name: str,
new_arg_name: Optional[str],
mapping: Optional[Union[Dict[Any, Any], Callable[[Any], Any]]] = None,
mapping: Optional[Union[Mapping[Any, Any], Callable[[Any], Any]]] = None,
stacklevel: int = 2,
) -> Callable[..., Any]:
"""
Expand Down