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/formatters.py b/IPython/core/formatters.py index 15cf703..4344c08 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") + + @default("ipython_display_formatter") def _default_formatter(self): return IPythonDisplayFormatter(parent=self) - mimebundle_formatter = ForwardDeclaredInstance('FormatterABC') - @default('mimebundle_formatter') + mimebundle_formatter = ForwardDeclaredInstance("FormatterABC") + + @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/magics/ast_mod.py b/IPython/core/magics/ast_mod.py index e28b9f1..35cd1df 100644 --- a/IPython/core/magics/ast_mod.py +++ b/IPython/core/magics/ast_mod.py @@ -178,11 +178,11 @@ 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 +231,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/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"