Open aspfohl opened 12 months ago
I get a similar issue with mypy:
error: "Action" has no attribute "completer" [attr-defined]
I don't see any way around this other than to define a subclass of argparse.Action
with the additional completer
attribute, and then cast the result of parser.add_argument
to it, for example:
import argparse
from collections.abc import Iterable
from typing import cast
from argcomplete.completers import BaseCompleter
class Action(argparse.Action):
completer: BaseCompleter | None
class ExampleCompleter(BaseCompleter):
def __call__(
self,
prefix: str,
action: argparse.Action,
parser: argparse.ArgumentParser,
parsed_args: argparse.Namespace,
) -> Iterable[str]:
return {"foo", "bar"}
class ExampleCompleterWithHelp(BaseCompleter):
def __call__(
self,
prefix: str,
action: argparse.Action,
parser: argparse.ArgumentParser,
parsed_args: argparse.Namespace,
) -> dict[str, str]:
return {"foo": "foo help", "bar": "bar help"}
cast(Action, parser.add_argument("--arg1")).completer = ExampleCompleter()
cast(Action, parser.add_argument("--arg2")).completer = ExampleCompleterWithHelp()
Seeing typing issues in pyright:
with a set up like this:
Any pointers on calling
completer
in a type-safe way? This is with argcomplete==3.0.8 and Python 3.10.5