Skip to content

Commit d42b21a

Browse files
committed
Turn FuncProps into a dataclass
1 parent c2e3fac commit d42b21a

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

bpython/autocomplete.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,11 +570,11 @@ def matches(
570570

571571
matches = {
572572
f"{name}="
573-
for name in argspec[1][0]
573+
for name in argspec.argspec[0]
574574
if isinstance(name, str) and name.startswith(r.word)
575575
}
576576
matches.update(
577-
name + "=" for name in argspec[1][4] if name.startswith(r.word)
577+
name + "=" for name in argspec.argspec[4] if name.startswith(r.word)
578578
)
579579
return matches if matches else None
580580

@@ -711,7 +711,7 @@ def get_completer(
711711
line is a string of the current line
712712
kwargs (all optional):
713713
locals_ is a dictionary of the environment
714-
argspec is an inspect.ArgSpec instance for the current function where
714+
argspec is an inspect.FuncProps instance for the current function where
715715
the cursor is
716716
current_block is the possibly multiline not-yet-evaluated block of
717717
code which the current line is part of

bpython/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
from . import translations
102102
from .translations import _
103103

104-
from . import repl
104+
from . import repl, inspection
105105
from . import args as bpargs
106106
from .pager import page
107107
from .args import parse as argsparse
@@ -726,7 +726,7 @@ def lf(self) -> None:
726726

727727
def mkargspec(
728728
self,
729-
topline: Any, # Named tuples don't seem to play nice with mypy
729+
topline: inspection.FuncProps,
730730
in_arg: Union[str, int, None],
731731
down: bool,
732732
) -> int:
@@ -1298,7 +1298,7 @@ def show_list(
12981298
self,
12991299
items: List[str],
13001300
arg_pos: Union[str, int, None],
1301-
topline: Any = None, # Named tuples don't play nice with mypy
1301+
topline: Optional[inspection.FuncProps] = None,
13021302
formatter: Optional[Callable] = None,
13031303
current_item: Union[str, Literal[False]] = None,
13041304
) -> None:

bpython/inspection.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
import pydoc
2727
import re
2828
from collections import namedtuple
29-
from typing import Any, Optional, Type, Dict, List
29+
from dataclasses import dataclass
30+
from typing import Any, Callable, Optional, Type, Dict, List
3031
from types import MemberDescriptorType, TracebackType
3132
from ._typing_compat import Literal
3233

@@ -35,6 +36,7 @@
3536

3637
from .lazyre import LazyReCompile
3738

39+
3840
ArgSpec = namedtuple(
3941
"ArgSpec",
4042
[
@@ -48,7 +50,12 @@
4850
],
4951
)
5052

51-
FuncProps = namedtuple("FuncProps", ["func", "argspec", "is_bound_method"])
53+
54+
@dataclass
55+
class FuncProps:
56+
func: str
57+
argspec: ArgSpec
58+
is_bound_method: bool
5259

5360

5461
class AttrCleaner:
@@ -112,10 +119,10 @@ class _Repr:
112119
Helper for `fixlongargs()`: Returns the given value in `__repr__()`.
113120
"""
114121

115-
def __init__(self, value):
122+
def __init__(self, value: str) -> None:
116123
self.value = value
117124

118-
def __repr__(self):
125+
def __repr__(self) -> str:
119126
return self.value
120127

121128
__str__ = __repr__

bpython/test/test_autocomplete.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
except ImportError:
1212
has_jedi = False
1313

14-
from bpython import autocomplete
14+
from bpython import autocomplete, inspection
1515
from bpython.line import LinePart
1616

1717
glob_function = "glob.iglob"
@@ -418,8 +418,8 @@ def test_set_of_params_returns_when_matches_found(self):
418418
def func(apple, apricot, banana, carrot):
419419
pass
420420

421-
argspec = list(inspect.getfullargspec(func))
422-
argspec = ["func", argspec, False]
421+
argspec = inspection.ArgSpec(*inspect.getfullargspec(func))
422+
argspec = inspection.FuncProps("func", argspec, False)
423423
com = autocomplete.ParameterNameCompletion()
424424
self.assertSetEqual(
425425
com.matches(1, "a", argspec=argspec), {"apple=", "apricot="}

bpython/urwid.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,9 @@ def _populate_completion(self):
762762
if self.complete():
763763
if self.funcprops:
764764
# This is mostly just stolen from the cli module.
765-
func_name, args, is_bound = self.funcprops
765+
func_name = self.funcprops.func
766+
args = self.funcprops.argspec
767+
is_bound = self.funcprops.is_bound_method
766768
in_arg = self.arg_pos
767769
args, varargs, varkw, defaults = args[:4]
768770
kwonly = self.funcprops.argspec.kwonly

0 commit comments

Comments
 (0)