Skip to content

Commit

Permalink
Misc typing and code cleanup. (#14324)
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Feb 8, 2024
2 parents 0a3cf8f + 20fc7c2 commit 27b7f55
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 52 deletions.
5 changes: 4 additions & 1 deletion IPython/core/alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@
from traitlets import List, Instance
from logging import error

import typing as t


#-----------------------------------------------------------------------------
# Utilities
#-----------------------------------------------------------------------------

# 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
Expand Down
2 changes: 1 addition & 1 deletion IPython/core/completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2550,7 +2550,7 @@ def dict_key_matches(self, text: str) -> List[str]:
EvaluationContext(
globals=self.global_namespace,
locals=self.namespace,
evaluation=self.evaluation,
evaluation=self.evaluation, # type: ignore
in_subscript=True,
),
)
Expand Down
13 changes: 8 additions & 5 deletions IPython/core/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,23 @@ def _active_types_changed(self, change):
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 = [
Expand Down
17 changes: 9 additions & 8 deletions IPython/core/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -515,8 +516,8 @@ def _dir_hist_default(self):
"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',
Expand All @@ -527,10 +528,10 @@ def _dir_hist_default(self):
# 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
Expand Down
38 changes: 23 additions & 15 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down Expand Up @@ -553,14 +554,20 @@ def input_splitter(self):
).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
Expand Down Expand Up @@ -1396,6 +1403,7 @@ def reset(self, new_session=True, aggressive=False):
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:
Expand Down Expand Up @@ -1482,6 +1490,7 @@ def del_var(self, varname, by_name=False):
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]
Expand Down Expand Up @@ -1801,7 +1810,7 @@ def _object_find(self, oname, namespaces=None) -> OInfo:
"""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.
Expand Down Expand Up @@ -2409,7 +2418,7 @@ def _find_with_lazy_load(self, /, type_, magic_name: str):
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
Expand Down Expand Up @@ -3256,6 +3265,7 @@ def error_before_exec(value):

# 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)
Expand All @@ -3272,8 +3282,6 @@ def error_before_exec(value):
# 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)

Expand Down
20 changes: 15 additions & 5 deletions IPython/core/magics/ast_mod.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,21 @@ def load_ipython_extension(ip):
__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
Expand Down Expand Up @@ -231,13 +241,13 @@ def visit_FunctionDef(self, node):
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):
Expand Down
3 changes: 2 additions & 1 deletion IPython/core/magics/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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"""
Expand Down
13 changes: 8 additions & 5 deletions IPython/core/prefilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,14 @@ def check(self, line_info):


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__(
Expand Down
28 changes: 17 additions & 11 deletions IPython/utils/text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# encoding: utf-8
"""
Utilities for working with strings and text.
Expand All @@ -11,13 +10,12 @@
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):
Expand Down Expand Up @@ -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)
Expand All @@ -563,7 +562,8 @@ def vformat(self, format_string:str, args, kwargs)->str:
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, ''))
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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"

Expand Down

0 comments on commit 27b7f55

Please sign in to comment.