BX21
BX21

Reputation: 461

How does VS Code get type hints?

I was using argparse in a project when I noticed the following:

How would I write a function in my own code that shows type hints in the same way (i.e. the function I write has *args and **kwargs as the arguments, but any user of the function can see the names and types of the expected kwargs)?

Upvotes: 4

Views: 3031

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 122154

That type information comes from the typeshed:

Typeshed contains external type annotations for the Python standard library and Python builtins, as well as third party packages as contributed by people external to those projects.

This data can e.g. be used for static analysis, type checking or type inference.

You can see the specific types for add_argument here:

def add_argument(self,
                 *name_or_flags: Text,
                 action: Union[Text, Type[Action]] = ...,
                 nargs: Union[int, Text] = ...,
                 const: Any = ...,
                 default: Any = ...,
                 type: Union[Callable[[Text], _T], Callable[[str], _T], FileType] = ...,
                 choices: Iterable[_T] = ...,
                 required: bool = ...,
                 help: Optional[Text] = ...,
                 metavar: Optional[Union[Text, Tuple[Text, ...]]] = ...,
                 dest: Optional[Text] = ...,
                 version: Text = ...,
                 **kwargs: Any) -> Action: ...

For your own code, you can just add those type hints in directly, unless you need to support older versions of Python (syntax available from 3.0 per PEP-3107, typing module available from 3.5 per PEP-0484).

Upvotes: 5

Related Questions