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

Feature Request: Support for typing_extensions.Annotated (from PEP 593) #837

Closed
dmontagu opened this issue Sep 26, 2019 · 11 comments
Closed

Comments

@dmontagu
Copy link
Contributor

Feature Request

Support for typing_extensions.Annotated (from PEP 593)

This could offer a nice way to solve the problem of assigning a Schema to a field without upsetting mypy, and without introducing an auxiliary function returning type Any (as discussed in #577):

Instead of

class MyModel(BaseModel):
    x: int = Schema(..., ge=0)
    y: str

The API could be:

from typing_extensions import Annotated

class MyModel(BaseModel):
    x: Annotated[int, Schema(ge=0)]
    y: str
  • Mypy support for this typing_extensions.Annotated was added in mypy 0.730.
  • See PEP 593 for more details about typing_extensions.Annotated.
@dmontagu
Copy link
Contributor Author

dmontagu commented Sep 26, 2019

Also note: mypy has typing_extensions as a dependency, so you wouldn't need to install any additional packages to use this capability to get mypy checks to pass.

(And as long as we don't drop support for the = Schema(..., ge=0) api, no existing code or currently-passing static checks should break.)

@samuelcolvin It might be worth considering this option more seriously prior to releasing V1.0, in case it allows us to drop the Field(...) function in #824.

@dmontagu dmontagu mentioned this issue Sep 26, 2019
4 tasks
@tiangolo
Copy link
Member

That's interesting. Although it makes the code more verbose and (subjectively) less intuitive.

I like that the = Field(...) is somewhat similar to dataclasses field() functions: https://docs.python.org/3/library/dataclasses.html#dataclasses.field

@dmontagu
Copy link
Contributor Author

@tiangolo I think that's a good point on both counts.

At any rate, I think it should be possible to handle this more completely by way of a mypy plugin (I'm already using the plugin I've opened a PR for in pydantic in all my projects, and I plan to get it into a release-worthy state as soon as v1.0 drops). Given the limited scope of issues that type-hinting as Any could cause, this seems like it may be preferable to supporting Annotated anyway.

That said, I think it may still make sense to support Annotated for compatibility with other frameworks that may choose to use it. I'd be happy to wait to implement it until someone requests it for more practical reasons though 😄.

@samuelcolvin
Copy link
Member

See my response on #824.

My only other question: is there any change whatsoever that the pep get's declined and we're left with unofficial syntax? I guess if that does happen and mypy stops supporting Annotated, we could change back in future.

@samuelcolvin
Copy link
Member

In summary I'm keen to implement this, I think it should be backwards compatible so can happen after v1.

As discussed on #824 and here, use of x: Whatever = Field(...) should continue to work and is likely to be the primary way most people use Field.

@tiangolo
Copy link
Member

In recent versions Field() is now a function, so mypy doesn't complain.

And @dmontagu 's mypy plugin is working great.

I would think this specific issue could be closed and we could open a new one if there's a new use case that benefits from Annotated, what do you think?

@mcgfeller
Copy link

mcgfeller commented Aug 15, 2020

PEP 593 is now in Python 3.9. Would this help to have better support for Pydantic in Pyright, Pylance and hence VS Code, making Option 2 of microsoft/python-language-server#1898 feasible?

Field information could be basically stored in Annotated, making Pydantic even more pythonic (and more pythonic than dataclasses).

Best regards, Martin

@pikeas
Copy link

pikeas commented Jan 14, 2021

Would love to see Pylance and Pydantic playing nicely together without the decorator hack!

@JacobHayes
Copy link
Contributor

@pikeas If you get a chance, try out #2147 and see if 1: it works and 2: Pylance plays nicely.

@pikeas
Copy link

pikeas commented Jan 15, 2021

@JacobHayes

# pip install git+https://github.com/samuelcolvin/pydantic.git@refs/pull/2147/head
from typing import Annotated
# also tried from typing_extensions import Annotated

class FooModel(BaseModel):
    value1: str
    value2: Annotated[str, []]

foo = FooModel()

No errors but Pylance still only sees **data: Any when hovering FooModel

@thomascobb
Copy link

As a test I put the decorator hack at the end of pydantic.dataclasses:

if TYPE_CHECKING:
    from dataclasses import dataclass

Now Pylance gives type hinting for this:

from pydantic.dataclasses import dataclass

@dataclass
class Foo:
    value1: str
    value2: Annotated[str, Field(description="Something else")]

f = Foo("boo", value2="foo")

All tests still pass, but I don't know if this has made mypy ignore any internal tests...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants