Skip to content

TYP: some types for pandas/_config/config.py #29897

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
Nov 29, 2019
Merged
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
14 changes: 8 additions & 6 deletions pandas/_config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from collections import namedtuple
from contextlib import contextmanager
import re
from typing import Dict, List
from typing import Any, Dict, Iterable, List
import warnings

DeprecatedOption = namedtuple("DeprecatedOption", "key msg rkey removal_ver")
Expand All @@ -64,7 +64,7 @@
_registered_options: Dict[str, RegisteredOption] = {}

# holds the current values for registered options
_global_config: Dict[str, str] = {}
_global_config: Dict[str, Any] = {}

# keys which have a special meaning
_reserved_keys: List[str] = ["all"]
Expand Down Expand Up @@ -412,7 +412,7 @@ def __exit__(self, *args):
_set_option(pat, val, silent=True)


def register_option(key, defval, doc="", validator=None, cb=None):
def register_option(key: str, defval: object, doc="", validator=None, cb=None):
"""Register an option in the package-wide pandas config object

Parameters
Expand Down Expand Up @@ -455,7 +455,9 @@ def register_option(key, defval, doc="", validator=None, cb=None):
path = key.split(".")

for k in path:
if not bool(re.match("^" + tokenize.Name + "$", k)):
# NOTE: tokenize.Name is not a public constant
# error: Module has no attribute "Name" [attr-defined]
if not bool(re.match("^" + tokenize.Name + "$", k)): # type: ignore
raise ValueError("{k} is not a valid identifier".format(k=k))
if keyword.iskeyword(k):
raise ValueError("{k} is a python keyword".format(k=k))
Expand Down Expand Up @@ -666,7 +668,7 @@ def pp_options_list(keys, width=80, _print=False):
from textwrap import wrap
from itertools import groupby

def pp(name, ks):
def pp(name: str, ks: Iterable[str]) -> List[str]:
pfx = "- " + name + ".[" if name else ""
ls = wrap(
", ".join(ks),
Expand All @@ -679,7 +681,7 @@ def pp(name, ks):
ls[-1] = ls[-1] + "]"
return ls

ls = []
ls: List[str] = []
singles = [x for x in sorted(keys) if x.find(".") < 0]
if singles:
ls += pp("", singles)
Expand Down