jazzband / jazzband/jsonmodels

populate - arg throw_on_unknown_keyword

Open
#125 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
343
Forks
50
PR merge metrics
No merged PRs in 30d

Description

Hi,

I would like the `__init__` and `populate` methods [ref](https://github.com/beregond/jsonmodels/blob/master/jsonmodels/models.py#L32), to have the following signature:

```python
def __init__(self, throw_on_unknown_keyword = False, **kwargs):
# ...

def populate(self, throw_on_unknown_keyword = False, **values):
# ...
if structure_name in values:
field.__set__(self, values.pop(structure_name))
elif throw_on_unknown_keyword and structure_name != 'throw_on_unknown_keyword' :
throw # ...
for name, _, field in fields:
if name in values:
field.__set__(self, values.pop(name))
elif throw_on_unknown_keyword and name != 'throw_on_unknown_keyword':
throw # ...

# warning: I have not checked the details!
```

Reason: I wrap the Model inside a class:

```python

class A:
class ModelA(models.Base):
a = fields.DateTimeField(required=True)
b = fields.FloatField(required=True)
c = fields.FloatField(required=True)

def __init__(self, **kwargs):
self.model = self.ModelA(throw_on_unknown_keyword = True, **kwargs)

# #Instead of
#def __init__(self, a, b, c):
# self.model = self.ModelA(a = a, b = b, c = c)
```

With `throw_on_unknown_keyword = True`, I keep the constructor of A and the model synchronized, and get an exception if they are not synchronized.

Is that feasible?

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.