diff --git a/IPython/core/alias.py b/IPython/core/alias.py index 52843b3..845e6b7 100644 --- a/IPython/core/alias.py +++ b/IPython/core/alias.py @@ -30,6 +30,9 @@ from .error import UsageError from traitlets import List, Instance from logging import error +import typing as t + + #----------------------------------------------------------------------------- # Utilities #----------------------------------------------------------------------------- @@ -37,7 +40,7 @@ from logging import error # This is used as the pattern for calls to split_user_input. shell_line_split = re.compile(r'^(\s*)()(\S+)(.*$)') -def default_aliases(): +def default_aliases() -> t.List[t.Tuple[str, str]]: """Return list of shell aliases to auto-define. """ # Note: the aliases defined here should be safe to use on a kernel 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 15cf703..9e59e23 100644 --- a/IPython/core/formatters.py +++ b/IPython/core/formatters.py @@ -53,20 +53,23 @@ class DisplayFormatter(Configurable): else: formatter.enabled = False - ipython_display_formatter = ForwardDeclaredInstance('FormatterABC') - @default('ipython_display_formatter') + ipython_display_formatter = ForwardDeclaredInstance("FormatterABC") # type: ignore + + @default("ipython_display_formatter") def _default_formatter(self): return IPythonDisplayFormatter(parent=self) - mimebundle_formatter = ForwardDeclaredInstance('FormatterABC') - @default('mimebundle_formatter') + mimebundle_formatter = ForwardDeclaredInstance("FormatterABC") # type: ignore + + @default("mimebundle_formatter") def _default_mime_formatter(self): return MimeBundleFormatter(parent=self) # A dict of formatter whose keys are format types (MIME types) and whose # values are subclasses of BaseFormatter. formatters = Dict() - @default('formatters') + + @default("formatters") def _formatters_default(self): """Activate the default formatters.""" formatter_classes = [ diff --git a/IPython/core/history.py b/IPython/core/history.py index cb11cfb..f59ca11 100644 --- a/IPython/core/history.py +++ b/IPython/core/history.py @@ -490,8 +490,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()] @@ -515,8 +516,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', @@ -527,10 +528,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 e28b9f1..fa54791 100644 --- a/IPython/core/magics/ast_mod.py +++ b/IPython/core/magics/ast_mod.py @@ -178,11 +178,21 @@ transforming: __skip_doctest__ = True -from ast import NodeTransformer, Store, Load, Name, Expr, Assign, Module +from ast import ( + NodeTransformer, + Store, + Load, + Name, + Expr, + Assign, + Module, + Import, + ImportFrom, +) import ast import copy -from typing import Dict, Optional +from typing import Dict, Optional, Union mangle_all = lambda name: False if name in ("__ret__", "__code__") else True @@ -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): + def visit_ImportFrom(self, node: ImportFrom): return self._visit_Import_and_ImportFrom(node) - def visit_Import(self, node): + def visit_Import(self, node: Import): return self._visit_Import_and_ImportFrom(node) - def _visit_Import_and_ImportFrom(self, node): + 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__( diff --git a/IPython/utils/text.py b/IPython/utils/text.py index 8f73dca..51dcdae 100644 --- a/IPython/utils/text.py +++ b/IPython/utils/text.py @@ -1,4 +1,3 @@ -# encoding: utf-8 """ Utilities for working with strings and text. @@ -11,13 +10,12 @@ Inheritance diagram: import os import re import string -import sys import textwrap import warnings from string import Formatter from pathlib import Path -from typing import List, Dict, Tuple +from typing import List, Dict, Tuple, Optional, cast class LSString(str): @@ -540,11 +538,12 @@ class FullEvalFormatter(Formatter): """ # copied from Formatter._vformat with minor changes to allow eval # and replace the format_spec code with slicing - def vformat(self, format_string:str, args, kwargs)->str: + def vformat(self, format_string: str, args, kwargs) -> str: result = [] - for literal_text, field_name, format_spec, conversion in \ - self.parse(format_string): - + conversion: Optional[str] + for literal_text, field_name, format_spec, conversion in self.parse( + format_string + ): # output the literal text if literal_text: result.append(literal_text) @@ -563,7 +562,8 @@ class FullEvalFormatter(Formatter): obj = eval(field_name, kwargs) # do any conversion on the resulting object - obj = self.convert_field(obj, conversion) + # type issue in typeshed, fined in https://github.com/python/typeshed/pull/11377 + obj = self.convert_field(obj, conversion) # type: ignore[arg-type] # format the object and append to the result result.append(self.format_field(obj, '')) @@ -722,7 +722,13 @@ def compute_item_matrix( return ([[_get_or_default(items, c * nrow + r, default=empty) for c in range(ncol)] for r in range(nrow)], info) -def columnize(items, row_first=False, separator=" ", displaywidth=80, spread=False): +def columnize( + items: List[str], + row_first: bool = False, + separator: str = " ", + displaywidth: int = 80, + spread: bool = False, +): """Transform a list of strings into a single string with columns. Parameters @@ -743,7 +749,7 @@ def columnize(items, row_first=False, separator=" ", displaywidth=80, spread=Fa """ warnings.warn( "`columnize` is Pending Deprecation since IPython 8.17." - "It is considered fro removal in in future version. " + "It is considered for removal in future versions. " "Please open an issue if you believe it should be kept.", stacklevel=2, category=PendingDeprecationWarning, @@ -761,7 +767,7 @@ def columnize(items, row_first=False, separator=" ", displaywidth=80, spread=Fa 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"])] + [y.ljust(w, " ") for y, w in zip(x, cast(List[int], info["column_widths"]))] ) return "\n".join(map(sjoin, fmatrix)) + "\n"