diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f1a6ae47..107bf9e7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,9 +7,9 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: '3.11' cache: 'pip' @@ -22,13 +22,13 @@ jobs: test: strategy: matrix: - python: ['3.7', '3.8', '3.9', '3.10', '3.11'] + python: ['3.8', '3.9', '3.10', '3.11'] os: [ubuntu-latest, windows-latest, macos-latest] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup Python - uses: actions/setup-python@v4 + uses: actions/setup-python@v5 with: python-version: ${{ matrix.python }} cache: 'pip' diff --git a/.github/workflows/pypi-release.yml b/.github/workflows/pypi-release.yml index 56bbf66a..2b1dbf0c 100644 --- a/.github/workflows/pypi-release.yml +++ b/.github/workflows/pypi-release.yml @@ -12,15 +12,16 @@ jobs: name: Build source distribution runs-on: ubuntu-22.04 steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 - name: Build SDist and wheel run: pipx run build - - uses: actions/upload-artifact@v3 + - uses: actions/upload-artifact@v4 with: + name: artifact path: dist/* - name: Check metadata @@ -30,7 +31,7 @@ jobs: if: github.event_name == 'push' needs: [ build_dist ] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 - name: Set up Python @@ -40,7 +41,7 @@ jobs: cache: 'pip' - name: Install build dependencies run: pip install -U setuptools wheel build - - uses: actions/download-artifact@v3 + - uses: actions/download-artifact@v4 with: # unpacks default artifact into dist/ # if `name: artifact` is omitted, the action will create extra parent dir @@ -51,6 +52,7 @@ jobs: with: user: __token__ password: ${{ secrets.pypi_password }} + attestations: false - name: Install GitPython and cloudevents for pypi_packaging run: pip install -U -r requirements/publish.txt - name: Create Tag diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 15ab6545..75ad2ef1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,17 +6,17 @@ repos: - id: end-of-file-fixer - id: check-toml - repo: https://github.com/pycqa/isort - rev: 5.12.0 + rev: 5.13.2 hooks: - id: isort args: [ "--profile", "black", "--filter-files" ] - repo: https://github.com/psf/black - rev: 23.10.1 + rev: 24.4.2 hooks: - id: black language_version: python3.11 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.6.1 + rev: v1.10.0 hooks: - id: mypy files: ^(cloudevents/) @@ -24,4 +24,4 @@ repos: types: [ python ] args: [ ] additional_dependencies: - - "pydantic" + - "pydantic~=2.7" diff --git a/CHANGELOG.md b/CHANGELOG.md index 51fcb0e5..458a1dd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.11.1] + +### Fixed +- Kafka `conversion` marshaller and unmarshaller typings ([#240]) + +## [1.11.0] + +### Fixed +- Pydantic v2 `examples` keyword usage and improved typings handling ([#235]) +- Kafka `to_binary` check for invalid `content-type` attribute ([#232]) + +### Changed + +- Dropped Python3.7 from CI while its EOL. ([#236]) + ## [1.10.1] ### Fixed @@ -190,6 +205,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Initial release +[1.11.0]: https://github.com/cloudevents/sdk-python/compare/1.10.1...1.11.0 [1.10.1]: https://github.com/cloudevents/sdk-python/compare/1.10.0...1.10.1 [1.10.0]: https://github.com/cloudevents/sdk-python/compare/1.9.0...1.10.0 [1.9.0]: https://github.com/cloudevents/sdk-python/compare/1.8.0...1.9.0 @@ -273,3 +289,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#219]: https://github.com/cloudevents/sdk-python/pull/219 [#221]: https://github.com/cloudevents/sdk-python/pull/221 [#229]: https://github.com/cloudevents/sdk-python/pull/229 +[#232]: https://github.com/cloudevents/sdk-python/pull/232 +[#235]: https://github.com/cloudevents/sdk-python/pull/235 +[#236]: https://github.com/cloudevents/sdk-python/pull/236 +[#240]: https://github.com/cloudevents/sdk-python/pull/240 diff --git a/cloudevents/__init__.py b/cloudevents/__init__.py index c6e11514..d332910d 100644 --- a/cloudevents/__init__.py +++ b/cloudevents/__init__.py @@ -12,4 +12,4 @@ # License for the specific language governing permissions and limitations # under the License. -__version__ = "1.10.1" +__version__ = "1.11.1" diff --git a/cloudevents/kafka/conversion.py b/cloudevents/kafka/conversion.py index 832594d1..bfddca61 100644 --- a/cloudevents/kafka/conversion.py +++ b/cloudevents/kafka/conversion.py @@ -21,9 +21,14 @@ from cloudevents.kafka.exceptions import KeyMapperError from cloudevents.sdk import types -DEFAULT_MARSHALLER: types.MarshallerType = json.dumps -DEFAULT_UNMARSHALLER: types.MarshallerType = json.loads -DEFAULT_EMBEDDED_DATA_MARSHALLER: types.MarshallerType = lambda x: x +JSON_MARSHALLER: types.MarshallerType = json.dumps +JSON_UNMARSHALLER: types.UnmarshallerType = json.loads +IDENTITY_MARSHALLER = IDENTITY_UNMARSHALLER = lambda x: x + +DEFAULT_MARSHALLER: types.MarshallerType = JSON_MARSHALLER +DEFAULT_UNMARSHALLER: types.UnmarshallerType = JSON_UNMARSHALLER +DEFAULT_EMBEDDED_DATA_MARSHALLER: types.MarshallerType = IDENTITY_MARSHALLER +DEFAULT_EMBEDDED_DATA_UNMARSHALLER: types.UnmarshallerType = IDENTITY_UNMARSHALLER class KafkaMessage(typing.NamedTuple): @@ -87,10 +92,10 @@ def to_binary( ) headers = {} - if event["content-type"]: - headers["content-type"] = event["content-type"].encode("utf-8") + if event["datacontenttype"]: + headers["content-type"] = event["datacontenttype"].encode("utf-8") for attr, value in event.get_attributes().items(): - if attr not in ["data", "partitionkey", "content-type"]: + if attr not in ["data", "partitionkey", "datacontenttype"]: if value is not None: headers["ce_{0}".format(attr)] = value.encode("utf-8") @@ -109,7 +114,7 @@ def to_binary( def from_binary( message: KafkaMessage, event_type: typing.Optional[typing.Type[AnyCloudEvent]] = None, - data_unmarshaller: typing.Optional[types.MarshallerType] = None, + data_unmarshaller: typing.Optional[types.UnmarshallerType] = None, ) -> AnyCloudEvent: """ Returns a CloudEvent from a KafkaMessage in binary format. @@ -126,7 +131,7 @@ def from_binary( for header, value in message.headers.items(): header = header.lower() if header == "content-type": - attributes["content-type"] = value.decode() + attributes["datacontenttype"] = value.decode() elif header.startswith("ce_"): attributes[header[3:]] = value.decode() @@ -189,8 +194,8 @@ def to_structured( attrs["data"] = data headers = {} - if "content-type" in attrs: - headers["content-type"] = attrs.pop("content-type").encode("utf-8") + if "datacontenttype" in attrs: + headers["content-type"] = attrs.pop("datacontenttype").encode("utf-8") try: value = envelope_marshaller(attrs) @@ -208,7 +213,7 @@ def to_structured( def from_structured( message: KafkaMessage, event_type: typing.Optional[typing.Type[AnyCloudEvent]] = None, - data_unmarshaller: typing.Optional[types.MarshallerType] = None, + data_unmarshaller: typing.Optional[types.UnmarshallerType] = None, envelope_unmarshaller: typing.Optional[types.UnmarshallerType] = None, ) -> AnyCloudEvent: """ @@ -222,7 +227,7 @@ def from_structured( :returns: CloudEvent """ - data_unmarshaller = data_unmarshaller or DEFAULT_EMBEDDED_DATA_MARSHALLER + data_unmarshaller = data_unmarshaller or DEFAULT_EMBEDDED_DATA_UNMARSHALLER envelope_unmarshaller = envelope_unmarshaller or DEFAULT_UNMARSHALLER try: structure = envelope_unmarshaller(message.value) @@ -255,7 +260,10 @@ def from_structured( attributes[name] = decoded_value for header, val in message.headers.items(): - attributes[header.lower()] = val.decode() + if header.lower() == "content-type": + attributes["datacontenttype"] = val.decode() + else: + attributes[header.lower()] = val.decode() if event_type: result = event_type.create(attributes, data) else: diff --git a/cloudevents/pydantic/__init__.py b/cloudevents/pydantic/__init__.py index 409eb441..f8556ca1 100644 --- a/cloudevents/pydantic/__init__.py +++ b/cloudevents/pydantic/__init__.py @@ -12,22 +12,31 @@ # License for the specific language governing permissions and limitations # under the License. +from typing import TYPE_CHECKING + from cloudevents.exceptions import PydanticFeatureNotInstalled try: - from pydantic import VERSION as PYDANTIC_VERSION - - pydantic_major_version = PYDANTIC_VERSION.split(".")[0] - if pydantic_major_version == "1": - from cloudevents.pydantic.v1 import CloudEvent, from_dict, from_http, from_json - + if TYPE_CHECKING: + from cloudevents.pydantic.v2 import CloudEvent, from_dict, from_http, from_json else: - from cloudevents.pydantic.v2 import ( # type: ignore - CloudEvent, - from_dict, - from_http, - from_json, - ) + from pydantic import VERSION as PYDANTIC_VERSION + + pydantic_major_version = PYDANTIC_VERSION.split(".")[0] + if pydantic_major_version == "1": + from cloudevents.pydantic.v1 import ( + CloudEvent, + from_dict, + from_http, + from_json, + ) + else: + from cloudevents.pydantic.v2 import ( + CloudEvent, + from_dict, + from_http, + from_json, + ) except ImportError: # pragma: no cover # hard to test raise PydanticFeatureNotInstalled( diff --git a/cloudevents/pydantic/v2/event.py b/cloudevents/pydantic/v2/event.py index 4ae8bb5c..643794c1 100644 --- a/cloudevents/pydantic/v2/event.py +++ b/cloudevents/pydantic/v2/event.py @@ -51,53 +51,53 @@ def create( data: typing.Optional[typing.Any] = Field( title=FIELD_DESCRIPTIONS["data"].get("title"), description=FIELD_DESCRIPTIONS["data"].get("description"), - example=FIELD_DESCRIPTIONS["data"].get("example"), + examples=[FIELD_DESCRIPTIONS["data"].get("example")], default=None, ) source: str = Field( title=FIELD_DESCRIPTIONS["source"].get("title"), description=FIELD_DESCRIPTIONS["source"].get("description"), - example=FIELD_DESCRIPTIONS["source"].get("example"), + examples=[FIELD_DESCRIPTIONS["source"].get("example")], ) id: str = Field( title=FIELD_DESCRIPTIONS["id"].get("title"), description=FIELD_DESCRIPTIONS["id"].get("description"), - example=FIELD_DESCRIPTIONS["id"].get("example"), + examples=[FIELD_DESCRIPTIONS["id"].get("example")], default_factory=attribute.default_id_selection_algorithm, ) type: str = Field( title=FIELD_DESCRIPTIONS["type"].get("title"), description=FIELD_DESCRIPTIONS["type"].get("description"), - example=FIELD_DESCRIPTIONS["type"].get("example"), + examples=[FIELD_DESCRIPTIONS["type"].get("example")], ) specversion: attribute.SpecVersion = Field( title=FIELD_DESCRIPTIONS["specversion"].get("title"), description=FIELD_DESCRIPTIONS["specversion"].get("description"), - example=FIELD_DESCRIPTIONS["specversion"].get("example"), + examples=[FIELD_DESCRIPTIONS["specversion"].get("example")], default=attribute.DEFAULT_SPECVERSION, ) time: typing.Optional[datetime.datetime] = Field( title=FIELD_DESCRIPTIONS["time"].get("title"), description=FIELD_DESCRIPTIONS["time"].get("description"), - example=FIELD_DESCRIPTIONS["time"].get("example"), + examples=[FIELD_DESCRIPTIONS["time"].get("example")], default_factory=attribute.default_time_selection_algorithm, ) subject: typing.Optional[str] = Field( title=FIELD_DESCRIPTIONS["subject"].get("title"), description=FIELD_DESCRIPTIONS["subject"].get("description"), - example=FIELD_DESCRIPTIONS["subject"].get("example"), + examples=[FIELD_DESCRIPTIONS["subject"].get("example")], default=None, ) datacontenttype: typing.Optional[str] = Field( title=FIELD_DESCRIPTIONS["datacontenttype"].get("title"), description=FIELD_DESCRIPTIONS["datacontenttype"].get("description"), - example=FIELD_DESCRIPTIONS["datacontenttype"].get("example"), + examples=[FIELD_DESCRIPTIONS["datacontenttype"].get("example")], default=None, ) dataschema: typing.Optional[str] = Field( title=FIELD_DESCRIPTIONS["dataschema"].get("title"), description=FIELD_DESCRIPTIONS["dataschema"].get("description"), - example=FIELD_DESCRIPTIONS["dataschema"].get("example"), + examples=[FIELD_DESCRIPTIONS["dataschema"].get("example")], default=None, ) diff --git a/cloudevents/tests/test_kafka_conversions.py b/cloudevents/tests/test_kafka_conversions.py index 696e75cb..5580773a 100644 --- a/cloudevents/tests/test_kafka_conversions.py +++ b/cloudevents/tests/test_kafka_conversions.py @@ -59,7 +59,7 @@ def source_event(self) -> CloudEvent: "source": "pytest", "type": "com.pytest.test", "time": datetime.datetime(2000, 1, 1, 6, 42, 33).isoformat(), - "content-type": "foo", + "datacontenttype": "foo", "partitionkey": "test_key_123", }, data=self.expected_data, @@ -123,7 +123,7 @@ def test_sets_headers(self, source_event): assert result.headers["ce_source"] == source_event["source"].encode("utf-8") assert result.headers["ce_type"] == source_event["type"].encode("utf-8") assert result.headers["ce_time"] == source_event["time"].encode("utf-8") - assert result.headers["content-type"] == source_event["content-type"].encode( + assert result.headers["content-type"] == source_event["datacontenttype"].encode( "utf-8" ) assert "data" not in result.headers @@ -163,7 +163,7 @@ def source_binary_bytes_message(self) -> KafkaMessage: "ce_time": datetime.datetime(2000, 1, 1, 6, 42, 33) .isoformat() .encode("utf-8"), - "content-type": "foo".encode("utf-8"), + "datacontenttype": "foo".encode("utf-8"), }, value=simple_serialize(self.expected_data), key="test_key_123", @@ -205,7 +205,7 @@ def test_sets_attrs_from_headers(self, source_binary_json_message): assert result["type"] == source_binary_json_message.headers["ce_type"].decode() assert result["time"] == source_binary_json_message.headers["ce_time"].decode() assert ( - result["content-type"] + result["datacontenttype"] == source_binary_json_message.headers["content-type"].decode() ) @@ -328,7 +328,7 @@ def test_no_key(self, source_event): def test_sets_headers(self, source_event): result = to_structured(source_event) assert len(result.headers) == 1 - assert result.headers["content-type"] == source_event["content-type"].encode( + assert result.headers["content-type"] == source_event["datacontenttype"].encode( "utf-8" ) @@ -474,7 +474,7 @@ def test_sets_content_type_default_envelope_unmarshaller( ): result = from_structured(source_structured_json_message) assert ( - result["content-type"] + result["datacontenttype"] == source_structured_json_message.headers["content-type"].decode() ) @@ -487,7 +487,7 @@ def test_sets_content_type_custom_envelope_unmarshaller( envelope_unmarshaller=custom_unmarshaller, ) assert ( - result["content-type"] + result["datacontenttype"] == source_structured_bytes_bytes_message.headers["content-type"].decode() ) diff --git a/mypy.ini b/mypy.ini index 39426375..d8fb9cc0 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,6 +1,6 @@ [mypy] plugins = pydantic.mypy -python_version = 3.7 +python_version = 3.8 pretty = True show_error_context = True diff --git a/setup.py b/setup.py index 95ccf97c..a4e4befc 100644 --- a/setup.py +++ b/setup.py @@ -65,7 +65,6 @@ def get_version(rel_path): "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", diff --git a/tox.ini b/tox.ini index a5cbdfa7..0436a1be 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py{37,38,39,310,311},lint +envlist = py{38,39,310,311,312},lint skipsdist = True [testenv]