Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Allow annotations for specifying fields instead of class attributes #99

Closed
esoterra opened this issue Aug 1, 2019 · 3 comments · Fixed by #115
Closed

Allow annotations for specifying fields instead of class attributes #99

esoterra opened this issue Aug 1, 2019 · 3 comments · Fixed by #115
Labels
feature New feature or request

Comments

@esoterra
Copy link

esoterra commented Aug 1, 2019

The new typing.NamedTuple class offers a new way to specify named tuples using colon based type labels.

I would like this syntax to be available for defining model classes so that the Dog example in the README can be written like this

class Dog(Model):
     name: fields.Str()
     hates_cats: fields.Optional(fields.Bool, default=True)

Reference:
https://docs.python.org/3/library/typing.html#typing.NamedTuple

@rossmacarthur rossmacarthur added the feature New feature or request label Sep 11, 2019
@rossmacarthur
Copy link
Owner

rossmacarthur commented Sep 11, 2019

Thanks for the suggestion. This looks pretty easy to implement (they simply end up in a class attribute __annotations__). I think the only question that would need to be resolved is what happens in cases where both are used.

For example:

class Dog(Model):
    name: fields.Str()
    name = fields.Int()

or

class Dog(Model):
    name: fields.Str() = 'hello'

or even just a mix

class Dog(Model):
    name: fields.Str()
    hates_cats = fields.Bool()
  • If annotations are used should we ignore any further class attributes?
  • Does the one override the other? I.e. if annotations and class attributes assign an identically named attribute which has priority then?
  • Do we error if both are used?

@rossmacarthur rossmacarthur changed the title Suggestion: Allow colon ':' for specifying fields in addition to/instead of '=' Allow annotations for specifying fields instead of class attributes Sep 11, 2019
@rossmacarthur
Copy link
Owner

rossmacarthur commented Sep 16, 2019

Additionally I think it would be fine for these annotations used the same "resolving" of types and model classes into fields that are used for container fields like fields.Dict. So you would be able to do something like this

class Dog(Model):
    name: str
    hates_cats: fields.Optional(bool, default=True)

@esoterra
Copy link
Author

Thanks, this looks like it's headed where I was hoping it would.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants