Skip to content

Commit 05d4aed

Browse files
Added more precise types in various places.
1 parent 042ecc3 commit 05d4aed

13 files changed

+324
-160
lines changed

ptpython/completer.py

+18-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import keyword
55
import re
66
from enum import Enum
7-
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional
7+
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, List, Optional, Tuple
88

99
from prompt_toolkit.completion import (
1010
CompleteEvent,
@@ -21,6 +21,7 @@
2121
from ptpython.utils import get_jedi_script_from_document
2222

2323
if TYPE_CHECKING:
24+
import jedi.api.classes
2425
from prompt_toolkit.contrib.regular_languages.compiler import _CompiledGrammar
2526

2627
__all__ = ["PythonCompleter", "CompletePrivateAttributes", "HidePrivateCompleter"]
@@ -43,8 +44,8 @@ class PythonCompleter(Completer):
4344

4445
def __init__(
4546
self,
46-
get_globals: Callable[[], dict],
47-
get_locals: Callable[[], dict],
47+
get_globals: Callable[[], Dict[str, Any]],
48+
get_locals: Callable[[], Dict[str, Any]],
4849
enable_dictionary_completion: Callable[[], bool],
4950
) -> None:
5051
super().__init__()
@@ -200,7 +201,11 @@ class JediCompleter(Completer):
200201
Autocompleter that uses the Jedi library.
201202
"""
202203

203-
def __init__(self, get_globals, get_locals) -> None:
204+
def __init__(
205+
self,
206+
get_globals: Callable[[], Dict[str, Any]],
207+
get_locals: Callable[[], Dict[str, Any]],
208+
) -> None:
204209
super().__init__()
205210

206211
self.get_globals = get_globals
@@ -296,7 +301,11 @@ class DictionaryCompleter(Completer):
296301
function calls, so it only triggers attribute access.
297302
"""
298303

299-
def __init__(self, get_globals, get_locals):
304+
def __init__(
305+
self,
306+
get_globals: Callable[[], Dict[str, Any]],
307+
get_locals: Callable[[], Dict[str, Any]],
308+
) -> None:
300309
super().__init__()
301310

302311
self.get_globals = get_globals
@@ -574,7 +583,7 @@ def _sort_attribute_names(self, names: List[str]) -> List[str]:
574583
underscore names to the end.
575584
"""
576585

577-
def sort_key(name: str):
586+
def sort_key(name: str) -> Tuple[int, str]:
578587
if name.startswith("__"):
579588
return (2, name) # Double underscore comes latest.
580589
if name.startswith("_"):
@@ -639,7 +648,9 @@ class ReprFailedError(Exception):
639648
_builtin_names = []
640649

641650

642-
def _get_style_for_jedi_completion(jedi_completion) -> str:
651+
def _get_style_for_jedi_completion(
652+
jedi_completion: "jedi.api.classes.Completion",
653+
) -> str:
643654
"""
644655
Return completion style to use for this name.
645656
"""

ptpython/entry_points/run_ptpython.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
import pathlib
2727
import sys
2828
from textwrap import dedent
29-
from typing import Tuple
29+
from typing import IO, Optional, Tuple
3030

3131
import appdirs
3232
from prompt_toolkit.formatted_text import HTML
3333
from prompt_toolkit.shortcuts import print_formatted_text
3434

35-
from ptpython.repl import embed, enable_deprecation_warnings, run_config
35+
from ptpython.repl import PythonRepl, embed, enable_deprecation_warnings, run_config
3636

3737
try:
38-
from importlib import metadata
38+
from importlib import metadata # type: ignore
3939
except ImportError:
4040
import importlib_metadata as metadata # type: ignore
4141

@@ -44,7 +44,7 @@
4444

4545

4646
class _Parser(argparse.ArgumentParser):
47-
def print_help(self):
47+
def print_help(self, file: Optional[IO[str]] = None) -> None:
4848
super().print_help()
4949
print(
5050
dedent(
@@ -84,7 +84,7 @@ def create_parser() -> _Parser:
8484
"-V",
8585
"--version",
8686
action="version",
87-
version=metadata.version("ptpython"), # type: ignore
87+
version=metadata.version("ptpython"),
8888
)
8989
parser.add_argument("args", nargs="*", help="Script and arguments")
9090
return parser
@@ -190,7 +190,7 @@ def run() -> None:
190190
enable_deprecation_warnings()
191191

192192
# Apply config file
193-
def configure(repl) -> None:
193+
def configure(repl: PythonRepl) -> None:
194194
if os.path.exists(config_file):
195195
run_config(repl, config_file)
196196

ptpython/eventloop.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import sys
1111
import time
1212

13+
from prompt_toolkit.eventloop import InputHookContext
14+
1315
__all__ = ["inputhook"]
1416

1517

16-
def _inputhook_tk(inputhook_context):
18+
def _inputhook_tk(inputhook_context: InputHookContext) -> None:
1719
"""
1820
Inputhook for Tk.
1921
Run the Tk eventloop until prompt-toolkit needs to process the next input.
@@ -23,9 +25,9 @@ def _inputhook_tk(inputhook_context):
2325

2426
import _tkinter # Keep this imports inline!
2527

26-
root = tkinter._default_root
28+
root = tkinter._default_root # type: ignore
2729

28-
def wait_using_filehandler():
30+
def wait_using_filehandler() -> None:
2931
"""
3032
Run the TK eventloop until the file handler that we got from the
3133
inputhook becomes readable.
@@ -34,7 +36,7 @@ def wait_using_filehandler():
3436
# to process.
3537
stop = [False]
3638

37-
def done(*a):
39+
def done(*a: object) -> None:
3840
stop[0] = True
3941

4042
root.createfilehandler(inputhook_context.fileno(), _tkinter.READABLE, done)
@@ -46,7 +48,7 @@ def done(*a):
4648

4749
root.deletefilehandler(inputhook_context.fileno())
4850

49-
def wait_using_polling():
51+
def wait_using_polling() -> None:
5052
"""
5153
Windows TK doesn't support 'createfilehandler'.
5254
So, run the TK eventloop and poll until input is ready.
@@ -65,7 +67,7 @@ def wait_using_polling():
6567
wait_using_polling()
6668

6769

68-
def inputhook(inputhook_context):
70+
def inputhook(inputhook_context: InputHookContext) -> None:
6971
# Only call the real input hook when the 'Tkinter' library was loaded.
7072
if "Tkinter" in sys.modules or "tkinter" in sys.modules:
7173
_inputhook_tk(inputhook_context)

0 commit comments

Comments
 (0)