Skip to content
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

Type inference override #765

Closed
nemec opened this issue Dec 22, 2020 · 4 comments
Closed

Type inference override #765

nemec opened this issue Dec 22, 2020 · 4 comments
Labels
waiting for user response Requires more information from user

Comments

@nemec
Copy link

nemec commented Dec 22, 2020

I'm not sure if this is a bug or a feature request, but in order to support auto-completion for argparse (see #628) in other language servers (the default vscode one, Microsoft I think?) I like to create a marker class defining the acceptable properties and then manually annotate the type signature of the parse_args method with the class, so it's used when auto-completing. Pylance, however, ignores my manual annotation and continues to assume the type is the return type from parse_args (which is Namespace).

Note: I don't expect Pylance to special-case code for argparse. I simply want it to use my Args class as the type for all inference decisions, even if I make a mistake and my Args class gets out of sync with how I've actually configured my argparse parser.

This is a minimal reproduction:

import argparse

class Args:
    command: bool = None

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--command', action='store_true')
    args: Args = parser.parse_args()

The inferred type is Namespace, but it would be nice if it said Args (however, the default language server also shows Namespace despite the annotation, so this is less important than auto-complete support):
Screenshot from 2020-12-22 03-32-06

This is the auto-complete output from the Pylance language server. There is no suggestion for command.
Screenshot from 2020-12-22 03-32-23

This is the auto-complete output from the default language server, which includes command as a suggestion:
Screenshot from 2020-12-22 03-35-06

@erictraut
Copy link
Contributor

There's no type inference applied when evaluating the type of the expression parser.parse_args(). Inference is used only in cases where a type declaration (i.e. an explicit annotation) is not present, and the type checker needs to infer the type based on other contextual information. In this case, the types are explicitly declared in the argparse.pyi stub file.

The problem here is that you are not specifying a namespace argument to parse_args(). With the following small change, it will work as you are expecting:

args = parser.parse_args(namespace=Args())

@judej judej added the waiting for user response Requires more information from user label Dec 22, 2020
@github-actions github-actions bot removed the triage label Dec 22, 2020
@nemec
Copy link
Author

nemec commented Dec 23, 2020

I see, thank you. That does set the tooltip properly.

Maybe this is beyond the scope, but do you know if it's possible to do this with multiple subparsers (e.g. git add becomes AddArgs(), git commit becomes CommitArgs()). This was easy enough to do after the fact by manually "casting" with an annotation, but with the parse_args solution I won't know the true type until after the arguments are parsed.

@erictraut
Copy link
Contributor

Sorry, but the only thing I know about parse_args is from the research I did to answer this question. You'd have to ask someone who is more familiar with this interface.

@nemec
Copy link
Author

nemec commented Dec 23, 2020

I appreciate the help anyway! I guess it's not possible to override the type that Pylance thinks a variable is, then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for user response Requires more information from user
Projects
None yet
Development

No branches or pull requests

3 participants