8000 Improve public API type annotations & fix unit test type errors by h4l · Pull Request #248 · cloudevents/sdk-python · GitHub
[go: up one dir, main page]

Skip to content

Improve public API type annotations & fix unit test type errors #248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 23, 2025
Merged
Prev Previous commit
Next Next commit
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>
  • Loading branch information
h4l committed Mar 25, 2025
commit f611c299b6efb45c2ffe78b83ab22fb5dc1e0766
2 changes: 1 addition & 1 deletion cloudevents/abstract/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class CloudEvent:
@classmethod
def create(
cls: typing.Type[AnyCloudEvent],
attributes: typing.Dict[str, typing.Any],
attributes: typing.Mapping[str, typing.Any],
data: typing.Optional[typing.Any],
) -> AnyCloudEvent:
"""
Expand Down
2 changes: 1 addition & 1 deletion cloudevents/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def best_effort_encode_attribute_value(value: typing.Any) -> typing.Any:

def from_dict(
event_type: typing.Type[AnyCloudEvent],
event: typing.Dict[str, typing.Any],
event: typing.Mapping[str, typing.Any],
) -> AnyCloudEvent:
"""
Constructs an Event object of a given `event_type` from
Expand Down
2 changes: 1 addition & 1 deletion cloudevents/http/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def from_http(


def from_dict(
event: typing.Dict[str, typing.Any],
event: typing.Mapping[str, typing.Any],
) -> CloudEvent:
"""
Constructs a CloudEvent from a dict `event` representation.
Expand Down
6 changes: 4 additions & 2 deletions cloudevents/http/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ class CloudEvent(abstract.CloudEvent):

@classmethod
def create(
cls, attributes: typing.Dict[str, typing.Any], data: typing.Optional[typing.Any]
cls,
attributes: typing.Mapping[str, typing.Any],
data: typing.Optional[typing.Any],
) -> "CloudEvent":
return cls(attributes, data)

def __init__(self, attributes: typing.Dict[str, str], data: typing.Any = None):
def __init__(self, attributes: typing.Mapping[str, str], data: typing.Any = None):
"""
Event Constructor
:param attributes: a dict with cloudevent attributes. Minimally
Expand Down
2 changes: 1 addition & 1 deletion cloudevents/pydantic/v1/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def from_json(


def from_dict(
event: typing.Dict[str, typing.Any],
event: typing.Mapping[str, typing.Any],
) -> CloudEvent:
"""
Construct an CloudEvent from a dict `event` representation.
Expand Down
6 changes: 4 additions & 2 deletions cloudevents/pydantic/v1/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ class CloudEvent(abstract.CloudEvent, BaseModel): # type: ignore

@classmethod
def create(
cls, attributes: typing.Dict[str, typing.Any], data: typing.Optional[typing.Any]
cls,
attributes: typing.Mapping[str, typing.Any],
data: typing.Optional[typing.Any],
) -> "CloudEvent":
return cls(attributes, data)

Expand Down Expand Up @@ -155,7 +157,7 @@ def create(

def __init__( # type: ignore[no-untyped-def]
self,
attributes: typing.Optional[typing.Dict[str, typing.Any]] = None,
attributes: typing.Optional[typing.Mapping[str, typing.Any]] = None,
data: typing.Optional[typing.Any] = None,
**kwargs,
):
Expand Down
2 changes: 1 addition & 1 deletion cloudevents/pydantic/v2/conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def from_json(


def from_dict(
event: typing.Dict[str, typing.Any],
event: typing.Mapping[str, typing.Any],
) -> CloudEvent:
"""
Construct an CloudEvent from a dict `event` representation.
Expand Down
6 changes: 4 additions & 2 deletions cloudevents/pydantic/v2/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class CloudEvent(abstract.CloudEvent, BaseModel): # type: ignore

@classmethod
def create(
cls, attributes: typing.Dict[str, typing.Any], data: typing.Optional[typing.Any]
cls,
attributes: typing.Mapping[str, typing.Any],
data: typing.Optional[typing.Any],
) -> "CloudEvent":
return cls(attributes, data)

Expand Down Expand Up @@ -103,7 +105,7 @@ def create(

def __init__( # type: ignore[no-untyped-def]
self,
attributes: typing.Optional[typing.Dict[str, typing.Any]] = None,
attributes: typing.Optional[typing.Mapping[str, typing.Any]] = None,
data: typing.Optional[typing.Any] = None,
**kwargs,
):
Expand Down
0