1- from typing import Dict , Optional , Sequence , Type , Union
1+ from collections . abc import Sequence
22
33from odin import ResourceAdapter , resources
44from odin .exceptions import CodecDecodeError
@@ -21,10 +21,10 @@ def load(fp, resource=None, full_clean=True, default_to_not_supplied=False):
2121 """
2222 Load a resource from a TOML encoded file.
2323
24- If a ``resource`` value is supplied it is used as the base resource for the supplied YAML. If one is not supplied a
25- resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be
26- raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file
27- to be a child object from within the inheritance tree.
24+ If a ``resource`` value is supplied it is used as the base resource for the supplied YAML. If one is not
25+ supplied, a resource type field ``$`` is used to obtain the type represented by the dictionary. A
26+ ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a
27+ type to be supplied in the file to be a child object from within the inheritance tree.
2828
2929 :param fp: a file pointer to read TOML data format.
3030 :param resource: A resource type, resource name or list of resources and names to use as the base for creating a
@@ -53,10 +53,10 @@ def load(fp, resource=None, full_clean=True, default_to_not_supplied=False):
5353def loads (s , resource = None , full_clean = True , default_to_not_supplied = False ):
5454 """Load a resource from a TOML encoded string.
5555
56- If a ``resource`` value is supplied it is used as the base resource for the supplied YAML. If one is not supplied a
57- resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be
58- raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file
59- to be a child object from within the inheritance tree.
56+ If a ``resource`` value is supplied it is used as the base resource for the supplied YAML. If one is not
57+ supplied, a resource type field ``$`` is used to obtain the type represented by the dictionary. A
58+ ``ValidationError`` will be raised if either of these values are supplied and not compatible. It is valid for a
59+ type to be supplied in the file to be a child object from within the inheritance tree.
6060
6161 :param s: a string containing TOML.
6262 :param resource: A resource type, resource name or list of resources and names to use as the base for creating a
@@ -90,7 +90,7 @@ def __init__(
9090 include_virtual_fields : bool = True ,
9191 include_type_field : bool = True ,
9292 * args ,
93- ** kwargs
93+ ** kwargs ,
9494 ):
9595 super ().__init__ (* args , ** kwargs )
9696 self .include_virtual_fields = include_virtual_fields
@@ -104,7 +104,7 @@ def resource_to_dict(self, v):
104104 return resource_dict
105105
106106 def dump_value (self , v ):
107- if isinstance (v , ( ResourceBase , ResourceAdapter ) ):
107+ if isinstance (v , ResourceBase | ResourceAdapter ):
108108 resource_dict = self .resource_to_dict (v )
109109 return self .dump_inline_table (resource_dict )
110110
@@ -113,21 +113,21 @@ def dump_value(self, v):
113113 return super ().dump_value (v )
114114
115115
116- RT = Union [
117- ResourceBase ,
118- ResourceAdapter ,
119- Sequence [ResourceBase ],
120- Sequence [ResourceAdapter ],
121- Dict ,
122- ]
116+ RT = (
117+ ResourceBase
118+ | ResourceAdapter
119+ | Sequence [ResourceBase ]
120+ | Sequence [ResourceAdapter ]
121+ | dict ,
122+ )
123123
124124
125125def dump (
126126 resource : RT ,
127127 fp ,
128- encoder : Optional [ Type [ OdinEncoder ]] = None ,
128+ encoder : type [ OdinEncoder ] | None = None ,
129129 include_virtual_fields : bool = True ,
130- ** kwargs
130+ ** kwargs ,
131131):
132132 """Dump to a TOML encoded file.
133133
@@ -139,17 +139,17 @@ def dump(
139139 """
140140 encoder = (encoder or OdinEncoder )(include_virtual_fields , ** kwargs )
141141
142- if isinstance (resource , ( ResourceBase , ResourceAdapter ) ):
142+ if isinstance (resource , ResourceBase | ResourceAdapter ):
143143 resource = encoder .resource_to_dict (resource )
144144
145145 toml .dump (resource , fp , encoder )
146146
147147
148148def dumps (
149149 resource : RT ,
150- encoder : Optional [ Type [ OdinEncoder ]] = None ,
150+ encoder : type [ OdinEncoder ] | None = None ,
151151 include_virtual_fields : bool = True ,
152- ** kwargs
152+ ** kwargs ,
153153) -> str :
154154 """Dump to a TOML encoded file.
155155
@@ -161,7 +161,7 @@ def dumps(
161161 """
162162 encoder = (encoder or OdinEncoder )(include_virtual_fields , ** kwargs )
163163
164- if isinstance (resource , ( ResourceBase , ResourceAdapter ) ):
164+ if isinstance (resource , ResourceBase | ResourceAdapter ):
165165 resource = encoder .resource_to_dict (resource )
166166
167167 return toml .dumps (resource , encoder )
0 commit comments