diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2023-11-14 19:18:07 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2023-11-14 20:20:53 +0300 |
commit | 874ef51d3d3edfa25f5a505ec6ab50e172965d1e (patch) | |
tree | 620fb5e02063d23509d3aa3df2215c099ccde0b7 /contrib/python/ipython/py3/IPython | |
parent | e356b34d3b0399e2f170881af15c91e4db9e3d11 (diff) | |
download | ydb-874ef51d3d3edfa25f5a505ec6ab50e172965d1e.tar.gz |
Intermediate changes
Diffstat (limited to 'contrib/python/ipython/py3/IPython')
18 files changed, 256 insertions, 117 deletions
diff --git a/contrib/python/ipython/py3/IPython/__main__.py b/contrib/python/ipython/py3/IPython/__main__.py index 8e9f989a82..3b4605601a 100644 --- a/contrib/python/ipython/py3/IPython/__main__.py +++ b/contrib/python/ipython/py3/IPython/__main__.py @@ -2,13 +2,13 @@ # encoding: utf-8 """Terminal-based IPython entry point. """ -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- # Copyright (c) 2012, IPython Development Team. # # Distributed under the terms of the Modified BSD License. # # The full license is in the file COPYING.txt, distributed with this software. -#----------------------------------------------------------------------------- +# ----------------------------------------------------------------------------- from IPython import start_ipython diff --git a/contrib/python/ipython/py3/IPython/core/completerlib.py b/contrib/python/ipython/py3/IPython/core/completerlib.py index 65efa42254..74252dcfad 100644 --- a/contrib/python/ipython/py3/IPython/core/completerlib.py +++ b/contrib/python/ipython/py3/IPython/core/completerlib.py @@ -238,6 +238,7 @@ def try_import(mod: str, only_modules=False) -> List[str]: completions.extend(m_all) if m_is_init: + file_ = m.__file__ completions.extend(arcadia_module_list(mod)) completions_set = {c for c in completions if isinstance(c, str)} completions_set.discard('__init__') diff --git a/contrib/python/ipython/py3/IPython/core/display.py b/contrib/python/ipython/py3/IPython/core/display.py index ffa6e185c4..20e2e34b8f 100644 --- a/contrib/python/ipython/py3/IPython/core/display.py +++ b/contrib/python/ipython/py3/IPython/core/display.py @@ -286,7 +286,7 @@ class DisplayObject(object): in the frontend. The MIME type of the data should match the subclasses used, so the Png subclass should be used for 'image/png' data. If the data is a URL, the data will first be downloaded - and then displayed. If + and then displayed. Parameters ---------- diff --git a/contrib/python/ipython/py3/IPython/core/events.py b/contrib/python/ipython/py3/IPython/core/events.py index 3a66e75e5a..90ff8f746d 100644 --- a/contrib/python/ipython/py3/IPython/core/events.py +++ b/contrib/python/ipython/py3/IPython/core/events.py @@ -13,8 +13,6 @@ events and the arguments which will be passed to them. This API is experimental in IPython 2.0, and may be revised in future versions. """ -from backcall import callback_prototype - class EventManager(object): """Manage a collection of events and a sequence of callbacks for each. @@ -63,23 +61,14 @@ class EventManager(object): """ if not callable(function): raise TypeError('Need a callable, got %r' % function) - callback_proto = available_events.get(event) if function not in self.callbacks[event]: - self.callbacks[event].append(callback_proto.adapt(function)) + self.callbacks[event].append(function) def unregister(self, event, function): """Remove a callback from the given event.""" if function in self.callbacks[event]: return self.callbacks[event].remove(function) - # Remove callback in case ``function`` was adapted by `backcall`. - for callback in self.callbacks[event]: - try: - if callback.__wrapped__ is function: - return self.callbacks[event].remove(callback) - except AttributeError: - pass - raise ValueError('Function {!r} is not registered as a {} callback'.format(function, event)) def trigger(self, event, *args, **kwargs): @@ -100,9 +89,8 @@ class EventManager(object): available_events = {} def _define_event(callback_function): - callback_proto = callback_prototype(callback_function) - available_events[callback_function.__name__] = callback_proto - return callback_proto + available_events[callback_function.__name__] = callback_function + return callback_function # ------------------------------------------------------------------------------ # Callback prototypes diff --git a/contrib/python/ipython/py3/IPython/core/guarded_eval.py b/contrib/python/ipython/py3/IPython/core/guarded_eval.py index 5d405b2208..a304affc35 100644 --- a/contrib/python/ipython/py3/IPython/core/guarded_eval.py +++ b/contrib/python/ipython/py3/IPython/core/guarded_eval.py @@ -1,3 +1,4 @@ +from inspect import signature, Signature from typing import ( Any, Callable, @@ -335,6 +336,7 @@ class _IdentitySubscript: IDENTITY_SUBSCRIPT = _IdentitySubscript() SUBSCRIPT_MARKER = "__SUBSCRIPT_SENTINEL__" +UNKNOWN_SIGNATURE = Signature() class GuardRejection(Exception): @@ -415,6 +417,10 @@ UNARY_OP_DUNDERS: Dict[Type[ast.unaryop], Tuple[str, ...]] = { } +class Duck: + """A dummy class used to create objects of other classes without calling their ``__init__``""" + + def _find_dunder(node_op, dunders) -> Union[Tuple[str, ...], None]: dunder = None for op, candidate_dunder in dunders.items(): @@ -584,6 +590,27 @@ def eval_node(node: Union[ast.AST, None], context: EvaluationContext): if policy.can_call(func) and not node.keywords: args = [eval_node(arg, context) for arg in node.args] return func(*args) + try: + sig = signature(func) + except ValueError: + sig = UNKNOWN_SIGNATURE + # if annotation was not stringized, or it was stringized + # but resolved by signature call we know the return type + not_empty = sig.return_annotation is not Signature.empty + not_stringized = not isinstance(sig.return_annotation, str) + if not_empty and not_stringized: + duck = Duck() + # if allow-listed builtin is on type annotation, instantiate it + if policy.can_call(sig.return_annotation) and not node.keywords: + args = [eval_node(arg, context) for arg in node.args] + return sig.return_annotation(*args) + try: + # if custom class is in type annotation, mock it; + # this only works for heap types, not builtins + duck.__class__ = sig.return_annotation + return duck + except TypeError: + pass raise GuardRejection( "Call for", func, # not joined to avoid calling `repr` diff --git a/contrib/python/ipython/py3/IPython/core/inputsplitter.py b/contrib/python/ipython/py3/IPython/core/inputsplitter.py index a4401184bd..33ed563221 100644 --- a/contrib/python/ipython/py3/IPython/core/inputsplitter.py +++ b/contrib/python/ipython/py3/IPython/core/inputsplitter.py @@ -31,7 +31,8 @@ import sys import tokenize import warnings -from typing import List +from typing import List, Tuple, Union, Optional +from types import CodeType from IPython.core.inputtransformer import (leading_indent, classic_prompt, @@ -91,7 +92,13 @@ def num_ini_spaces(s): ------- n : int """ - + warnings.warn( + "`num_ini_spaces` is Pending Deprecation since IPython 8.17." + "It is considered fro removal in in future version. " + "Please open an issue if you believe it should be kept.", + stacklevel=2, + category=PendingDeprecationWarning, + ) ini_spaces = ini_spaces_re.match(s) if ini_spaces: return ini_spaces.end() @@ -144,7 +151,7 @@ def partial_tokens(s): else: raise -def find_next_indent(code): +def find_next_indent(code) -> int: """Find the number of spaces for the next line of indentation""" tokens = list(partial_tokens(code)) if tokens[-1].type == tokenize.ENDMARKER: @@ -318,7 +325,7 @@ class InputSplitter(object): # If self.source matches the first value, the second value is a valid # current indentation. Otherwise, the cache is invalid and the indentation # must be recalculated. - _indent_spaces_cache = None, None + _indent_spaces_cache: Union[Tuple[None, None], Tuple[str, int]] = None, None # String, indicating the default input encoding. It is computed by default # at initialization time via get_input_encoding(), but it can be reset by a # client with specific knowledge of the encoding. @@ -326,11 +333,11 @@ class InputSplitter(object): # String where the current full source input is stored, properly encoded. # Reading this attribute is the normal way of querying the currently pushed # source code, that has been properly encoded. - source = '' + source: str = "" # Code object corresponding to the current source. It is automatically # synced to the source, so it can be queried at any time to obtain the code # object; it will be None if the source doesn't compile to valid Python. - code = None + code: Optional[CodeType] = None # Private attributes @@ -339,9 +346,9 @@ class InputSplitter(object): # Command compiler _compile: codeop.CommandCompiler # Boolean indicating whether the current block is complete - _is_complete = None + _is_complete: Optional[bool] = None # Boolean indicating whether the current block has an unrecoverable syntax error - _is_invalid = False + _is_invalid: bool = False def __init__(self) -> None: """Create a new InputSplitter instance.""" @@ -511,9 +518,10 @@ class InputSplitter(object): # General fallback - accept more code return True - def get_indent_spaces(self): + def get_indent_spaces(self) -> int: sourcefor, n = self._indent_spaces_cache if sourcefor == self.source: + assert n is not None return n # self.source always has a trailing newline @@ -562,7 +570,7 @@ class IPythonInputSplitter(InputSplitter): # Private attributes # List with lines of raw input accumulated so far. - _buffer_raw = None + _buffer_raw: List[str] def __init__(self, line_input_checker=True, physical_line_transforms=None, logical_line_transforms=None, python_line_transforms=None): @@ -652,8 +660,8 @@ class IPythonInputSplitter(InputSplitter): tmp = transform.reset() if tmp is not None: yield tmp - - out = [] + + out: List[str] = [] for t in self.transforms_in_use: out = _flush(t, out) diff --git a/contrib/python/ipython/py3/IPython/core/inputtransformer.py b/contrib/python/ipython/py3/IPython/core/inputtransformer.py index 81cd1fa08c..bb1061e8dc 100644 --- a/contrib/python/ipython/py3/IPython/core/inputtransformer.py +++ b/contrib/python/ipython/py3/IPython/core/inputtransformer.py @@ -71,8 +71,8 @@ class InputTransformer(metaclass=abc.ABCMeta): """ @functools.wraps(func) def transformer_factory(**kwargs): - return cls(func, **kwargs) - + return cls(func, **kwargs) # type: ignore [call-arg] + return transformer_factory class StatelessInputTransformer(InputTransformer): @@ -194,7 +194,7 @@ def assemble_logical_lines(): line = ''.join(parts) # Utilities -def _make_help_call(target, esc, lspace): +def _make_help_call(target: str, esc: str, lspace: str) -> str: """Prepares a pinfo(2)/psearch call from a target name and the escape (i.e. ? or ??)""" method = 'pinfo2' if esc == '??' \ @@ -212,17 +212,19 @@ def _make_help_call(target, esc, lspace): # These define the transformations for the different escape characters. -def _tr_system(line_info): +def _tr_system(line_info: LineInfo): "Translate lines escaped with: !" cmd = line_info.line.lstrip().lstrip(ESC_SHELL) return '%sget_ipython().system(%r)' % (line_info.pre, cmd) -def _tr_system2(line_info): + +def _tr_system2(line_info: LineInfo): "Translate lines escaped with: !!" cmd = line_info.line.lstrip()[2:] return '%sget_ipython().getoutput(%r)' % (line_info.pre, cmd) -def _tr_help(line_info): + +def _tr_help(line_info: LineInfo): "Translate lines escaped with: ?/??" # A naked help line should just fire the intro help screen if not line_info.line[1:]: @@ -230,7 +232,8 @@ def _tr_help(line_info): return _make_help_call(line_info.ifun, line_info.esc, line_info.pre) -def _tr_magic(line_info): + +def _tr_magic(line_info: LineInfo): "Translate lines escaped with: %" tpl = '%sget_ipython().run_line_magic(%r, %r)' if line_info.line.startswith(ESC_MAGIC2): @@ -241,17 +244,20 @@ def _tr_magic(line_info): t_magic_name = t_magic_name.lstrip(ESC_MAGIC) return tpl % (line_info.pre, t_magic_name, t_magic_arg_s) -def _tr_quote(line_info): + +def _tr_quote(line_info: LineInfo): "Translate lines escaped with: ," return '%s%s("%s")' % (line_info.pre, line_info.ifun, '", "'.join(line_info.the_rest.split()) ) -def _tr_quote2(line_info): + +def _tr_quote2(line_info: LineInfo): "Translate lines escaped with: ;" return '%s%s("%s")' % (line_info.pre, line_info.ifun, line_info.the_rest) -def _tr_paren(line_info): + +def _tr_paren(line_info: LineInfo): "Translate lines escaped with: /" return '%s%s(%s)' % (line_info.pre, line_info.ifun, ", ".join(line_info.the_rest.split())) @@ -266,9 +272,8 @@ tr = { ESC_SHELL : _tr_system, ESC_PAREN : _tr_paren } @StatelessInputTransformer.wrap -def escaped_commands(line): - """Transform escaped commands - %magic, !system, ?help + various autocalls. - """ +def escaped_commands(line: str): + """Transform escaped commands - %magic, !system, ?help + various autocalls.""" if not line or line.isspace(): return line lineinf = LineInfo(line) @@ -342,20 +347,22 @@ def ends_in_comment_or_string(src): @StatelessInputTransformer.wrap -def help_end(line): +def help_end(line: str): """Translate lines with ?/?? at the end""" m = _help_end_re.search(line) if m is None or ends_in_comment_or_string(line): return line target = m.group(1) esc = m.group(3) - lspace = _initial_space_re.match(line).group(0) + match = _initial_space_re.match(line) + assert match is not None + lspace = match.group(0) return _make_help_call(target, esc, lspace) @CoroutineInputTransformer.wrap -def cellmagic(end_on_blank_line=False): +def cellmagic(end_on_blank_line: bool = False): """Captures & transforms cell magics. After a cell magic is started, this stores up any lines it gets until it is diff --git a/contrib/python/ipython/py3/IPython/core/interactiveshell.py b/contrib/python/ipython/py3/IPython/core/interactiveshell.py index 894403f98b..0a528c51f2 100644 --- a/contrib/python/ipython/py3/IPython/core/interactiveshell.py +++ b/contrib/python/ipython/py3/IPython/core/interactiveshell.py @@ -21,6 +21,7 @@ import inspect import os import re import runpy +import shutil import subprocess import sys import tempfile @@ -36,7 +37,28 @@ from typing import List as ListType, Dict as DictType, Any as AnyType from typing import Optional, Sequence, Tuple from warnings import warn -from pickleshare import PickleShareDB +try: + from pickleshare import PickleShareDB +except ModuleNotFoundError: + + class PickleShareDB: # type: ignore [no-redef] + def __init__(self, path): + pass + + def get(self, key, default): + warn( + f"using {key} requires you to install the `pickleshare` library.", + stacklevel=2, + ) + return default + + def __setitem__(self, key, value): + warn( + f"using {key} requires you to install the `pickleshare` library.", + stacklevel=2, + ) + + from tempfile import TemporaryDirectory from traitlets import ( Any, @@ -3902,7 +3924,7 @@ class InteractiveShell(SingletonConfigurable): del self.tempfiles for tdir in self.tempdirs: try: - tdir.rmdir() + shutil.rmtree(tdir) self.tempdirs.remove(tdir) except FileNotFoundError: pass diff --git a/contrib/python/ipython/py3/IPython/core/magics/packaging.py b/contrib/python/ipython/py3/IPython/core/magics/packaging.py index 2f7652c169..093b0a2ec1 100644 --- a/contrib/python/ipython/py3/IPython/core/magics/packaging.py +++ b/contrib/python/ipython/py3/IPython/core/magics/packaging.py @@ -8,6 +8,7 @@ # The full license is in the file COPYING.txt, distributed with this software. #----------------------------------------------------------------------------- +import functools import re import shlex import sys @@ -16,33 +17,49 @@ from pathlib import Path from IPython.core.magic import Magics, magics_class, line_magic -def _is_conda_environment(): - """Return True if the current Python executable is in a conda env""" - # TODO: does this need to change on windows? - return Path(sys.prefix, "conda-meta", "history").exists() +def is_conda_environment(func): + @functools.wraps(func) + def wrapper(*args, **kwargs): + """Return True if the current Python executable is in a conda env""" + # TODO: does this need to change on windows? + if not Path(sys.prefix, "conda-meta", "history").exists(): + raise ValueError( + "The python kernel does not appear to be a conda environment. " + "Please use ``%pip install`` instead." + ) + return func(*args, **kwargs) + return wrapper -def _get_conda_executable(): - """Find the path to the conda executable""" + +def _get_conda_like_executable(command): + """Find the path to the given executable + + Parameters + ---------- + + executable: string + Value should be: conda, mamba or micromamba + """ # Check if there is a conda executable in the same directory as the Python executable. # This is the case within conda's root environment. - conda = Path(sys.executable).parent / "conda" - if conda.is_file(): - return str(conda) + executable = Path(sys.executable).parent / command + if executable.is_file(): + return str(executable) # Otherwise, attempt to extract the executable from conda history. # This applies in any conda environment. history = Path(sys.prefix, "conda-meta", "history").read_text(encoding="utf-8") match = re.search( - r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]", + rf"^#\s*cmd:\s*(?P<command>.*{executable})\s[create|install]", history, flags=re.MULTILINE, ) if match: return match.groupdict()["command"] - # Fallback: assume conda is available on the system path. - return "conda" + # Fallback: assume the executable is available on the system path. + return command CONDA_COMMANDS_REQUIRING_PREFIX = { @@ -76,18 +93,7 @@ class PackagingMagics(Magics): print("Note: you may need to restart the kernel to use updated packages.") - @line_magic - def conda(self, line): - """Run the conda package manager within the current kernel. - - Usage: - %conda install [pkgs] - """ - if not _is_conda_environment(): - raise ValueError("The python kernel does not appear to be a conda environment. " - "Please use ``%pip install`` instead.") - - conda = _get_conda_executable() + def _run_command(self, cmd, line): args = shlex.split(line) command = args[0] if len(args) > 0 else "" args = args[1:] if len(args) > 1 else [""] @@ -108,5 +114,38 @@ class PackagingMagics(Magics): if needs_prefix and not has_prefix: extra_args.extend(["--prefix", sys.prefix]) - self.shell.system(' '.join([conda, command] + extra_args + args)) + self.shell.system(" ".join([cmd, command] + extra_args + args)) print("\nNote: you may need to restart the kernel to use updated packages.") + + @line_magic + @is_conda_environment + def conda(self, line): + """Run the conda package manager within the current kernel. + + Usage: + %conda install [pkgs] + """ + conda = _get_conda_like_executable("conda") + self._run_command(conda, line) + + @line_magic + @is_conda_environment + def mamba(self, line): + """Run the mamba package manager within the current kernel. + + Usage: + %mamba install [pkgs] + """ + mamba = _get_conda_like_executable("mamba") + self._run_command(mamba, line) + + @line_magic + @is_conda_environment + def micromamba(self, line): + """Run the conda package manager within the current kernel. + + Usage: + %micromamba install [pkgs] + """ + micromamba = _get_conda_like_executable("micromamba") + self._run_command(micromamba, line) diff --git a/contrib/python/ipython/py3/IPython/core/oinspect.py b/contrib/python/ipython/py3/IPython/core/oinspect.py index ef6a0d02d7..d77a732267 100644 --- a/contrib/python/ipython/py3/IPython/core/oinspect.py +++ b/contrib/python/ipython/py3/IPython/core/oinspect.py @@ -416,6 +416,8 @@ class Inspector(Colorable): If any exception is generated, None is returned instead and the exception is suppressed.""" + if not callable(obj): + return None try: return _render_signature(signature(obj), oname) except: diff --git a/contrib/python/ipython/py3/IPython/core/release.py b/contrib/python/ipython/py3/IPython/core/release.py index 152ebcb87b..ee0042d081 100644 --- a/contrib/python/ipython/py3/IPython/core/release.py +++ b/contrib/python/ipython/py3/IPython/core/release.py @@ -16,7 +16,7 @@ # release. 'dev' as a _version_extra string means this is a development # version _version_major = 8 -_version_minor = 16 +_version_minor = 17 _version_patch = 1 _version_extra = ".dev" # _version_extra = "rc1" diff --git a/contrib/python/ipython/py3/IPython/lib/pretty.py b/contrib/python/ipython/py3/IPython/lib/pretty.py index 3486450786..2575c3b0a9 100644 --- a/contrib/python/ipython/py3/IPython/lib/pretty.py +++ b/contrib/python/ipython/py3/IPython/lib/pretty.py @@ -841,17 +841,8 @@ _env_type = type(os.environ) if _env_type is not dict: _type_pprinters[_env_type] = _dict_pprinter_factory('environ{', '}') -try: - # In PyPy, types.DictProxyType is dict, setting the dictproxy printer - # using dict.setdefault avoids overwriting the dict printer - _type_pprinters.setdefault(types.DictProxyType, - _dict_pprinter_factory('dict_proxy({', '})')) - _type_pprinters[types.ClassType] = _type_pprint - _type_pprinters[types.SliceType] = _repr_pprint -except AttributeError: # Python 3 - _type_pprinters[types.MappingProxyType] = \ - _dict_pprinter_factory('mappingproxy({', '})') - _type_pprinters[slice] = _repr_pprint +_type_pprinters[types.MappingProxyType] = _dict_pprinter_factory("mappingproxy({", "})") +_type_pprinters[slice] = _repr_pprint _type_pprinters[range] = _repr_pprint _type_pprinters[bytes] = _repr_pprint diff --git a/contrib/python/ipython/py3/IPython/utils/_sysinfo.py b/contrib/python/ipython/py3/IPython/utils/_sysinfo.py index 5e2eeadcf3..8d771a843f 100644 --- a/contrib/python/ipython/py3/IPython/utils/_sysinfo.py +++ b/contrib/python/ipython/py3/IPython/utils/_sysinfo.py @@ -1,2 +1,2 @@ # GENERATED BY setup.py -commit = "a2af83ddf" +commit = "0cfced36e" diff --git a/contrib/python/ipython/py3/IPython/utils/py3compat.py b/contrib/python/ipython/py3/IPython/utils/py3compat.py index 34af4c58f4..66ef337248 100644 --- a/contrib/python/ipython/py3/IPython/utils/py3compat.py +++ b/contrib/python/ipython/py3/IPython/utils/py3compat.py @@ -57,6 +57,7 @@ def execfile(fname, glob, loc=None, compiler=None): PYPY = platform.python_implementation() == "PyPy" + # Cython still rely on that as a Dec 28 2019 # See https://github.com/cython/cython/pull/3291 and # https://github.com/ipython/ipython/issues/12068 diff --git a/contrib/python/ipython/py3/IPython/utils/sysinfo.py b/contrib/python/ipython/py3/IPython/utils/sysinfo.py index 857f0cf2d8..347123fd53 100644 --- a/contrib/python/ipython/py3/IPython/utils/sysinfo.py +++ b/contrib/python/ipython/py3/IPython/utils/sysinfo.py @@ -20,6 +20,8 @@ import pprint import sys import subprocess +from pathlib import Path + from IPython.core import release from IPython.utils import _sysinfo, encoding @@ -27,7 +29,7 @@ from IPython.utils import _sysinfo, encoding # Code #----------------------------------------------------------------------------- -def pkg_commit_hash(pkg_path): +def pkg_commit_hash(pkg_path: str) -> tuple[str, str]: """Get short form of commit hash given directory `pkg_path` We get the commit hash from (in order of preference): @@ -65,7 +67,7 @@ def pkg_commit_hash(pkg_path): return '(none found)', '<not found>' -def pkg_info(pkg_path): +def pkg_info(pkg_path: str) -> dict: """Return dict describing the context of this package Parameters @@ -92,11 +94,10 @@ def pkg_info(pkg_path): default_encoding=encoding.DEFAULT_ENCODING, ) -def get_sys_info(): +def get_sys_info() -> dict: """Return useful information about IPython and the system, as a dict.""" - p = os.path - path = p.realpath(p.dirname(p.abspath(p.join(__file__, '..')))) - return pkg_info(path) + path = Path(__file__, "..").resolve().parent + return pkg_info(str(path)) def sys_info(): """Return useful information about IPython and the system, as a string. diff --git a/contrib/python/ipython/py3/IPython/utils/text.py b/contrib/python/ipython/py3/IPython/utils/text.py index 74bccddf68..9b653dcee0 100644 --- a/contrib/python/ipython/py3/IPython/utils/text.py +++ b/contrib/python/ipython/py3/IPython/utils/text.py @@ -13,15 +13,12 @@ import re import string import sys import textwrap +import warnings from string import Formatter from pathlib import Path +from typing import List, Union, Optional, Dict, Tuple -# datetime.strftime date format for ipython -if sys.platform == 'win32': - date_format = "%B %d, %Y" -else: - date_format = "%B %-d, %Y" class LSString(str): """String derivative with a special access attributes. @@ -336,7 +333,13 @@ ini_spaces_re = re.compile(r'^(\s+)') def num_ini_spaces(strng): """Return the number of initial spaces in a string""" - + warnings.warn( + "`num_ini_spaces` is Pending Deprecation since IPython 8.17." + "It is considered fro removal in in future version. " + "Please open an issue if you believe it should be kept.", + stacklevel=2, + category=PendingDeprecationWarning, + ) ini_spaces = ini_spaces_re.match(strng) if ini_spaces: return ini_spaces.end() @@ -391,6 +394,13 @@ def wrap_paragraphs(text, ncols=80): ------- list of complete paragraphs, wrapped to fill `ncols` columns. """ + warnings.warn( + "`wrap_paragraphs` is Pending Deprecation since IPython 8.17." + "It is considered fro removal in in future version. " + "Please open an issue if you believe it should be kept.", + stacklevel=2, + category=PendingDeprecationWarning, + ) paragraph_re = re.compile(r'\n(\s*\n)+', re.MULTILINE) text = dedent(text).strip() paragraphs = paragraph_re.split(text)[::2] # every other entry is space @@ -465,6 +475,14 @@ def strip_ansi(source): source : str Source to remove the ansi from """ + warnings.warn( + "`strip_ansi` is Pending Deprecation since IPython 8.17." + "It is considered fro removal in in future version. " + "Please open an issue if you believe it should be kept.", + stacklevel=2, + category=PendingDeprecationWarning, + ) + return re.sub(r'\033\[(\d|;)+?m', '', source) @@ -611,7 +629,7 @@ def _col_chunks(l, max_rows, row_first=False): yield l[i:(i + max_rows)] -def _find_optimal(rlist, row_first=False, separator_size=2, displaywidth=80): +def _find_optimal(rlist, row_first: bool, separator_size: int, displaywidth: int): """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))) @@ -634,7 +652,9 @@ def _get_or_default(mylist, i, default=None): return mylist[i] -def compute_item_matrix(items, row_first=False, empty=None, *args, **kwargs) : +def compute_item_matrix( + items, row_first: bool = False, empty=None, *, separator_size=2, displaywidth=80 +) -> Tuple[List[List[int]], Dict[str, int]]: """Returns a nested list, and info to columnize items Parameters @@ -682,8 +702,20 @@ def compute_item_matrix(items, row_first=False, empty=None, *args, **kwargs) : In [5]: all((info[k] == ideal[k] for k in ideal.keys())) Out[5]: True """ - info = _find_optimal(list(map(len, items)), row_first, *args, **kwargs) - nrow, ncol = info['max_rows'], info['num_columns'] + warnings.warn( + "`compute_item_matrix` is Pending Deprecation since IPython 8.17." + "It is considered fro removal in in future version. " + "Please open an issue if you believe it should be kept.", + stacklevel=2, + category=PendingDeprecationWarning, + ) + info = _find_optimal( + list(map(len, items)), + row_first, + separator_size=separator_size, + displaywidth=displaywidth, + ) + nrow, ncol = info["max_rows"], info["num_columns"] if row_first: return ([[_get_or_default(items, r * ncol + c, default=empty) for c in range(ncol)] for r in range(nrow)], info) else: @@ -709,14 +741,29 @@ def columnize(items, row_first=False, separator=" ", displaywidth=80, spread=Fa ------- The formatted string. """ + warnings.warn( + "`columnize` is Pending Deprecation since IPython 8.17." + "It is considered fro removal in in future version. " + "Please open an issue if you believe it should be kept.", + stacklevel=2, + category=PendingDeprecationWarning, + ) if not items: - return '\n' - matrix, info = compute_item_matrix(items, row_first=row_first, separator_size=len(separator), displaywidth=displaywidth) + return "\n" + matrix: List[List[int]] + matrix, info = compute_item_matrix( + items, + row_first=row_first, + separator_size=len(separator), + displaywidth=displaywidth, + ) if spread: - separator = separator.ljust(int(info['optimal_separator_width'])) - fmatrix = [filter(None, x) for x in matrix] - sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['column_widths'])]) - return '\n'.join(map(sjoin, fmatrix))+'\n' + separator = separator.ljust(int(info["optimal_separator_width"])) + fmatrix: List[filter[int]] = [filter(None, x) for x in matrix] + sjoin = lambda x: separator.join( + [y.ljust(w, " ") for y, w in zip(x, info["column_widths"])] + ) + return "\n".join(map(sjoin, fmatrix)) + "\n" def get_text_list(list_, last_sep=' and ', sep=", ", wrap_item_with=""): diff --git a/contrib/python/ipython/py3/IPython/utils/timing.py b/contrib/python/ipython/py3/IPython/utils/timing.py index 3a181ae728..d87e5fa8ef 100644 --- a/contrib/python/ipython/py3/IPython/utils/timing.py +++ b/contrib/python/ipython/py3/IPython/utils/timing.py @@ -23,8 +23,8 @@ import time # If possible (Unix), use the resource module instead of time.clock() try: import resource -except ImportError: - resource = None +except ModuleNotFoundError: + resource = None # type: ignore [assignment] # Some implementations (like jyputerlite) don't have getrusage if resource is not None and hasattr(resource, "getrusage"): diff --git a/contrib/python/ipython/py3/IPython/utils/tokenutil.py b/contrib/python/ipython/py3/IPython/utils/tokenutil.py index 5fd8a1fbe1..6b99a58ae2 100644 --- a/contrib/python/ipython/py3/IPython/utils/tokenutil.py +++ b/contrib/python/ipython/py3/IPython/utils/tokenutil.py @@ -8,12 +8,14 @@ from io import StringIO from keyword import iskeyword import tokenize +from tokenize import TokenInfo +from typing import List, Optional Token = namedtuple('Token', ['token', 'text', 'start', 'end', 'line']) def generate_tokens(readline): - """wrap generate_tokens to catch EOF errors""" + """wrap generate_tkens to catch EOF errors""" try: for token in tokenize.generate_tokens(readline): yield token @@ -22,7 +24,9 @@ def generate_tokens(readline): return -def generate_tokens_catch_errors(readline, extra_errors_to_catch=None): +def generate_tokens_catch_errors( + readline, extra_errors_to_catch: Optional[List[str]] = None +): default_errors_to_catch = [ "unterminated string literal", "invalid non-printable character", @@ -31,7 +35,7 @@ def generate_tokens_catch_errors(readline, extra_errors_to_catch=None): assert extra_errors_to_catch is None or isinstance(extra_errors_to_catch, list) errors_to_catch = default_errors_to_catch + (extra_errors_to_catch or []) - tokens = [] + tokens: List[TokenInfo] = [] try: for token in tokenize.generate_tokens(readline): tokens.append(token) @@ -84,7 +88,8 @@ def line_at_cursor(cell, cursor_pos=0): line = "" return (line, offset) -def token_at_cursor(cell, cursor_pos=0): + +def token_at_cursor(cell: str, cursor_pos: int = 0): """Get the token at a given cursor Used for introspection. @@ -94,13 +99,13 @@ def token_at_cursor(cell, cursor_pos=0): Parameters ---------- - cell : unicode + cell : str A block of Python code cursor_pos : int The location of the cursor in the block where the token should be found """ - names = [] - tokens = [] + names: List[str] = [] + tokens: List[Token] = [] call_names = [] offsets = {1: 0} # lines start at 1 |