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

Overriding Schema instantiation. #34

Open
tomchristie opened this issue Mar 5, 2019 · 2 comments
Open

Overriding Schema instantiation. #34

tomchristie opened this issue Mar 5, 2019 · 2 comments

Comments

@tomchristie
Copy link
Member

Eg:

  1. Use a schema class, but just instantiate a plain dict.
  2. Use a schema class, but instantiate a different type. (Eg. seperate schema from model class)
@skewty
Copy link
Contributor

skewty commented May 12, 2019

Is one of the purposes of this issue / feature request to provide type hinting in VSCode and PyCharm?

from datetime import date
import typesystem

class Artist(typesystem.Schema):
    name = typesystem.String(max_length=100)  # type: str

class Album(typesystem.Schema):
    title = typesystem.String(max_length=100)  # type: str
    release_date = typesystem.Date()  # type: date
    artist = typesystem.Reference(Artist)  # type: Artist

album = Album.validate({
    "title": "Double Negative",
    "release_date": "2018-09-14",
    "artist": {"name": "Low"}
})

album.artist.name  # the expected resultant type of name is str but the inspector shows Field unless I add the # type as above.

Is your issue about making Field class more of a mix-in (or use composition instead of inheritance)?

I am interested because this lack of correct type being provided by the inspector is a deal breaker for my team. The #type type override is perhaps an acceptable band-aid though as ugly as it is.

NOTE: pydantic, although not nearly as elegant and simple to use as typesystem in my opinion, provides the expected field type to the inspector by using the plain type. Perhaps that project should be mentioned in the Alternatives section of README.md.

Thanks!

@tomchristie
Copy link
Member Author

Is one of the purposes of this issue / feature request to provide type hinting in VSCode and PyCharm?

Not specficially, but I guess that's a bit related. I'm just not convinced by my earlier design decision to have the schemas be instances themselves. I think it'd likely be neater if they were just that classes that provided the validate/serialize behavior. (And have the actually instances themselves be whatever actual type you want to use for the models)

Btw. Very welcome to add pydantic to the relevant README section if you'd like, yup.

polyrand added a commit to polyrand/typesystem that referenced this issue Apr 4, 2020
Also mentioned in encode#34.
tomchristie pushed a commit that referenced this issue Apr 7, 2020
Also mentioned in #34.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants