Skip to content

Commit

Permalink
stricter types (ipython#14447)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau authored May 31, 2024
2 parents dee457e + 9eb1d12 commit 2217c97
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 15 deletions.
53 changes: 38 additions & 15 deletions IPython/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
import os
import re
import string
import sys
import textwrap
import warnings
from string import Formatter
from pathlib import Path

from typing import List, Dict, Tuple, Optional, cast
from typing import List, Dict, Tuple, Optional, cast, Sequence, Mapping, Any

if sys.version_info < (3, 12):
from typing_extensions import Self
else:
from typing import Self


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

def get_list(self):
__list: List[str]
__spstr: str
__paths: List[Path]

def get_list(self) -> List[str]:
try:
return self.__list
except AttributeError:
Expand All @@ -43,7 +53,7 @@ def get_list(self):

l = list = property(get_list)

def get_spstr(self):
def get_spstr(self) -> str:
try:
return self.__spstr
except AttributeError:
Expand All @@ -52,12 +62,12 @@ def get_spstr(self):

s = spstr = property(get_spstr)

def get_nlstr(self):
def get_nlstr(self) -> Self:
return self

n = nlstr = property(get_nlstr)

def get_paths(self):
def get_paths(self) -> List[Path]:
try:
return self.__paths
except AttributeError:
Expand Down Expand Up @@ -92,12 +102,16 @@ class SList(list):
Any values which require transformations are computed only once and
cached."""

def get_list(self):
__spstr: str
__nlstr: str
__paths: List[Path]

def get_list(self) -> Self:
return self

l = list = property(get_list)

def get_spstr(self):
def get_spstr(self) -> str:
try:
return self.__spstr
except AttributeError:
Expand All @@ -106,7 +120,7 @@ def get_spstr(self):

s = spstr = property(get_spstr)

def get_nlstr(self):
def get_nlstr(self) -> str:
try:
return self.__nlstr
except AttributeError:
Expand All @@ -115,7 +129,7 @@ def get_nlstr(self):

n = nlstr = property(get_nlstr)

def get_paths(self):
def get_paths(self) -> List[Path]:
try:
return self.__paths
except AttributeError:
Expand Down Expand Up @@ -538,7 +552,9 @@ class FullEvalFormatter(Formatter):
"""
# copied from Formatter._vformat with minor changes to allow eval
# and replace the format_spec code with slicing
def vformat(self, format_string: str, args, kwargs) -> str:
def vformat(
self, format_string: str, args: Sequence[Any], kwargs: Mapping[str, Any]
) -> str:
result = []
conversion: Optional[str]
for literal_text, field_name, format_spec, conversion in self.parse(
Expand All @@ -559,7 +575,7 @@ def vformat(self, format_string: str, args, kwargs) -> str:

# eval the contents of the field for the object
# to be formatted
obj = eval(field_name, kwargs)
obj = eval(field_name, dict(kwargs))

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


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


def compute_item_matrix(
items, row_first: bool = False, empty=None, *, separator_size=2, displaywidth=80
items: List[str],
row_first: bool = False,
empty: Optional[str] = None,
*,
separator_size: int = 2,
displaywidth: int = 80,
) -> Tuple[List[List[int]], Dict[str, int]]:
"""Returns a nested list, and info to columnize items
Expand Down Expand Up @@ -710,7 +733,7 @@ def compute_item_matrix(
category=PendingDeprecationWarning,
)
info = _find_optimal(
list(map(len, items)),
list(map(len, items)), # type: ignore[arg-type]
row_first,
separator_size=separator_size,
displaywidth=displaywidth,
Expand All @@ -728,7 +751,7 @@ def columnize(
separator: str = " ",
displaywidth: int = 80,
spread: bool = False,
):
) -> str:
"""Transform a list of strings into a single string with columns.
Parameters
Expand Down
158 changes: 158 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,164 @@ exclude = [
'IPython/utils/_process_win32.py',
'IPython/utils/path.py',
]
disallow_untyped_defs = true
# ignore_errors = false
# ignore_missing_imports = false
# disallow_untyped_calls = true
disallow_incomplete_defs = true
# check_untyped_defs = true
# disallow_untyped_decorators = true
warn_redundant_casts = true

[[tool.mypy.overrides]]
module = [
"IPython.utils.text",
]
disallow_untyped_defs = false
check_untyped_defs = false
disallow_untyped_decorators = false


# gloabl ignore error
[[tool.mypy.overrides]]
module = [
"IPython",
"IPython.conftest",
"IPython.core.alias",
"IPython.core.async_helpers",
"IPython.core.autocall",
"IPython.core.builtin_trap",
"IPython.core.compilerop",
"IPython.core.completer",
"IPython.core.completerlib",
"IPython.core.crashhandler",
"IPython.core.debugger",
"IPython.core.display",
"IPython.core.display_functions",
"IPython.core.display_trap",
"IPython.core.displayhook",
"IPython.core.displaypub",
"IPython.core.events",
"IPython.core.excolors",
"IPython.core.extensions",
"IPython.core.formatters",
"IPython.core.getipython",
"IPython.core.guarded_eval",
"IPython.core.history",
"IPython.core.historyapp",
"IPython.core.hooks",
"IPython.core.inputsplitter",
"IPython.core.inputtransformer",
"IPython.core.inputtransformer2",
"IPython.core.interactiveshell",
"IPython.core.logger",
"IPython.core.macro",
"IPython.core.magic",
"IPython.core.magic_arguments",
"IPython.core.magics.ast_mod",
"IPython.core.magics.auto",
"IPython.core.magics.basic",
"IPython.core.magics.code",
"IPython.core.magics.config",
"IPython.core.magics.display",
"IPython.core.magics.execution",
"IPython.core.magics.extension",
"IPython.core.magics.history",
"IPython.core.magics.logging",
"IPython.core.magics.namespace",
"IPython.core.magics.osm",
"IPython.core.magics.packaging",
"IPython.core.magics.pylab",
"IPython.core.magics.script",
"IPython.core.oinspect",
"IPython.core.page",
"IPython.core.payload",
"IPython.core.payloadpage",
"IPython.core.prefilter",
"IPython.core.profiledir",
"IPython.core.prompts",
"IPython.core.pylabtools",
"IPython.core.shellapp",
"IPython.core.splitinput",
"IPython.core.ultratb",
"IPython.extensions.autoreload",
"IPython.extensions.storemagic",
"IPython.external.qt_for_kernel",
"IPython.external.qt_loaders",
"IPython.lib.backgroundjobs",
"IPython.lib.clipboard",
"IPython.lib.demo",
"IPython.lib.display",
"IPython.lib.editorhooks",
"IPython.lib.guisupport",
"IPython.lib.latextools",
"IPython.lib.lexers",
"IPython.lib.pretty",
"IPython.paths",
"IPython.sphinxext.ipython_console_highlighting",
"IPython.terminal.debugger",
"IPython.terminal.embed",
"IPython.terminal.interactiveshell",
"IPython.terminal.magics",
"IPython.terminal.prompts",
"IPython.terminal.pt_inputhooks",
"IPython.terminal.pt_inputhooks.asyncio",
"IPython.terminal.pt_inputhooks.glut",
"IPython.terminal.pt_inputhooks.gtk",
"IPython.terminal.pt_inputhooks.gtk3",
"IPython.terminal.pt_inputhooks.gtk4",
"IPython.terminal.pt_inputhooks.osx",
"IPython.terminal.pt_inputhooks.pyglet",
"IPython.terminal.pt_inputhooks.qt",
"IPython.terminal.pt_inputhooks.tk",
"IPython.terminal.pt_inputhooks.wx",
"IPython.terminal.ptutils",
"IPython.terminal.shortcuts",
"IPython.terminal.shortcuts.auto_match",
"IPython.terminal.shortcuts.auto_suggest",
"IPython.terminal.shortcuts.filters",
"IPython.utils._process_cli",
"IPython.utils._process_common",
"IPython.utils._process_emscripten",
"IPython.utils._process_posix",
"IPython.utils.capture",
"IPython.utils.coloransi",
"IPython.utils.contexts",
"IPython.utils.data",
"IPython.utils.decorators",
"IPython.utils.dir2",
"IPython.utils.encoding",
"IPython.utils.frame",
"IPython.utils.generics",
"IPython.utils.importstring",
"IPython.utils.io",
"IPython.utils.ipstruct",
"IPython.utils.module_paths",
"IPython.utils.openpy",
"IPython.utils.process",
"IPython.utils.py3compat",
"IPython.utils.sentinel",
"IPython.utils.shimmodule",
"IPython.utils.strdispatch",
"IPython.utils.sysinfo",
"IPython.utils.syspathcontext",
"IPython.utils.tempdir",
"IPython.utils.terminal",
"IPython.utils.timing",
"IPython.utils.tokenutil",
"IPython.utils.tz",
"IPython.utils.ulinecache",
"IPython.utils.version",
"IPython.utils.wildcard",

]
disallow_untyped_defs = false
ignore_errors = true
ignore_missing_imports = true
disallow_untyped_calls = false
disallow_incomplete_defs = false
check_untyped_defs = false
disallow_untyped_decorators = false

[tool.pytest.ini_options]
addopts = [
Expand Down

0 comments on commit 2217c97

Please sign in to comment.