Skip to content

Commit f211c0a

Browse files
committed
Update typing on msgpack and yaml codecs
1 parent 36c8b23 commit f211c0a

2 files changed

Lines changed: 40 additions & 31 deletions

File tree

src/odin/codecs/msgpack_codec.py

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
"""Codec to load/save Message Pack (msgpack) documents."""
12
import datetime
23
import uuid
34
from typing import TextIO
@@ -9,7 +10,7 @@
910
"odin.codecs.msgpack_codec requires the 'msgpack-python' package."
1011
) # noqa
1112

12-
from odin import bases, Resource
13+
from odin import bases
1314
from odin import serializers, resources, ResourceAdapter
1415
from odin.utils import getmeta
1516

@@ -24,11 +25,9 @@
2425

2526

2627
class OdinPacker(msgpack.Packer):
27-
"""
28-
Encoder for Odin resources.
29-
"""
28+
"""Encoder for Odin resources."""
3029

31-
def __init__(self, include_virtual_fields=True, *args, **kwargs):
30+
def __init__(self, include_virtual_fields: bool = True, *args, **kwargs):
3231
kwargs.setdefault("default", self.default)
3332
super().__init__(*args, **kwargs)
3433
self.include_virtual_fields = include_virtual_fields
@@ -45,9 +44,13 @@ def default(self, o):
4544
return TYPE_SERIALIZERS[o.__class__](o)
4645

4746

48-
def load(fp, resource=None, full_clean=True, default_to_not_supplied=False):
49-
"""
50-
Load a from a MessagePack encoded file.
47+
def load(
48+
fp: TextIO,
49+
resource: resources.ResourceBase = None,
50+
full_clean: bool = True,
51+
default_to_not_supplied: bool = False,
52+
):
53+
"""Load a from a MessagePack encoded file.
5154
5255
See :py:meth:`loads` for more details of the loading operation.
5356
@@ -62,9 +65,13 @@ def load(fp, resource=None, full_clean=True, default_to_not_supplied=False):
6265
)
6366

6467

65-
def loads(s, resource=None, full_clean=True, default_to_not_supplied=False):
66-
"""
67-
Load from a MessagePack encoded string/bytes.
68+
def loads(
69+
s: str,
70+
resource: resources.ResourceBase = None,
71+
full_clean: bool = True,
72+
default_to_not_supplied: bool = False,
73+
):
74+
"""Load from a MessagePack encoded string/bytes.
6875
6976
If a ``resource`` value is supplied it is used as the base resource for the supplied MessagePack data. I one is not
7077
supplied a resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError``
@@ -83,14 +90,13 @@ def loads(s, resource=None, full_clean=True, default_to_not_supplied=False):
8390

8491

8592
def dump(
86-
resource, # type: Resource
87-
fp, # type: TextIO,
93+
resource: resources.ResourceBase,
94+
fp: TextIO,
8895
cls=OdinPacker,
89-
include_virtual_fields=True, # type: bool
96+
include_virtual_fields: bool = True,
9097
**kwargs
9198
):
92-
"""
93-
Dump to a MessagePack encoded file.
99+
"""Dump to a MessagePack encoded file.
94100
95101
:param include_virtual_fields:
96102
:param resource: The root resource to dump to a MessagePack encoded file.
@@ -101,13 +107,12 @@ def dump(
101107

102108

103109
def dumps(
104-
resource, # type: Resource
110+
resource: resources.ResourceBase,
105111
cls=OdinPacker,
106-
include_virtual_fields=True, # type: bool
112+
include_virtual_fields: bool = True,
107113
**kwargs
108114
):
109-
"""
110-
Dump to a MessagePack encoded string.
115+
"""Dump to a MessagePack encoded string.
111116
112117
:param include_virtual_fields:
113118
:param resource: The root resource to dump to a MessagePack encoded file.

src/odin/codecs/yaml_codec.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Codec to load/save Yaml documents."""
2+
from typing import TextIO, Union
13
from io import StringIO
24

35
from odin import bases
@@ -26,8 +28,8 @@ class OdinDumper(SafeDumper):
2628
def __init__(
2729
self,
2830
stream,
29-
include_virtual_fields=True,
30-
include_type_field=True,
31+
include_virtual_fields: bool = True,
32+
include_type_field: bool = True,
3133
*args,
3234
**kwargs
3335
):
@@ -48,9 +50,13 @@ def represent_resource(self, data):
4850
OdinDumper.add_multi_representer(bases.ResourceIterable, OdinDumper.represent_list)
4951

5052

51-
def load(fp, resource=None, full_clean=True, default_to_not_supplied=False):
52-
"""
53-
Load a resource from a YAML encoded file.
53+
def load(
54+
fp: Union[TextIO, str],
55+
resource: resources.ResourceBase = None,
56+
full_clean: bool = True,
57+
default_to_not_supplied: bool = False,
58+
):
59+
"""Load a resource from a YAML encoded file.
5460
5561
If a ``resource`` value is supplied it is used as the base resource for the supplied YAML. I one is not supplied a
5662
resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be
@@ -79,9 +85,8 @@ def load(fp, resource=None, full_clean=True, default_to_not_supplied=False):
7985
loads = load
8086

8187

82-
def dump(resource, fp, dumper=OdinDumper, **kwargs):
83-
"""
84-
Dump to a YAML encoded file.
88+
def dump(resource: resources.ResourceBase, fp: TextIO, dumper=OdinDumper, **kwargs):
89+
"""Dump to a YAML encoded file.
8590
8691
:param resource: The root resource to dump to a YAML encoded file.
8792
:param dumper: Dumper to use serializing to a string; default is the :py:class:`OdinDumper`.
@@ -94,9 +99,8 @@ def dump(resource, fp, dumper=OdinDumper, **kwargs):
9499
raise CodecEncodeError(str(ex))
95100

96101

97-
def dumps(resource, dumper=OdinDumper, **kwargs):
98-
"""
99-
Dump to a YAML encoded string.
102+
def dumps(resource: resources.ResourceBase, dumper=OdinDumper, **kwargs) -> str:
103+
"""Dump to a YAML encoded string.
100104
101105
:param resource: The root resource to dump to a YAML encoded file.
102106
:param dumper: Dumper to use serializing to a string; default is the :py:class:`OdinDumper`.

0 commit comments

Comments
 (0)