55import re
66import uuid
77from functools import cached_property
8- from typing import Sequence , Tuple , Any , TypeVar , Optional , Type
8+ from typing import Sequence , Tuple , Any , TypeVar , Optional , Type , Dict
99
1010from odin import exceptions , datetimeutil , registration
1111from odin .utils import getmeta
2424
2525__all__ = (
2626 "NotProvided" ,
27+ "NotProvidedType" ,
2728 "BaseField" ,
2829 "Field" ,
2930 "BooleanField" ,
@@ -61,14 +62,14 @@ class NotProvided:
6162 pass
6263
6364
65+ NotProvidedType = Type [NotProvided ]
66+
6467# Backwards compatibility
6568NOT_PROVIDED = NotProvided
6669
6770
6871class Field (BaseField ):
69- """
70- Base class for fields.
71- """
72+ """Base class for fields."""
7273
7374 default_validators = []
7475 default_error_messages = {
@@ -102,7 +103,7 @@ def __init__(
102103 default = NotProvided ,
103104 help_text : str = "" ,
104105 validators : Sequence = None ,
105- error_messages = None ,
106+ error_messages : Dict [ str , str ] = None ,
106107 is_attribute : bool = False ,
107108 doc_text : str = "" ,
108109 key : bool = False ,
@@ -159,33 +160,32 @@ def __deepcopy__(self, memodict):
159160
160161 @cached_property
161162 def choice_values (self ):
162- """
163- Choice values to allow choices to simplify checking if a choice is valid.
164- """
163+ """Choice values to allow choices to simplify checking if a choice is valid."""
165164 if self .choices is not None :
166165 return tuple (c [0 ] for c in self .choices )
167166
168167 @property
169168 def choices_doc_text (self ) -> Sequence [Tuple [str , str ]]:
170- """
171- Choices converted for documentation purposes.
172- """
169+ """Choices converted for documentation purposes."""
173170 return self .choices
174171
175- def contribute_to_class (self , cls , name ):
176- self .set_attributes_from_name (name )
172+ def contribute_to_class (self , cls , name : str ):
173+ """Contribute value this field to a resource class."""
174+ meta = getmeta (cls )
175+ self .set_attributes_from_name (name , meta .field_name_format )
177176 self .resource = cls
178- getmeta ( cls ) .add_field (self )
177+ meta .add_field (self )
179178
180179 def to_python (self , value ):
181180 """
182181 Converts the input value into the expected Python data type, raising
183- odin.exceptions.ValidationError if the data can't be converted.
182+ `` odin.exceptions.ValidationError`` if the data can't be converted.
184183 Returns the converted value. Subclasses should override this.
185184 """
186185 raise NotImplementedError ()
187186
188187 def run_validators (self , value ):
188+ """Execute validators against supplied value."""
189189 if value in self .empty_values :
190190 return
191191
@@ -200,6 +200,7 @@ def run_validators(self, value):
200200 raise exceptions .ValidationError (errors )
201201
202202 def validate (self , value ):
203+ """Validate a supplied value."""
203204 if (
204205 self .choice_values
205206 and (value not in self .empty_values )
@@ -225,25 +226,19 @@ def clean(self, value):
225226 return value
226227
227228 def has_default (self ):
228- """
229- Returns a bool of whether this field has a default value.
230- """
229+ """Returns a bool of whether this field has a default value."""
231230 return self .default is not NotProvided
232231
233232 def get_default (self ):
234- """
235- Returns the default value for this field.
236- """
233+ """Returns the default value for this field."""
237234 if self .has_default ():
238235 if callable (self .default ):
239236 return self .default ()
240237 return self .default
241238 return None
242239
243240 def value_to_object (self , obj , data ):
244- """
245- Assign a value to an object
246- """
241+ """Assign a value to an object."""
247242 setattr (obj , self .attname , data )
248243
249244
0 commit comments