Skip to content

Commit da2f981

Browse files
committed
docs: formatting
1 parent f8ee0c0 commit da2f981

File tree

1 file changed

+43
-42
lines changed

1 file changed

+43
-42
lines changed

README.md

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -344,59 +344,60 @@ dump_dict = {"endpoint": "some_api_endpoint", "data": {"foo": 1, "bar": "2"}, "u
344344
1. You can enforce to always raise an error by setting the `undefined` keyword to `Undefined.RAISE`
345345
(`'RAISE'` as a case-insensitive string works as well). Of course it works normally if you don't pass any undefined parameters.
346346

347-
```python
348-
from dataclasses_json import Undefined
349-
350-
@dataclass_json(undefined=Undefined.RAISE)
351-
@dataclass()
352-
class ExactAPIDump:
353-
endpoint: str
354-
data: Dict[str, Any]
355-
356-
dump = ExactAPIDump.from_dict(dump_dict) # raises UndefinedParameterError
357-
```
347+
```python
348+
from dataclasses_json import Undefined
349+
350+
@dataclass_json(undefined=Undefined.RAISE)
351+
@dataclass()
352+
class ExactAPIDump:
353+
endpoint: str
354+
data: Dict[str, Any]
355+
356+
dump = ExactAPIDump.from_dict(dump_dict) # raises UndefinedParameterError
357+
```
358358

359359
2. You can simply ignore any undefined parameters by setting the `undefined` keyword to `Undefined.EXCLUDE`
360360
(`'EXCLUDE'` as a case-insensitive string works as well). Note that you will not be able to retrieve them using `to_dict`:
361361

362-
```python
363-
from dataclasses_json import Undefined
364-
365-
@dataclass_json(undefined=Undefined.EXCLUDE)
366-
@dataclass()
367-
class DontCareAPIDump:
368-
endpoint: str
369-
data: Dict[str, Any]
370-
371-
dump = DontCareAPIDump.from_dict(dump_dict) # DontCareAPIDump(endpoint='some_api_endpoint', data={'foo': 1, 'bar': '2'})
372-
dump.to_dict() # {"endpoint": "some_api_endpoint", "data": {"foo": 1, "bar": "2"}}
373-
```
362+
```python
363+
from dataclasses_json import Undefined
364+
365+
@dataclass_json(undefined=Undefined.EXCLUDE)
366+
@dataclass()
367+
class DontCareAPIDump:
368+
endpoint: str
369+
data: Dict[str, Any]
370+
371+
dump = DontCareAPIDump.from_dict(dump_dict) # DontCareAPIDump(endpoint='some_api_endpoint', data={'foo': 1, 'bar': '2'})
372+
dump.to_dict() # {"endpoint": "some_api_endpoint", "data": {"foo": 1, "bar": "2"}}
373+
```
374374

375375
3. You can save them in a catch-all field and do whatever needs to be done later. Simply set the `undefined`
376376
keyword to `Undefined.INCLUDE` (`'INCLUDE'` as a case-insensitive string works as well) and define a field
377377
of type `CatchAll` where all unknown values will end up.
378378
This simply represents a dictionary that can hold anything.
379379
If there are no undefined parameters, this will be an empty dictionary.
380380

381-
```python
382-
from dataclasses_json import Undefined, CatchAll
383-
384-
@dataclass_json(undefined=Undefined.INCLUDE)
385-
@dataclass()
386-
class UnknownAPIDump:
387-
endpoint: str
388-
data: Dict[str, Any]
389-
unknown_things: CatchAll
390-
391-
dump = UnknownAPIDump.from_dict(dump_dict) # UnknownAPIDump(endpoint='some_api_endpoint', data={'foo': 1, 'bar': '2'}, unknown_things={'undefined_field_name': [1, 2, 3]})
392-
dump.to_dict() # {'endpoint': 'some_api_endpoint', 'data': {'foo': 1, 'bar': '2'}, 'undefined_field_name': [1, 2, 3]}
393-
```
394-
395-
- When using `Undefined.INCLUDE`, an `UndefinedParameterError` will be raised if you don't specify
396-
exactly one field of type `CatchAll`.
397-
- Note that `LetterCase` does not affect values written into the `CatchAll` field, they will be as they are given.
398-
- When specifying a default (or a default factory) for the the `CatchAll`-field, e.g. `unknown_things: CatchAll = None`, the default value will be used instead of an empty dict if there are no undefined parameters.
399-
- Calling __init__ with non-keyword arguments resolves the arguments to the defined fields and writes everything else into the catch-all field.
381+
```python
382+
from dataclasses_json import Undefined, CatchAll
383+
384+
@dataclass_json(undefined=Undefined.INCLUDE)
385+
@dataclass()
386+
class UnknownAPIDump:
387+
endpoint: str
388+
data: Dict[str, Any]
389+
unknown_things: CatchAll
390+
391+
dump = UnknownAPIDump.from_dict(dump_dict) # UnknownAPIDump(endpoint='some_api_endpoint', data={'foo': 1, 'bar': '2'}, unknown_things={'undefined_field_name': [1, 2, 3]})
392+
dump.to_dict() # {'endpoint': 'some_api_endpoint', 'data': {'foo': 1, 'bar': '2'}, 'undefined_field_name': [1, 2, 3]}
393+
```
394+
395+
Notes:
396+
- When using `Undefined.INCLUDE`, an `UndefinedParameterError` will be raised if you don't specify
397+
exactly one field of type `CatchAll`.
398+
- Note that `LetterCase` does not affect values written into the `CatchAll` field, they will be as they are given.
399+
- When specifying a default (or a default factory) for the the `CatchAll`-field, e.g. `unknown_things: CatchAll = None`, the default value will be used instead of an empty dict if there are no undefined parameters.
400+
- Calling __init__ with non-keyword arguments resolves the arguments to the defined fields and writes everything else into the catch-all field.
400401

401402
4. All 3 options work as well using `schema().loads` and `schema().dumps`, as long as you don't overwrite it by specifying `schema(unknown=<a marshmallow value>)`.
402403
marshmallow uses the same 3 keywords ['include', 'exclude', 'raise'](https://marshmallow.readthedocs.io/en/stable/quickstart.html#handling-unknown-fields).

0 commit comments

Comments
 (0)