From 14068d24a0a930eeccaa2151546d4a4087ed420a 2024-02-14 13:26:17 From: Matthias Bussonnier Date: 2024-02-14 13:26:17 Subject: [PATCH] even more types --- diff --git a/IPython/core/completerlib.py b/IPython/core/completerlib.py index 29ddab1..9b97d70 100644 --- a/IPython/core/completerlib.py +++ b/IPython/core/completerlib.py @@ -64,7 +64,8 @@ magic_run_re = re.compile(r'.*(\.ipy|\.ipynb|\.py[w]?)$') # Local utilities #----------------------------------------------------------------------------- -def module_list(path): + +def module_list(path: str) -> List[str]: """ Return the list containing the names of the modules available in the given folder. @@ -80,7 +81,7 @@ def module_list(path): # Build a list of all files in the directory and all files # in its subdirectories. For performance reasons, do not # recurse more than one level into subdirectories. - files = [] + files: List[str] = [] for root, dirs, nondirs in os.walk(path, followlinks=True): subdir = root[len(path)+1:] if subdir: @@ -91,8 +92,8 @@ def module_list(path): else: try: - files = list(zipimporter(path)._files.keys()) - except: + files = list(zipimporter(path)._files.keys()) # type: ignore + except Exception: files = [] # Build a list of modules which match the import_re regex. @@ -194,7 +195,9 @@ def try_import(mod: str, only_modules=False) -> List[str]: if m_is_init: file_ = m.__file__ - completions.extend(module_list(os.path.dirname(file_))) + file_path = os.path.dirname(file_) # type: ignore + if file_path is not None: + completions.extend(module_list(file_path)) completions_set = {c for c in completions if isinstance(c, str)} completions_set.discard('__init__') return list(completions_set) diff --git a/IPython/core/displaypub.py b/IPython/core/displaypub.py index 74028ec..ed6a708 100644 --- a/IPython/core/displaypub.py +++ b/IPython/core/displaypub.py @@ -24,7 +24,9 @@ from traitlets import List # This used to be defined here - it is imported for backwards compatibility from .display_functions import publish_display_data -#----------------------------------------------------------------------------- +import typing as t + +# ----------------------------------------------------------------------------- # Main payload class #----------------------------------------------------------------------------- @@ -103,9 +105,9 @@ class DisplayPublisher(Configurable): rather than creating a new output. """ - handlers = {} + handlers: t.Dict = {} if self.shell is not None: - handlers = getattr(self.shell, 'mime_renderers', {}) + handlers = getattr(self.shell, "mime_renderers", {}) for mime, handler in handlers.items(): if mime in data: @@ -125,11 +127,20 @@ class DisplayPublisher(Configurable): class CapturingDisplayPublisher(DisplayPublisher): """A DisplayPublisher that stores""" - outputs = List() - def publish(self, data, metadata=None, source=None, *, transient=None, update=False): - self.outputs.append({'data':data, 'metadata':metadata, - 'transient':transient, 'update':update}) + outputs: List = List() + + def publish( + self, data, metadata=None, source=None, *, transient=None, update=False + ): + self.outputs.append( + { + "data": data, + "metadata": metadata, + "transient": transient, + "update": update, + } + ) def clear_output(self, wait=False): super(CapturingDisplayPublisher, self).clear_output(wait) diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index 02e44d0..2d5ade8 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -31,8 +31,7 @@ import sys import tokenize import warnings -from typing import List, Tuple, Union, Optional -from typing_extensions import Self +from typing import List, Tuple, Union, Optional, TYPE_CHECKING from types import CodeType from IPython.core.inputtransformer import (leading_indent, @@ -53,6 +52,8 @@ from IPython.core.inputtransformer import (ESC_SHELL, ESC_SH_CAP, ESC_HELP, ESC_HELP2, ESC_MAGIC, ESC_MAGIC2, ESC_QUOTE, ESC_QUOTE2, ESC_PAREN, ESC_SEQUENCES) +if TYPE_CHECKING: + from typing_extensions import Self #----------------------------------------------------------------------------- # Utilities #----------------------------------------------------------------------------- diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 4f9e4e5..bbb1550 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -26,6 +26,8 @@ from ..utils.text import dedent from traitlets import Bool, Dict, Instance, observe from logging import error +import typing as t + #----------------------------------------------------------------------------- # Globals #----------------------------------------------------------------------------- @@ -36,7 +38,7 @@ from logging import error # access to the class when they run. See for more details: # http://stackoverflow.com/questions/2366713/can-a-python-decorator-of-an-instance-method-access-the-class -magics = dict(line={}, cell={}) +magics: t.Dict = dict(line={}, cell={}) magic_kinds = ('line', 'cell') magic_spec = ('line', 'cell', 'line_cell') diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 2575c3b..631445b 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- """ Python advanced pretty printer. This pretty printer is intended to replace the old `pprint` python module which does not allow developers @@ -108,6 +107,8 @@ from warnings import warn from IPython.utils.decorators import undoc from IPython.utils.py3compat import PYPY +from typing import Dict + __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter', 'for_type', 'for_type_by_name', 'RawText', 'RawStringLiteral', 'CallExpression'] @@ -807,6 +808,7 @@ def _exception_pprint(obj, p, cycle): #: the exception base +_exception_base: type try: _exception_base = BaseException except NameError: @@ -848,8 +850,8 @@ _type_pprinters[range] = _repr_pprint _type_pprinters[bytes] = _repr_pprint #: printers for types specified by name -_deferred_type_pprinters = { -} +_deferred_type_pprinters: Dict = {} + def for_type(typ, func): """ diff --git a/pyproject.toml b/pyproject.toml index 32ef454..6cd5c98 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,14 +17,8 @@ exclude = [ 'PyColorize.py', '_process_win32_controller.py', 'IPython/core/application.py', - 'IPython/core/completerlib.py', - 'IPython/core/displaypub.py', - #'IPython/core/interactiveshell.py', - 'IPython/core/magic.py', 'IPython/core/profileapp.py', - # 'IPython/core/ultratb.py', 'IPython/lib/deepreload.py', - 'IPython/lib/pretty.py', 'IPython/sphinxext/ipython_directive.py', 'IPython/terminal/ipapp.py', 'IPython/utils/_process_win32.py',