davidhalter / jedi

Awesome autocompletion, static analysis and refactoring library for python
http://jedi.readthedocs.io
Other
5.78k stars 508 forks source link

Is it possible to generate completion for `argparse.Namespace`? #1980

Closed Felixoid closed 8 months ago

Felixoid commented 8 months ago

Hello!

For a long time, I have struggled with the result of argparse.ArgumentParser().parse_args() aka argparse.Namespace does not have completion for its attributes. Here's an example of the problematic script:

#!/usr/bin/env python

import argparse

parser = argparse.ArgumentParser(
    formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    description="dummy",
)
parser.add_argument("--token", help="github token")
parser.add_argument(
    "--repo", default="owner/repo1", help="repo owner/name"
)
parser.add_argument(
    "--from-repo", default="owner/repo2",
)
parser.add_argument(
    "--pr-to-sync",
    type=int,
    help="if set, only this single PR will be synced",
)
parser.add_argument("--dry-run", action="store_true", help="do not create anything")

args = parser.parse_args()
args.

And here's the output for its completion:

In [1]: from pathlib import Path, PurePath

In [2]: import jedi

In [3]: script = Path('test.py').read_text()

In [4]: script
Out[4]: '#!/usr/bin/env python\n\nimport argparse\n\nparser = argparse.ArgumentParser(\n    formatter_class=argparse.ArgumentDefaultsHelpFormatter,\n    description="dummy",\n)\nparser.add_argument("--token", help="github token")\nparser.add_argument(\n    "--repo", default="owner/repo1", help="repo owner/name"\n)\nparser.add_argument(\n    "--from-repo", default="owner/repo2",\n)\nparser.add_argument(\n    "--pr-to-sync",\n    type=int,\n    help="if set, only this single PR will be synced",\n)\nparser.add_argument("--dry-run", action="store_true", help="do not create anything")\n\nargs = parser.parse_args()\nargs.'

In [5]: jedi.Script(script).complete()
Out[5]:
[<Completion: _get_args>,
 <Completion: _get_kwargs>,
 <Completion: __annotations__>,
 <Completion: __class__>,
 <Completion: __contains__>,
 <Completion: __delattr__>,
 <Completion: __dict__>,
 <Completion: __dir__>,
 <Completion: __doc__>,
 <Completion: __eq__>,
 <Completion: __format__>,
 <Completion: __getattr__>,
 <Completion: __getattribute__>,
 <Completion: __hash__>,
 <Completion: __init__>,
 <Completion: __init_subclass__>,
 <Completion: __module__>,
 <Completion: __ne__>,
 <Completion: __new__>,
 <Completion: __reduce__>,
 <Completion: __reduce_ex__>,
 <Completion: __repr__>,
 <Completion: __setattr__>,
 <Completion: __sizeof__>,
 <Completion: __slots__>,
 <Completion: __str__>]

I understand it's because the attributes are added only in runtime, and the suggestion to use another library is given on the stackexchange.

But still, it's very inconvenient to have expected attributes token, repo, from-repo, pr-to-sync missed.

davidhalter commented 8 months ago

While this is not impossible if someone wrote a plugin for this, I don't think we will implement this. It's simply not worth the costs. Sorry.

Felixoid commented 8 months ago

I'd like to dig it a little bit. What's the right direction? Maybe some similar task is already done?

davidhalter commented 8 months ago

It's very hard to guide this one. But note that I won't merge this, if it's not very high quality, because I want to avoid the burden of maintenance for not so decent code.

I really don't have a very clear idea how this should be done. I just know that it's doable.

Felixoid commented 8 months ago

ok, I feel I need to take a look on https://github.com/davidhalter/jedi/tree/master/jedi/plugins

davidhalter commented 8 months ago

Yes that might for sure help, but I doubt that's enough.