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/historyapp.py b/IPython/core/historyapp.py index 01a5534..85dd9c5 100644 --- a/IPython/core/historyapp.py +++ b/IPython/core/historyapp.py @@ -32,25 +32,21 @@ This is an handy alias to `ipython history trim --keep=0` class HistoryTrim(BaseIPythonApplication): description = trim_hist_help - - backup = Bool(False, - help="Keep the old history file as history.sqlite." - ).tag(config=True) - - keep = Int(1000, - help="Number of recent lines to keep in the database." - ).tag(config=True) - - flags = Dict(dict( - backup = ({'HistoryTrim' : {'backup' : True}}, - backup.help - ) - )) - aliases=Dict(dict( - keep = 'HistoryTrim.keep' - )) - + backup = Bool(False, help="Keep the old history file as history.sqlite.").tag( + config=True + ) + + keep = Int(1000, help="Number of recent lines to keep in the database.").tag( + config=True + ) + + flags = Dict( # type: ignore + dict(backup=({"HistoryTrim": {"backup": True}}, backup.help)) + ) + + aliases = Dict(dict(keep="HistoryTrim.keep")) # type: ignore + def start(self): profile_dir = Path(self.profile_dir.location) hist_file = profile_dir / "history.sqlite" @@ -114,34 +110,33 @@ class HistoryTrim(BaseIPythonApplication): print("Backed up longer history file to", backup_hist_file) else: hist_file.unlink() - + new_hist_file.rename(hist_file) + class HistoryClear(HistoryTrim): description = clear_hist_help - keep = Int(0, - help="Number of recent lines to keep in the database.") - - force = Bool(False, - help="Don't prompt user for confirmation" - ).tag(config=True) - - flags = Dict(dict( - force = ({'HistoryClear' : {'force' : True}}, - force.help), - f = ({'HistoryTrim' : {'force' : True}}, - force.help + keep = Int(0, help="Number of recent lines to keep in the database.") + + force = Bool(False, help="Don't prompt user for confirmation").tag(config=True) + + flags = Dict( # type: ignore + dict( + force=({"HistoryClear": {"force": True}}, force.help), + f=({"HistoryTrim": {"force": True}}, force.help), ) - )) - aliases = Dict() + ) + aliases = Dict() # type: ignore def start(self): - if self.force or ask_yes_no("Really delete all ipython history? ", - default="no", interrupt="no"): + if self.force or ask_yes_no( + "Really delete all ipython history? ", default="no", interrupt="no" + ): HistoryTrim.start(self) + class HistoryApp(Application): - name = u'ipython-history' + name = "ipython-history" description = "Manage the IPython history database." subcommands = Dict(dict( diff --git a/IPython/core/inputsplitter.py b/IPython/core/inputsplitter.py index 33ed563..f1ebd96 100644 --- a/IPython/core/inputsplitter.py +++ b/IPython/core/inputsplitter.py @@ -15,6 +15,7 @@ and stores the results. For more details, see the class docstrings below. """ +from __future__ import annotations from warnings import warn @@ -31,7 +32,7 @@ import sys import tokenize import warnings -from typing import List, Tuple, Union, Optional +from typing import List, Tuple, Union, Optional, TYPE_CHECKING from types import CodeType from IPython.core.inputtransformer import (leading_indent, @@ -52,6 +53,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 #----------------------------------------------------------------------------- @@ -637,9 +640,9 @@ class IPythonInputSplitter(InputSplitter): # Nothing that calls reset() expects to handle transformer # errors pass - - def flush_transformers(self): - def _flush(transform, outs): + + def flush_transformers(self: Self): + def _flush(transform, outs: List[str]): """yield transformed lines always strings, never None diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 415f088..904f7b2 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -281,13 +281,14 @@ class ExecutionInfo(object): ) -class ExecutionResult(object): +class ExecutionResult: """The result of a call to :meth:`InteractiveShell.run_cell` Stores information about what took place. """ - execution_count = None - error_before_exec = None + + execution_count: Optional[int] = None + error_before_exec: Optional[bool] = None error_in_exec: Optional[BaseException] = None info = None result = None @@ -477,7 +478,8 @@ class InteractiveShell(SingletonConfigurable): def input_transformers_cleanup(self): return self.input_transformer_manager.cleanup_transforms - input_transformers_post = List([], + input_transformers_post: List = List( + [], help="A list of string input transformers, to be applied after IPython's " "own input transformations." ) @@ -3340,6 +3342,7 @@ class InteractiveShell(SingletonConfigurable): self.displayhook.exec_result = None if store_history: + assert self.history_manager is not None # Write output to the database. Does nothing unless # history output logging is enabled. self.history_manager.store_output(self.execution_count) 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 15ea5c1..6cd5c98 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,21 +17,12 @@ exclude = [ 'PyColorize.py', '_process_win32_controller.py', 'IPython/core/application.py', - 'IPython/core/completerlib.py', - 'IPython/core/displaypub.py', - 'IPython/core/historyapp.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', 'IPython/utils/path.py', - 'IPython/utils/timing.py', - 'IPython/utils/text.py' ] [tool.pytest.ini_options]