diff --git a/IPython/utils/text.py b/IPython/utils/text.py index a86177e41a..5876a05fb6 100644 --- a/IPython/utils/text.py +++ b/IPython/utils/text.py @@ -30,6 +30,7 @@ Iterator, TypeVar, ) +import builtins if sys.version_info < (3, 12): from typing_extensions import Self @@ -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: @@ -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: @@ -117,7 +124,7 @@ class SList(list): __spstr: str __nlstr: str - __paths: List[Path] + __paths: list[Path] def get_list(self) -> Self: return self @@ -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: @@ -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. @@ -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()) @@ -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. @@ -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 @@ -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 ): @@ -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) @@ -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))) @@ -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 @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 0e58d321c2..312d79deef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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`