Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
43 changes: 25 additions & 18 deletions IPython/utils/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Iterator,
TypeVar,
)
import builtins

if sys.version_info < (3, 12):
from typing_extensions import Self
Expand All @@ -53,18 +54,24 @@ class LSString(str):
Such strings are very useful to efficiently interact with the shell, which
typically only understands whitespace-separated options for commands."""

__list: List[str]
__list: list[str]
__spstr: str
__paths: List[Path]
__paths: list[Path]

def get_list(self) -> List[str]:
def get_list(self) -> list[str]:
try:
return self.__list
except AttributeError:
self.__list = self.split('\n')
return self.__list

l = list = property(get_list)
@property
def l(self) -> list[str]: # noqa
return self.get_list()

@property
def list(self) -> list[str]:
return self.get_list()

def get_spstr(self) -> str:
try:
Expand All @@ -80,7 +87,7 @@ def get_nlstr(self) -> Self:

n = nlstr = property(get_nlstr)

def get_paths(self) -> List[Path]:
def get_paths(self) -> builtins.list[Path]:
try:
return self.__paths
except AttributeError:
Expand Down Expand Up @@ -117,7 +124,7 @@ class SList(list):

__spstr: str
__nlstr: str
__paths: List[Path]
__paths: list[Path]

def get_list(self) -> Self:
return self
Expand All @@ -142,7 +149,7 @@ def get_nlstr(self) -> str:

n = nlstr = property(get_nlstr)

def get_paths(self) -> List[Path]:
def get_paths(self) -> builtins.list[Path]:
try:
return self.__paths
except AttributeError:
Expand Down Expand Up @@ -191,7 +198,7 @@ def match_target(s: str) -> str:
else:
return type(self)([el for el in self if not pred(match_target(el))])

def fields(self, *fields: List[str]) -> List[List[str]]:
def fields(self, *fields: builtins.list[str]) -> builtins.list[builtins.list[str]]:
"""Collect whitespace-separated fields from string list

Allows quick awk-like usage of string lists.
Expand Down Expand Up @@ -229,7 +236,7 @@ def fields(self, *fields: List[str]) -> List[List[str]]:

def sort( # type:ignore[override]
self,
field: Optional[List[str]] = None,
field: Optional[builtins.list[str]] = None,
nums: bool = False,
) -> Self:
"""sort by specified fields (see fields())
Expand Down Expand Up @@ -296,7 +303,7 @@ def indent(instr: str, nspaces: int = 4, ntabs: int = 0, flatten: bool = False)
return outstr


def list_strings(arg: Union[str, List[str]]) -> List[str]:
def list_strings(arg: Union[str, list[str]]) -> list[str]:
"""Always return a list of strings, given a string or list of strings
as input.

Expand Down Expand Up @@ -454,7 +461,7 @@ class EvalFormatter(Formatter):
Out[3]: 'll'
"""

def get_field(self, name: str, args: Any, kwargs: Any) -> Tuple[Any, str]:
def get_field(self, name: str, args: Any, kwargs: Any) -> tuple[Any, str]:
v = eval(name, kwargs)
return v, name

Expand Down Expand Up @@ -546,7 +553,7 @@ class DollarFormatter(FullEvalFormatter):
r"(.*?)\$(\$?[\w\.]+)(?=([^']*'[^']*')*[^']*$)"
)

def parse(self, fmt_string: str) -> Iterator[Tuple[Any, Any, Any, Any]]: # type: ignore[explicit-override]
def parse(self, fmt_string: str) -> Iterator[tuple[Any, Any, Any, Any]]: # type: ignore[explicit-override]
for literal_txt, field_name, format_spec, conversion in Formatter.parse(
self, fmt_string
):
Expand Down Expand Up @@ -575,8 +582,8 @@ def __repr__(self) -> str:


def _col_chunks(
l: List[int], max_rows: int, row_first: bool = False
) -> Iterator[List[int]]:
l: list[int], max_rows: int, row_first: bool = False
) -> Iterator[list[int]]:
"""Yield successive max_rows-sized column chunks from l."""
if row_first:
ncols = (len(l) // max_rows) + (len(l) % max_rows > 0)
Expand All @@ -588,8 +595,8 @@ def _col_chunks(


def _find_optimal(
rlist: List[int], row_first: bool, separator_size: int, displaywidth: int
) -> Dict[str, Any]:
rlist: list[int], 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 @@ -607,7 +614,7 @@ def _find_optimal(
T = TypeVar("T")


def _get_or_default(mylist: List[T], i: int, default: T) -> T:
def _get_or_default(mylist: list[T], i: int, default: T) -> T:
"""return list item number, or default if don't exist"""
if i >= len(mylist):
return default
Expand All @@ -616,7 +623,7 @@ def _get_or_default(mylist: List[T], i: int, default: T) -> T:


def get_text_list(
list_: List[str], last_sep: str = " and ", sep: str = ", ", wrap_item_with: str = ""
list_: list[str], last_sep: str = " and ", sep: str = ", ", wrap_item_with: str = ""
) -> str:
"""
Return a string with a natural enumeration of items
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ extend-select = [
# "T20", # flake8-print
# "UP", # pyupgrade
"UP004", # 1 [*] Class `...` inherits from `object`
# "UP006", # 1 [*] Use `list` instead of `t.List` for type annotation
"UP006", # 1 [*] Use `list` instead of `t.List` for type annotation
# "UP006", # 1 [*] Use `tuple` instead of `t.Tuple` for type annotation
# "UP010", # 1 [*] Unnecessary `__future__` import `print_function` for target Python version
"UP011", # 1 [*] Unnecessary parentheses to `functools.lru_cache`
Expand Down
Loading