Skip to content

Commit 9eb1d12

Browse files
committed
stricter types
1 parent dee457e commit 9eb1d12

File tree

2 files changed

+196
-15
lines changed

2 files changed

+196
-15
lines changed

IPython/utils/text.py

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
import os
1111
import re
1212
import string
13+
import sys
1314
import textwrap
1415
import warnings
1516
from string import Formatter
1617
from pathlib import Path
1718

18-
from typing import List, Dict, Tuple, Optional, cast
19+
from typing import List, Dict, Tuple, Optional, cast, Sequence, Mapping, Any
20+
21+
if sys.version_info < (3, 12):
22+
from typing_extensions import Self
23+
else:
24+
from typing import Self
1925

2026

2127
class LSString(str):
@@ -34,7 +40,11 @@ class LSString(str):
3440
Such strings are very useful to efficiently interact with the shell, which
3541
typically only understands whitespace-separated options for commands."""
3642

37-
def get_list(self):
43+
__list: List[str]
44+
__spstr: str
45+
__paths: List[Path]
46+
47+
def get_list(self) -> List[str]:
3848
try:
3949
return self.__list
4050
except AttributeError:
@@ -43,7 +53,7 @@ def get_list(self):
4353

4454
l = list = property(get_list)
4555

46-
def get_spstr(self):
56+
def get_spstr(self) -> str:
4757
try:
4858
return self.__spstr
4959
except AttributeError:
@@ -52,12 +62,12 @@ def get_spstr(self):
5262

5363
s = spstr = property(get_spstr)
5464

55-
def get_nlstr(self):
65+
def get_nlstr(self) -> Self:
5666
return self
5767

5868
n = nlstr = property(get_nlstr)
5969

60-
def get_paths(self):
70+
def get_paths(self) -> List[Path]:
6171
try:
6272
return self.__paths
6373
except AttributeError:
@@ -92,12 +102,16 @@ class SList(list):
92102
Any values which require transformations are computed only once and
93103
cached."""
94104

95-
def get_list(self):
105+
__spstr: str
106+
__nlstr: str
107+
__paths: List[Path]
108+
109+
def get_list(self) -> Self:
96110
return self
97111

98112
l = list = property(get_list)
99113

100-
def get_spstr(self):
114+
def get_spstr(self) -> str:
101115
try:
102116
return self.__spstr
103117
except AttributeError:
@@ -106,7 +120,7 @@ def get_spstr(self):
106120

107121
s = spstr = property(get_spstr)
108122

109-
def get_nlstr(self):
123+
def get_nlstr(self) -> str:
110124
try:
111125
return self.__nlstr
112126
except AttributeError:
@@ -115,7 +129,7 @@ def get_nlstr(self):
115129

116130
n = nlstr = property(get_nlstr)
117131

118-
def get_paths(self):
132+
def get_paths(self) -> List[Path]:
119133
try:
120134
return self.__paths
121135
except AttributeError:
@@ -538,7 +552,9 @@ class FullEvalFormatter(Formatter):
538552
"""
539553
# copied from Formatter._vformat with minor changes to allow eval
540554
# and replace the format_spec code with slicing
541-
def vformat(self, format_string: str, args, kwargs) -> str:
555+
def vformat(
556+
self, format_string: str, args: Sequence[Any], kwargs: Mapping[str, Any]
557+
) -> str:
542558
result = []
543559
conversion: Optional[str]
544560
for literal_text, field_name, format_spec, conversion in self.parse(
@@ -559,7 +575,7 @@ def vformat(self, format_string: str, args, kwargs) -> str:
559575

560576
# eval the contents of the field for the object
561577
# to be formatted
562-
obj = eval(field_name, kwargs)
578+
obj = eval(field_name, dict(kwargs))
563579

564580
# do any conversion on the resulting object
565581
# type issue in typeshed, fined in https://github.com/python/typeshed/pull/11377
@@ -629,7 +645,9 @@ def _col_chunks(l, max_rows, row_first=False):
629645
yield l[i:(i + max_rows)]
630646

631647

632-
def _find_optimal(rlist, row_first: bool, separator_size: int, displaywidth: int):
648+
def _find_optimal(
649+
rlist: List[str], row_first: bool, separator_size: int, displaywidth: int
650+
) -> Dict[str, Any]:
633651
"""Calculate optimal info to columnize a list of string"""
634652
for max_rows in range(1, len(rlist) + 1):
635653
col_widths = list(map(max, _col_chunks(rlist, max_rows, row_first)))
@@ -653,7 +671,12 @@ def _get_or_default(mylist, i, default=None):
653671

654672

655673
def compute_item_matrix(
656-
items, row_first: bool = False, empty=None, *, separator_size=2, displaywidth=80
674+
items: List[str],
675+
row_first: bool = False,
676+
empty: Optional[str] = None,
677+
*,
678+
separator_size: int = 2,
679+
displaywidth: int = 80,
657680
) -> Tuple[List[List[int]], Dict[str, int]]:
658681
"""Returns a nested list, and info to columnize items
659682
@@ -710,7 +733,7 @@ def compute_item_matrix(
710733
category=PendingDeprecationWarning,
711734
)
712735
info = _find_optimal(
713-
list(map(len, items)),
736+
list(map(len, items)), # type: ignore[arg-type]
714737
row_first,
715738
separator_size=separator_size,
716739
displaywidth=displaywidth,
@@ -728,7 +751,7 @@ def columnize(
728751
separator: str = " ",
729752
displaywidth: int = 80,
730753
spread: bool = False,
731-
):
754+
) -> str:
732755
"""Transform a list of strings into a single string with columns.
733756
734757
Parameters

pyproject.toml

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,164 @@ exclude = [
136136
'IPython/utils/_process_win32.py',
137137
'IPython/utils/path.py',
138138
]
139+
disallow_untyped_defs = true
140+
# ignore_errors = false
141+
# ignore_missing_imports = false
142+
# disallow_untyped_calls = true
143+
disallow_incomplete_defs = true
144+
# check_untyped_defs = true
145+
# disallow_untyped_decorators = true
146+
warn_redundant_casts = true
147+
148+
[[tool.mypy.overrides]]
149+
module = [
150+
"IPython.utils.text",
151+
]
152+
disallow_untyped_defs = false
153+
check_untyped_defs = false
154+
disallow_untyped_decorators = false
155+
156+
157+
# gloabl ignore error
158+
[[tool.mypy.overrides]]
159+
module = [
160+
"IPython",
161+
"IPython.conftest",
162+
"IPython.core.alias",
163+
"IPython.core.async_helpers",
164+
"IPython.core.autocall",
165+
"IPython.core.builtin_trap",
166+
"IPython.core.compilerop",
167+
"IPython.core.completer",
168+
"IPython.core.completerlib",
169+
"IPython.core.crashhandler",
170+
"IPython.core.debugger",
171+
"IPython.core.display",
172+
"IPython.core.display_functions",
173+
"IPython.core.display_trap",
174+
"IPython.core.displayhook",
175+
"IPython.core.displaypub",
176+
"IPython.core.events",
177+
"IPython.core.excolors",
178+
"IPython.core.extensions",
179+
"IPython.core.formatters",
180+
"IPython.core.getipython",
181+
"IPython.core.guarded_eval",
182+
"IPython.core.history",
183+
"IPython.core.historyapp",
184+
"IPython.core.hooks",
185+
"IPython.core.inputsplitter",
186+
"IPython.core.inputtransformer",
187+
"IPython.core.inputtransformer2",
188+
"IPython.core.interactiveshell",
189+
"IPython.core.logger",
190+
"IPython.core.macro",
191+
"IPython.core.magic",
192+
"IPython.core.magic_arguments",
193+
"IPython.core.magics.ast_mod",
194+
"IPython.core.magics.auto",
195+
"IPython.core.magics.basic",
196+
"IPython.core.magics.code",
197+
"IPython.core.magics.config",
198+
"IPython.core.magics.display",
199+
"IPython.core.magics.execution",
200+
"IPython.core.magics.extension",
201+
"IPython.core.magics.history",
202+
"IPython.core.magics.logging",
203+
"IPython.core.magics.namespace",
204+
"IPython.core.magics.osm",
205+
"IPython.core.magics.packaging",
206+
"IPython.core.magics.pylab",
207+
"IPython.core.magics.script",
208+
"IPython.core.oinspect",
209+
"IPython.core.page",
210+
"IPython.core.payload",
211+
"IPython.core.payloadpage",
212+
"IPython.core.prefilter",
213+
"IPython.core.profiledir",
214+
"IPython.core.prompts",
215+
"IPython.core.pylabtools",
216+
"IPython.core.shellapp",
217+
"IPython.core.splitinput",
218+
"IPython.core.ultratb",
219+
"IPython.extensions.autoreload",
220+
"IPython.extensions.storemagic",
221+
"IPython.external.qt_for_kernel",
222+
"IPython.external.qt_loaders",
223+
"IPython.lib.backgroundjobs",
224+
"IPython.lib.clipboard",
225+
"IPython.lib.demo",
226+
"IPython.lib.display",
227+
"IPython.lib.editorhooks",
228+
"IPython.lib.guisupport",
229+
"IPython.lib.latextools",
230+
"IPython.lib.lexers",
231+
"IPython.lib.pretty",
232+
"IPython.paths",
233+
"IPython.sphinxext.ipython_console_highlighting",
234+
"IPython.terminal.debugger",
235+
"IPython.terminal.embed",
236+
"IPython.terminal.interactiveshell",
237+
"IPython.terminal.magics",
238+
"IPython.terminal.prompts",
239+
"IPython.terminal.pt_inputhooks",
240+
"IPython.terminal.pt_inputhooks.asyncio",
241+
"IPython.terminal.pt_inputhooks.glut",
242+
"IPython.terminal.pt_inputhooks.gtk",
243+
"IPython.terminal.pt_inputhooks.gtk3",
244+
"IPython.terminal.pt_inputhooks.gtk4",
245+
"IPython.terminal.pt_inputhooks.osx",
246+
"IPython.terminal.pt_inputhooks.pyglet",
247+
"IPython.terminal.pt_inputhooks.qt",
248+
"IPython.terminal.pt_inputhooks.tk",
249+
"IPython.terminal.pt_inputhooks.wx",
250+
"IPython.terminal.ptutils",
251+
"IPython.terminal.shortcuts",
252+
"IPython.terminal.shortcuts.auto_match",
253+
"IPython.terminal.shortcuts.auto_suggest",
254+
"IPython.terminal.shortcuts.filters",
255+
"IPython.utils._process_cli",
256+
"IPython.utils._process_common",
257+
"IPython.utils._process_emscripten",
258+
"IPython.utils._process_posix",
259+
"IPython.utils.capture",
260+
"IPython.utils.coloransi",
261+
"IPython.utils.contexts",
262+
"IPython.utils.data",
263+
"IPython.utils.decorators",
264+
"IPython.utils.dir2",
265+
"IPython.utils.encoding",
266+
"IPython.utils.frame",
267+
"IPython.utils.generics",
268+
"IPython.utils.importstring",
269+
"IPython.utils.io",
270+
"IPython.utils.ipstruct",
271+
"IPython.utils.module_paths",
272+
"IPython.utils.openpy",
273+
"IPython.utils.process",
274+
"IPython.utils.py3compat",
275+
"IPython.utils.sentinel",
276+
"IPython.utils.shimmodule",
277+
"IPython.utils.strdispatch",
278+
"IPython.utils.sysinfo",
279+
"IPython.utils.syspathcontext",
280+
"IPython.utils.tempdir",
281+
"IPython.utils.terminal",
282+
"IPython.utils.timing",
283+
"IPython.utils.tokenutil",
284+
"IPython.utils.tz",
285+
"IPython.utils.ulinecache",
286+
"IPython.utils.version",
287+
"IPython.utils.wildcard",
288+
289+
]
290+
disallow_untyped_defs = false
291+
ignore_errors = true
292+
ignore_missing_imports = true
293+
disallow_untyped_calls = false
294+
disallow_incomplete_defs = false
295+
check_untyped_defs = false
296+
disallow_untyped_decorators = false
139297

140298
[tool.pytest.ini_options]
141299
addopts = [

0 commit comments

Comments
 (0)