-
-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typing support #443
Comments
I get a similar issue with mypy:
I don't see any way around this other than to define a subclass of 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() |
Tidier (IMO) solution that uses the same approach: import typing as t
import argparse
class ArgcompleteAction(argparse.Action):
completer: t.Callable | None
class ArgcompleteArgumentParser(argparse.ArgumentParser):
def add_argument(self, *args, **kwargs) -> ArgcompleteAction:
return t.cast(ArgcompleteAction, super().add_argument(*args, **kwargs)) Then you can just replace the normal call to This class could go into argcomplete itself. If I have a minute I'll put together a PR for discussion. |
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.5The text was updated successfully, but these errors were encountered: