Skip to content

Commit

Permalink
more types, everywhere, and remove some ignored files from mypy. (#14325
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Carreau committed Feb 16, 2024
2 parents 3d02f18 + cc6c7a0 commit 06d3ec8
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 69 deletions.
13 changes: 8 additions & 5 deletions IPython/core/completerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
# 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.
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 18 additions & 7 deletions IPython/core/displaypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
# 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
#-----------------------------------------------------------------------------

Expand Down Expand Up @@ -103,9 +105,9 @@ def publish(self, data, metadata=None, source=None, *, transient=None, update=Fa
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:
Expand All @@ -125,11 +127,20 @@ def clear_output(self, wait=False):

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)
Expand Down
67 changes: 31 additions & 36 deletions IPython/core/historyapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,21 @@

class HistoryTrim(BaseIPythonApplication):
description = trim_hist_help

backup = Bool(False,
help="Keep the old history file as history.sqlite.<N>"
).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.<N>").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"
Expand Down Expand Up @@ -114,34 +110,33 @@ def start(self):
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(
Expand Down
11 changes: 7 additions & 4 deletions IPython/core/inputsplitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
For more details, see the class docstrings below.
"""
from __future__ import annotations

from warnings import warn

Expand All @@ -31,7 +32,7 @@
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,
Expand All @@ -52,6 +53,8 @@
ESC_HELP2, ESC_MAGIC, ESC_MAGIC2,
ESC_QUOTE, ESC_QUOTE2, ESC_PAREN, ESC_SEQUENCES)

if TYPE_CHECKING:
from typing_extensions import Self
#-----------------------------------------------------------------------------
# Utilities
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -637,9 +640,9 @@ def reset(self):
# 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
Expand Down
11 changes: 7 additions & 4 deletions IPython/core/interactiveshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,14 @@ def __repr__(self):
)


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
Expand Down Expand Up @@ -477,7 +478,8 @@ def _exiter_default(self):
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."
)
Expand Down Expand Up @@ -3340,6 +3342,7 @@ def error_before_exec(value):
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)
Expand Down
4 changes: 3 additions & 1 deletion IPython/core/magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
from traitlets import Bool, Dict, Instance, observe
from logging import error

import typing as t

#-----------------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------------
Expand All @@ -36,7 +38,7 @@
# 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')
Expand Down
8 changes: 5 additions & 3 deletions IPython/lib/pretty.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -108,6 +107,8 @@ def _repr_pretty_(self, p, cycle):
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']

Expand Down Expand Up @@ -807,6 +808,7 @@ def _exception_pprint(obj, p, cycle):


#: the exception base
_exception_base: type
try:
_exception_base = BaseException
except NameError:
Expand Down Expand Up @@ -848,8 +850,8 @@ def _exception_pprint(obj, p, cycle):
_type_pprinters[bytes] = _repr_pprint

#: printers for types specified by name
_deferred_type_pprinters = {
}
_deferred_type_pprinters: Dict = {}


def for_type(typ, func):
"""
Expand Down
9 changes: 0 additions & 9 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 06d3ec8

Please sign in to comment.