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

Create an Object with fields as arguments #79

Open
arthur-proglove opened this issue Oct 29, 2020 · 5 comments
Open

Create an Object with fields as arguments #79

arthur-proglove opened this issue Oct 29, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@arthur-proglove
Copy link

It would be nice to be able to instantiate a model with some fields the same way a dataclass can be created and be able to use it to output a JSON dict.

class MySuperModel(Object):
    some_field: str = Property(String(enum=['foo', 'bar', 'baz']), required=True)


s = MySuperModel(some_field='foo')
print(s)

{
    'some_field': 'foo'
}
@arthur-proglove arthur-proglove added the enhancement New feature or request label Oct 29, 2020
@jacksmith15
Copy link
Owner

jacksmith15 commented Oct 29, 2020

I don't intend to add support for keyword arguments as the fields. Is there anything this prevents you from doing? If you replace some_field="foo" in the above example with dict(some_field="foo") then you can instantiate your model.

Instance conversion to a dictionary is a different story - if you could open a separate issue for that I would be grateful.

@arthur-proglove
Copy link
Author

arthur-proglove commented Oct 29, 2020

Yeah, this is what I am doing for now, I create my object this way

MySuperModel({'some_field': 'foo'})

which is basically the same as you propose, it works but I found it less convenient as using arguments, especially for nested objects.

I find this easier to instantiate to avoid errors at runtime and sport them with a linter:

MySuperModel(
   some_field='foo',
   nested_object=NestedObject(some_other_field='bar')
)

than this:

MySuperModel({
   'some_field': 'foo',
   'nested_object': {
    'some_other_field': 'bar'
  }
})

@arthur-proglove
Copy link
Author

Regarding the conversion to a dictionary it's anyway not an issue for now as converting a dict to json is fairly easy :-) since the Object is anyway created from a dict already.

@jacksmith15
Copy link
Owner

jacksmith15 commented Oct 29, 2020

I'm afraid even if the arguments were keyword arguments, I still don't think a linter could detect an issue there. If you can suggest a way to do that, I would certainly consider it.

The problem is signalling to a linter/static type checker:

  1. which arguments are allowed from properties
  2. which arguments are allowed via patternProperties
  3. whether additional properties are allowed

And all of the other variations supported by the JSON Schema spec.

@arthur-proglove
Copy link
Author

I agree with you it's an easy task, I'm not able to provide a solution here.

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

No branches or pull requests

2 participants