Skip to content

Commit bcb54fb

Browse files
authored
Merge pull request #114 from python-odin/release/1.7.0
Release/1.7.0
2 parents 825523f + 7c1a7b6 commit bcb54fb

File tree

7 files changed

+225
-112
lines changed

7 files changed

+225
-112
lines changed

HISTORY

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
1.7.0
2+
=====
3+
4+
- Migrate from collections.Iterable to typeing.Iterable to remove a warning
5+
- Add user defined metadata to Resource.Meta object
6+
- Add a reserved word guard, starting with fields (using fields causes clean_fields to go into a infinite recursion loop)
7+
18
1.6.2
29
=====
310

docs/ref/resources/options.rst

+12
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,15 @@ Meta Options
8484
class Meta:
8585
field_sorting = sort_by_name
8686

87+
88+
``user_data``
89+
Additional data that can be added to metadata. This can be used to provide additional
90+
parameters beyond those supported by odin for a custom application use-case.
91+
92+
For example::
93+
94+
class MyResource(Resource):
95+
class Meta:
96+
user_data = {
97+
"custom": "my-custom-value",
98+
}

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "odin"
3-
version = "1.6.2"
3+
version = "1.7.0"
44
description = "Data-structure definition/validation/traversal, mapping and serialisation toolkit for Python"
55
authors = ["Tim Savage <tim@savage.company>"]
66
license = "BSD-3-Clause"

src/odin/exceptions.py

+6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ class ResourceException(ValidationError):
8080
"""
8181

8282

83+
class ResourceDefError(Exception):
84+
"""
85+
Exceptions raised if a resource definition contains errors.
86+
"""
87+
88+
8389
class MappingError(Exception):
8490
"""
8591
Exceptions related to mapping, will typically be a more specific `MappingSetupError` or `MappingExecutionError`.

src/odin/mapping/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
import collections
33
import six
4+
import typing
45
from odin import bases
56
from odin import registration
67
from odin.fields import NotProvided
@@ -624,7 +625,7 @@ def _apply_rule(self, mapping_rule):
624625
)
625626

626627
if to_list:
627-
if isinstance(to_values, collections.Iterable):
628+
if isinstance(to_values, typing.Iterable):
628629
to_values = (list(to_values),)
629630
else:
630631
to_values = force_tuple(to_values)

src/odin/resources.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from odin import bases
77
from odin import exceptions, registration
8-
from odin.exceptions import ValidationError
8+
from odin.exceptions import ValidationError, ResourceDefError
99
from odin.fields import NotProvided
1010
from odin.utils import (
1111
lazy_property,
@@ -17,6 +17,11 @@
1717

1818
DEFAULT_TYPE_FIELD = "$"
1919

20+
# Set of field names that cannot be used
21+
RESERVED_FIELD_NAMES = {
22+
"fields", # Causes a infinite-recursion with clean_fields being a builtin method
23+
}
24+
2025

2126
class ResourceOptions(object):
2227
META_OPTION_NAMES = (
@@ -31,6 +36,7 @@ class ResourceOptions(object):
3136
"key_field_name",
3237
"key_field_names",
3338
"field_sorting",
39+
"user_data",
3440
)
3541

3642
def __init__(self, meta):
@@ -51,6 +57,7 @@ def __init__(self, meta):
5157
self.type_field = DEFAULT_TYPE_FIELD
5258
self.key_field_names = None
5359
self.field_sorting = NotProvided
60+
self.user_data = None
5461

5562
self._cache = {}
5663

@@ -110,6 +117,11 @@ def add_field(self, field):
110117
"""
111118
Dynamically add a field.
112119
"""
120+
if field.attname in RESERVED_FIELD_NAMES:
121+
raise ResourceDefError(
122+
"`{}` is a reserved name. Use the `name` argument to specify "
123+
"the name used for serialisation.".format(field.attname)
124+
)
113125
self.fields.append(field)
114126
if field.key:
115127
self._add_key_field(field)

0 commit comments

Comments
 (0)