From 20fc7c291f4e1661f1b3f26bc0865db5aaddb437 2024-02-08 08:35:55 From: Matthias Bussonnier Date: 2024-02-08 08:35:55 Subject: [PATCH] Some minimal typing and removal of allow-none --- diff --git a/IPython/core/completer.py b/IPython/core/completer.py index 7cc7676..4eef2b5 100644 --- a/IPython/core/completer.py +++ b/IPython/core/completer.py @@ -2550,7 +2550,7 @@ class IPCompleter(Completer): EvaluationContext( globals=self.global_namespace, locals=self.namespace, - evaluation=self.evaluation, + evaluation=self.evaluation, # type: ignore in_subscript=True, ), ) diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py index 4344c08..9e59e23 100644 --- a/IPython/core/formatters.py +++ b/IPython/core/formatters.py @@ -53,13 +53,13 @@ class DisplayFormatter(Configurable): else: formatter.enabled = False - ipython_display_formatter = ForwardDeclaredInstance("FormatterABC") + ipython_display_formatter = ForwardDeclaredInstance("FormatterABC") # type: ignore @default("ipython_display_formatter") def _default_formatter(self): return IPythonDisplayFormatter(parent=self) - mimebundle_formatter = ForwardDeclaredInstance("FormatterABC") + mimebundle_formatter = ForwardDeclaredInstance("FormatterABC") # type: ignore @default("mimebundle_formatter") def _default_mime_formatter(self): diff --git a/IPython/core/history.py b/IPython/core/history.py index fb67d15..bd0c687 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -489,8 +489,9 @@ class HistoryManager(HistoryAccessor): input_hist_parsed = List([""]) input_hist_raw = List([""]) # A list of directories visited during session - dir_hist = List() - @default('dir_hist') + dir_hist: List = List() + + @default("dir_hist") def _dir_hist_default(self): try: return [Path.cwd()] @@ -514,8 +515,8 @@ class HistoryManager(HistoryAccessor): "Values of 1 or less effectively disable caching." ).tag(config=True) # The input and output caches - db_input_cache = List() - db_output_cache = List() + db_input_cache: List = List() + db_output_cache: List = List() # History saving in separate thread save_thread = Instance('IPython.core.history.HistorySavingThread', @@ -526,10 +527,10 @@ class HistoryManager(HistoryAccessor): # Variables used to store the three last inputs from the user. On each new # history update, we populate the user's namespace with these, shifted as # necessary. - _i00 = Unicode(u'') - _i = Unicode(u'') - _ii = Unicode(u'') - _iii = Unicode(u'') + _i00 = Unicode("") + _i = Unicode("") + _ii = Unicode("") + _iii = Unicode("") # A regex matching all forms of the exit command, so that we don't store # them in the history (it's annoying to rewind the first entry and land on diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index fef5ddc..cba7774 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -314,11 +314,12 @@ class InteractiveShell(SingletonConfigurable): _instance = None - ast_transformers = List([], help= - """ + ast_transformers: List[ast.NodeTransformer] = List( + [], + help=""" A list of ast.NodeTransformer subclass instances, which will be applied to user input before code is run. - """ + """, ).tag(config=True) autocall = Enum((0,1,2), default_value=0, help= @@ -553,14 +554,20 @@ class InteractiveShell(SingletonConfigurable): ).tag(config=True) # Subcomponents of InteractiveShell - alias_manager = Instance('IPython.core.alias.AliasManager', allow_none=True) - prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) - builtin_trap = Instance('IPython.core.builtin_trap.BuiltinTrap', allow_none=True) - display_trap = Instance('IPython.core.display_trap.DisplayTrap', allow_none=True) - extension_manager = Instance('IPython.core.extensions.ExtensionManager', allow_none=True) - payload_manager = Instance('IPython.core.payload.PayloadManager', allow_none=True) - history_manager = Instance('IPython.core.history.HistoryAccessorBase', allow_none=True) - magics_manager = Instance('IPython.core.magic.MagicsManager', allow_none=True) + alias_manager = Instance("IPython.core.alias.AliasManager", allow_none=True) + prefilter_manager = Instance( + "IPython.core.prefilter.PrefilterManager", allow_none=True + ) + builtin_trap = Instance("IPython.core.builtin_trap.BuiltinTrap") + display_trap = Instance("IPython.core.display_trap.DisplayTrap") + extension_manager = Instance( + "IPython.core.extensions.ExtensionManager", allow_none=True + ) + payload_manager = Instance("IPython.core.payload.PayloadManager", allow_none=True) + history_manager = Instance( + "IPython.core.history.HistoryAccessorBase", allow_none=True + ) + magics_manager = Instance("IPython.core.magic.MagicsManager") profile_dir = Instance('IPython.core.application.ProfileDir', allow_none=True) @property @@ -1396,6 +1403,7 @@ class InteractiveShell(SingletonConfigurable): If new_session is True, a new history session will be opened. """ # Clear histories + assert self.history_manager is not None self.history_manager.reset(new_session) # Reset counter used to index all histories if new_session: @@ -1482,6 +1490,7 @@ class InteractiveShell(SingletonConfigurable): except KeyError as e: raise NameError("name '%s' is not defined" % varname) from e # Also check in output history + assert self.history_manager is not None ns_refs.append(self.history_manager.output_hist) for ns in ns_refs: to_delete = [n for n, o in ns.items() if o is obj] @@ -1801,7 +1810,7 @@ class InteractiveShell(SingletonConfigurable): """Find an object and return a struct with info about it.""" return self._ofind(oname, namespaces) - def _inspect(self, meth, oname, namespaces=None, **kw): + def _inspect(self, meth, oname: str, namespaces=None, **kw): """Generic interface to the inspector system. This function is meant to be called by pdef, pdoc & friends. @@ -2409,7 +2418,7 @@ class InteractiveShell(SingletonConfigurable): res = finder(magic_name) return res - def run_line_magic(self, magic_name: str, line, _stack_depth=1): + def run_line_magic(self, magic_name: str, line: str, _stack_depth=1): """Execute the given line magic. Parameters @@ -3256,6 +3265,7 @@ class InteractiveShell(SingletonConfigurable): # Store raw and processed history if store_history: + assert self.history_manager is not None self.history_manager.store_inputs(self.execution_count, cell, raw_cell) if not silent: self.logger.log(cell, raw_cell) @@ -3272,8 +3282,6 @@ class InteractiveShell(SingletonConfigurable): # compiler compiler = self.compile if shell_futures else self.compiler_class() - _run_async = False - with self.builtin_trap: cell_name = compiler.cache(cell, self.execution_count, raw_code=raw_cell) diff --git a/IPython/core/magics/ast_mod.py b/IPython/core/magics/ast_mod.py index 35cd1df..fa54791 100644 --- a/IPython/core/magics/ast_mod.py +++ b/IPython/core/magics/ast_mod.py @@ -178,7 +178,17 @@ transforming: __skip_doctest__ = True -from ast import NodeTransformer, Store, Load, Name, Expr, Assign, Module, Import, ImportFrom +from ast import ( + NodeTransformer, + Store, + Load, + Name, + Expr, + Assign, + Module, + Import, + ImportFrom, +) import ast import copy @@ -231,13 +241,13 @@ class Mangler(NodeTransformer): self.log("Not mangling function arg", arg.arg) return self.generic_visit(node) - def visit_ImportFrom(self, node:ImportFrom): + def visit_ImportFrom(self, node: ImportFrom): return self._visit_Import_and_ImportFrom(node) - def visit_Import(self, node:Import): + def visit_Import(self, node: Import): return self._visit_Import_and_ImportFrom(node) - def _visit_Import_and_ImportFrom(self, node:Union[Import, ImportFrom]): + def _visit_Import_and_ImportFrom(self, node: Union[Import, ImportFrom]): for alias in node.names: asname = alias.name if alias.asname is None else alias.asname if self.predicate(asname): diff --git a/IPython/core/magics/script.py b/IPython/core/magics/script.py index a858c64..0c405ef 100644 --- a/IPython/core/magics/script.py +++ b/IPython/core/magics/script.py @@ -86,7 +86,7 @@ class ScriptMagics(Magics): """ ) - script_magics = List( + script_magics: List = List( help="""Extra script cell magics to define This generates simple wrappers of `%%script foo` as `%%foo`. @@ -95,6 +95,7 @@ class ScriptMagics(Magics): specify them in script_paths """, ).tag(config=True) + @default('script_magics') def _script_magics_default(self): """default to a common list of programs""" diff --git a/IPython/core/prefilter.py b/IPython/core/prefilter.py index e7e82e3..5b1b86c 100644 --- a/IPython/core/prefilter.py +++ b/IPython/core/prefilter.py @@ -524,11 +524,14 @@ class AutocallChecker(PrefilterChecker): class PrefilterHandler(Configurable): - - handler_name = Unicode('normal') - esc_strings = List([]) - shell = Instance('IPython.core.interactiveshell.InteractiveShellABC', allow_none=True) - prefilter_manager = Instance('IPython.core.prefilter.PrefilterManager', allow_none=True) + handler_name = Unicode("normal") + esc_strings: List = List([]) + shell = Instance( + "IPython.core.interactiveshell.InteractiveShellABC", allow_none=True + ) + prefilter_manager = Instance( + "IPython.core.prefilter.PrefilterManager", allow_none=True + ) def __init__(self, shell=None, prefilter_manager=None, **kwargs): super(PrefilterHandler, self).__init__(