Skip to content

Commit 9984c23

Browse files
committed
Add type annotations for line cache
1 parent 2dd27f9 commit 9984c23

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

bpython/patch_linecache.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import linecache
2+
from typing import Any, List, Tuple
23

34

45
class BPythonLinecache(dict):
56
"""Replaces the cache dict in the standard-library linecache module,
67
to also remember (in an unerasable way) bpython console input."""
78

8-
def __init__(self, *args, **kwargs):
9+
def __init__(self, *args, **kwargs) -> None:
910
super().__init__(*args, **kwargs)
1011
self.bpython_history = []
1112

12-
def is_bpython_filename(self, fname):
13+
def is_bpython_filename(self, fname: Any) -> bool:
1314
try:
1415
return fname.startswith("<bpython-input-")
1516
except AttributeError:
1617
# In case the key isn't a string
1718
return False
1819

19-
def get_bpython_history(self, key):
20+
def get_bpython_history(self, key: str) -> Tuple[int, None, List[str], str]:
2021
"""Given a filename provided by remember_bpython_input,
2122
returns the associated source string."""
2223
try:
@@ -25,21 +26,21 @@ def get_bpython_history(self, key):
2526
except (IndexError, ValueError):
2627
raise KeyError
2728

28-
def remember_bpython_input(self, source):
29+
def remember_bpython_input(self, source: str) -> str:
2930
"""Remembers a string of source code, and returns
3031
a fake filename to use to retrieve it later."""
31-
filename = "<bpython-input-%s>" % len(self.bpython_history)
32+
filename = f"<bpython-input-{len(self.bpython_history)}>"
3233
self.bpython_history.append(
3334
(len(source), None, source.splitlines(True), filename)
3435
)
3536
return filename
3637

37-
def __getitem__(self, key):
38+
def __getitem__(self, key: Any) -> Any:
3839
if self.is_bpython_filename(key):
3940
return self.get_bpython_history(key)
4041
return super().__getitem__(key)
4142

42-
def __contains__(self, key):
43+
def __contains__(self, key: Any) -> bool:
4344
if self.is_bpython_filename(key):
4445
try:
4546
self.get_bpython_history(key)
@@ -48,9 +49,9 @@ def __contains__(self, key):
4849
return False
4950
return super().__contains__(key)
5051

51-
def __delitem__(self, key):
52+
def __delitem__(self, key: Any) -> None:
5253
if not self.is_bpython_filename(key):
53-
return super().__delitem__(key)
54+
super().__delitem__(key)
5455

5556

5657
def _bpython_clear_linecache() -> None:

0 commit comments

Comments
 (0)