davidhalter / davidhalter/jedi

Inference of typing.Annotated

Open
#1,747 3 comments 0 reactions 0 assignees View on GitHub
feature low-prio
Dominant language
Python
Stars
6.2k
Forks
535
PR merge metrics
No merged PRs in 30d

Description

Hey!

I've got a project where I'm looking at using the [`Annotated` type annotation](https://docs.python.org/3/library/typing.html#typing.Annotated) which was new in Python 3.9 (and backported through typing_extensions).

> ### typing.Annotated
> A type, introduced in PEP 593 (Flexible function and variable annotations), to decorate existing types with context-specific metadata (possibly multiple pieces of it, as Annotated is variadic). Specifically, a type `T` can be annotated with metadata `x` via the typehint `Annotated[T, x]`. This metadata can be used for either static analysis or at runtime. If a library (or tool) encounters a typehint `Annotated[T, x]` and has no special logic for metadata `x`, it should ignore it and simply treat the type as `T`.

I'd expect Jedi to take the naive route and go with "simply treat the type as `T`". However it's not something which Jedi handles at present as I've confirmed with a fresh clone of the repo today.

How might I go about adding support for this in Jedi? I'm new to this codebase so some pointers would be appreciated.

Or is this even a Jedi thing in the first place? is something else missing a definition about `Annotated`?

I was looking at how to implement it and ended up starting with a basic test which fails as expected. Does this look right as a starting point?

```
from typing import Annotated

# This is just a dummy and very meaningless thing to use with to the Annotated
# type hint
class Foo:
pass

class A:
pass

def annotated_function_params(
basic: Annotated[str, Foo()],
obj: A,
annotated_obj: Annotated[A, Foo()],
):
#? str
basic

#? A()
obj

#? A()
annotated_obj
```

The first and third tests there fail as expected with this sort of message:

```
E AssertionError:
E Test failed.
E actual =
E set()
E desired =
E {'completion.pep0593_annotations.A()'}
E
E assert set() == {'completion....otations.A()'}
E Extra items in the right set:
E 'completion.pep0593_annotations.A()'
E Full diff:
E - {'completion.pep0593_annotations.A()'}
E + set()
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.