Skip to content

Commit 1172d61

Browse files
WillAydjreback
authored andcommitted
Cleanup Typing in pandas.core.strings (#25904)
1 parent d591ade commit 1172d61

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

Diff for: mypy.ini

-3
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,6 @@ ignore_errors=True
137137
[mypy-pandas.core.sparse.frame]
138138
ignore_errors=True
139139

140-
[mypy-pandas.core.strings]
141-
ignore_errors=True
142-
143140
[mypy-pandas.core.util.hashing]
144141
ignore_errors=True
145142

Diff for: pandas/core/strings.py

+40-36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import codecs
33
import re
44
import textwrap
5+
from typing import Dict
56
import warnings
67

78
import numpy as np
@@ -28,7 +29,7 @@
2829
"utf-16", "utf-32"
2930
)
3031

31-
_shared_docs = dict()
32+
_shared_docs = dict() # type: Dict[str, str]
3233

3334

3435
def cat_core(list_of_columns, sep):
@@ -623,13 +624,13 @@ def str_repeat(arr, repeats):
623624
dtype: object
624625
"""
625626
if is_scalar(repeats):
626-
def rep(x):
627+
def scalar_rep(x):
627628
try:
628629
return bytes.__mul__(x, repeats)
629630
except TypeError:
630631
return str.__mul__(x, repeats)
631632

632-
return _na_map(rep, arr)
633+
return _na_map(scalar_rep, arr)
633634
else:
634635

635636
def rep(x, r):
@@ -2989,33 +2990,36 @@ def rindex(self, sub, start=0, end=None):
29892990
3 sWaPcAsE
29902991
dtype: object
29912992
""")
2992-
_shared_docs['lower'] = dict(type='lowercase', method='lower', version='')
2993-
_shared_docs['upper'] = dict(type='uppercase', method='upper', version='')
2994-
_shared_docs['title'] = dict(type='titlecase', method='title', version='')
2995-
_shared_docs['capitalize'] = dict(type='be capitalized',
2996-
method='capitalize', version='')
2997-
_shared_docs['swapcase'] = dict(type='be swapcased', method='swapcase',
2998-
version='')
2999-
_shared_docs['casefold'] = dict(type='be casefolded', method='casefold',
3000-
version='\n .. versionadded:: 0.25.0\n')
2993+
2994+
# _doc_args holds dict of strings to use in substituting casemethod docs
2995+
_doc_args = {} # type: Dict[str, Dict[str, str]]
2996+
_doc_args['lower'] = dict(type='lowercase', method='lower', version='')
2997+
_doc_args['upper'] = dict(type='uppercase', method='upper', version='')
2998+
_doc_args['title'] = dict(type='titlecase', method='title', version='')
2999+
_doc_args['capitalize'] = dict(type='be capitalized', method='capitalize',
3000+
version='')
3001+
_doc_args['swapcase'] = dict(type='be swapcased', method='swapcase',
3002+
version='')
3003+
_doc_args['casefold'] = dict(type='be casefolded', method='casefold',
3004+
version='\n .. versionadded:: 0.25.0\n')
30013005
lower = _noarg_wrapper(lambda x: x.lower(),
30023006
docstring=_shared_docs['casemethods'] %
3003-
_shared_docs['lower'])
3007+
_doc_args['lower'])
30043008
upper = _noarg_wrapper(lambda x: x.upper(),
30053009
docstring=_shared_docs['casemethods'] %
3006-
_shared_docs['upper'])
3010+
_doc_args['upper'])
30073011
title = _noarg_wrapper(lambda x: x.title(),
30083012
docstring=_shared_docs['casemethods'] %
3009-
_shared_docs['title'])
3013+
_doc_args['title'])
30103014
capitalize = _noarg_wrapper(lambda x: x.capitalize(),
30113015
docstring=_shared_docs['casemethods'] %
3012-
_shared_docs['capitalize'])
3016+
_doc_args['capitalize'])
30133017
swapcase = _noarg_wrapper(lambda x: x.swapcase(),
30143018
docstring=_shared_docs['casemethods'] %
3015-
_shared_docs['swapcase'])
3019+
_doc_args['swapcase'])
30163020
casefold = _noarg_wrapper(lambda x: x.casefold(),
30173021
docstring=_shared_docs['casemethods'] %
3018-
_shared_docs['casefold'])
3022+
_doc_args['casefold'])
30193023

30203024
_shared_docs['ismethods'] = ("""
30213025
Check whether all characters in each string are %(type)s.
@@ -3157,42 +3161,42 @@ def rindex(self, sub, start=0, end=None):
31573161
3 False
31583162
dtype: bool
31593163
""")
3160-
_shared_docs['isalnum'] = dict(type='alphanumeric', method='isalnum')
3161-
_shared_docs['isalpha'] = dict(type='alphabetic', method='isalpha')
3162-
_shared_docs['isdigit'] = dict(type='digits', method='isdigit')
3163-
_shared_docs['isspace'] = dict(type='whitespace', method='isspace')
3164-
_shared_docs['islower'] = dict(type='lowercase', method='islower')
3165-
_shared_docs['isupper'] = dict(type='uppercase', method='isupper')
3166-
_shared_docs['istitle'] = dict(type='titlecase', method='istitle')
3167-
_shared_docs['isnumeric'] = dict(type='numeric', method='isnumeric')
3168-
_shared_docs['isdecimal'] = dict(type='decimal', method='isdecimal')
3164+
_doc_args['isalnum'] = dict(type='alphanumeric', method='isalnum')
3165+
_doc_args['isalpha'] = dict(type='alphabetic', method='isalpha')
3166+
_doc_args['isdigit'] = dict(type='digits', method='isdigit')
3167+
_doc_args['isspace'] = dict(type='whitespace', method='isspace')
3168+
_doc_args['islower'] = dict(type='lowercase', method='islower')
3169+
_doc_args['isupper'] = dict(type='uppercase', method='isupper')
3170+
_doc_args['istitle'] = dict(type='titlecase', method='istitle')
3171+
_doc_args['isnumeric'] = dict(type='numeric', method='isnumeric')
3172+
_doc_args['isdecimal'] = dict(type='decimal', method='isdecimal')
31693173
isalnum = _noarg_wrapper(lambda x: x.isalnum(),
31703174
docstring=_shared_docs['ismethods'] %
3171-
_shared_docs['isalnum'])
3175+
_doc_args['isalnum'])
31723176
isalpha = _noarg_wrapper(lambda x: x.isalpha(),
31733177
docstring=_shared_docs['ismethods'] %
3174-
_shared_docs['isalpha'])
3178+
_doc_args['isalpha'])
31753179
isdigit = _noarg_wrapper(lambda x: x.isdigit(),
31763180
docstring=_shared_docs['ismethods'] %
3177-
_shared_docs['isdigit'])
3181+
_doc_args['isdigit'])
31783182
isspace = _noarg_wrapper(lambda x: x.isspace(),
31793183
docstring=_shared_docs['ismethods'] %
3180-
_shared_docs['isspace'])
3184+
_doc_args['isspace'])
31813185
islower = _noarg_wrapper(lambda x: x.islower(),
31823186
docstring=_shared_docs['ismethods'] %
3183-
_shared_docs['islower'])
3187+
_doc_args['islower'])
31843188
isupper = _noarg_wrapper(lambda x: x.isupper(),
31853189
docstring=_shared_docs['ismethods'] %
3186-
_shared_docs['isupper'])
3190+
_doc_args['isupper'])
31873191
istitle = _noarg_wrapper(lambda x: x.istitle(),
31883192
docstring=_shared_docs['ismethods'] %
3189-
_shared_docs['istitle'])
3193+
_doc_args['istitle'])
31903194
isnumeric = _noarg_wrapper(lambda x: x.isnumeric(),
31913195
docstring=_shared_docs['ismethods'] %
3192-
_shared_docs['isnumeric'])
3196+
_doc_args['isnumeric'])
31933197
isdecimal = _noarg_wrapper(lambda x: x.isdecimal(),
31943198
docstring=_shared_docs['ismethods'] %
3195-
_shared_docs['isdecimal'])
3199+
_doc_args['isdecimal'])
31963200

31973201
@classmethod
31983202
def _make_accessor(cls, data):

0 commit comments

Comments
 (0)