0

I have a problem with the semantic completion for specific class argparse.Namespace with YouCompleteMe.

Here's a minimal repro case I can provide:

# .vimrc

silent! call plug#begin()
Plug 'ycm-core/YouCompleteMe', { 'do': 'git submodule update --init --recursive && GOFLAGS=-modcacherw ./install.py' }
call plug#end()

set updatetime=1500
let g:ycm_python_binary_path = 'python3'

And then the following python code doesn't have the completion in the last line for the expected repo, from_repo, pr_to_sync and dry_run expected fields.

#!/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.

example

I feel it requires the runtime. How can I improve the situation?

2
  • "with YouCompleteMe". Use the plugin's issue tracker.
    – romainl
    Jan 15 at 12:17
  • I'm a bit confused by the screenshot. It looks to me that it does have the completion on the last line. Could you precise what you expect YouCompleteMe to propose? Jan 15 at 13:37

1 Answer 1

1

I believe this is the limit of the LSP.

The LSP does static analysis it doesn't properly execute your code.

In this case at line 24 it is not aware that 10, 13, 16 and 21 are executed and that the args object should have the the following members: repo, from_repo, pr_to_sync and dry_run.

It only knows that args is of type argparse.Namespace but that doesn't tell it what are the available fields.

It is a known weakness of static analysis and some have developed an alternative type-argument-parser to solve that problem.

A somehow equivalent of your code written using this parser would be:

from tap import Tap


class ArgumentParser(Tap):
    repo = "owner/repo1"
    """github token"""

    from_repo = "owner/repo2"
    """repo owner/name"""

    pr_to_sync: int
    """if set, only this single PR will be synced"""

    dry_run = False
    """do not create anything"""


parser = ArgumentParser()
args = parser.parse_args()
args.

In this version args is typed with the known attributes and the completion should work fine.

2
  • 1
    That's what I've been afraid of. Indeed, it looks like LSP (jedi in both cases) does not know about the object's attributes. The best next step is to spin the issue into the LSP repository, so here it is github.com/davidhalter/jedi/issues/1980. Thanks again!
    – Felixoid
    Jan 15 at 16:31
  • I have tested with pyright and the result is the same. I'm afraid the LSP analysis is not able to go that fast :-) Jan 15 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.