Skip to content

Commit f611c29

Browse files
committed
feat: use Mapping, not Dict for input arguments
Mapping imposes less restrictions on callers, because it's read-only and allows non-dict types to be passed without copying them as dict(), or passing dict-like values and ignoring the resulting type error. Signed-off-by: Hal Blackburn <hwtb2@cam.ac.uk>
1 parent 029d122 commit f611c29

File tree

8 files changed

+17
-11
lines changed

8 files changed

+17
-11
lines changed

cloudevents/abstract/event.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class CloudEvent:
3232
@classmethod
3333
def create(
3434
cls: typing.Type[AnyCloudEvent],
35-
attributes: typing.Dict[str, typing.Any],
35+
attributes: typing.Mapping[str, typing.Any],
3636
data: typing.Optional[typing.Any],
3737
) -> AnyCloudEvent:
3838
"""

cloudevents/conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def best_effort_encode_attribute_value(value: typing.Any) -> typing.Any:
260260

261261
def from_dict(
262262
event_type: typing.Type[AnyCloudEvent],
263-
event: typing.Dict[str, typing.Any],
263+
event: typing.Mapping[str, typing.Any],
264264
) -> AnyCloudEvent:
265265
"""
266266
Constructs an Event object of a given `event_type` from

cloudevents/http/conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def from_http(
5858

5959

6060
def from_dict(
61-
event: typing.Dict[str, typing.Any],
61+
event: typing.Mapping[str, typing.Any],
6262
) -> CloudEvent:
6363
"""
6464
Constructs a CloudEvent from a dict `event` representation.

cloudevents/http/event.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ class CloudEvent(abstract.CloudEvent):
3434

3535
@classmethod
3636
def create(
37-
cls, attributes: typing.Dict[str, typing.Any], data: typing.Optional[typing.Any]
37+
cls,
38+
attributes: typing.Mapping[str, typing.Any],
39+
data: typing.Optional[typing.Any],
3840
) -> "CloudEvent":
3941
return cls(attributes, data)
4042

41-
def __init__(self, attributes: typing.Dict[str, str], data: typing.Any = None):
43+
def __init__(self, attributes: typing.Mapping[str, str], data: typing.Any = None):
4244
"""
4345
Event Constructor
4446
:param attributes: a dict with cloudevent attributes. Minimally

cloudevents/pydantic/v1/conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def from_json(
6363

6464

6565
def from_dict(
66-
event: typing.Dict[str, typing.Any],
66+
event: typing.Mapping[str, typing.Any],
6767
) -> CloudEvent:
6868
"""
6969
Construct an CloudEvent from a dict `event` representation.

cloudevents/pydantic/v1/event.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ class CloudEvent(abstract.CloudEvent, BaseModel): # type: ignore
100100

101101
@classmethod
102102
def create(
103-
cls, attributes: typing.Dict[str, typing.Any], data: typing.Optional[typing.Any]
103+
cls,
104+
attributes: typing.Mapping[str, typing.Any],
105+
data: typing.Optional[typing.Any],
104106
) -> "CloudEvent":
105107
return cls(attributes, data)
106108

@@ -155,7 +157,7 @@ def create(
155157

156158
def __init__( # type: ignore[no-untyped-def]
157159
self,
158-
attributes: typing.Optional[typing.Dict[str, typing.Any]] = None,
160+
attributes: typing.Optional[typing.Mapping[str, typing.Any]] = None,
159161
data: typing.Optional[typing.Any] = None,
160162
**kwargs,
161163
):

cloudevents/pydantic/v2/conversion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def from_json(
6464

6565

6666
def from_dict(
67-
event: typing.Dict[str, typing.Any],
67+
event: typing.Mapping[str, typing.Any],
6868
) -> CloudEvent:
6969
"""
7070
Construct an CloudEvent from a dict `event` representation.

cloudevents/pydantic/v2/event.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class CloudEvent(abstract.CloudEvent, BaseModel): # type: ignore
4444

4545
@classmethod
4646
def create(
47-
cls, attributes: typing.Dict[str, typing.Any], data: typing.Optional[typing.Any]
47+
cls,
48+
attributes: typing.Mapping[str, typing.Any],
49+
data: typing.Optional[typing.Any],
4850
) -> "CloudEvent":
4951
return cls(attributes, data)
5052

@@ -103,7 +105,7 @@ def create(
103105

104106
def __init__( # type: ignore[no-untyped-def]
105107
self,
106-
attributes: typing.Optional[typing.Dict[str, typing.Any]] = None,
108+
attributes: typing.Optional[typing.Mapping[str, typing.Any]] = None,
107109
data: typing.Optional[typing.Any] = None,
108110
**kwargs,
109111
):

0 commit comments

Comments
 (0)