From 026dd1ea3641a711336e8bd10e4bfa059b9a8ca1 Mon Sep 17 00:00:00 2001 From: Jerry Date: Wed, 17 Dec 2025 13:55:10 +0900 Subject: [PATCH 1/6] Use cbor2pure by default --- Makefile | 23 +++-------------- README.md | 6 +++-- ensure_pure_cbor2.sh | 38 ---------------------------- poetry.lock | 17 ++++++++++++- pycardano/__init__.py | 7 +++++ pycardano/address.py | 2 +- pycardano/backend/blockfrost.py | 2 +- pycardano/backend/cardano_cli.py | 2 +- pycardano/plutus.py | 2 +- pycardano/serialization.py | 2 +- pycardano/transaction.py | 2 +- pycardano/utils.py | 2 +- pyproject.toml | 1 + test/pycardano/test_serialization.py | 2 +- 14 files changed, 40 insertions(+), 68 deletions(-) delete mode 100755 ensure_pure_cbor2.sh diff --git a/Makefile b/Makefile index c72b327b..755e769e 100644 --- a/Makefile +++ b/Makefile @@ -23,25 +23,10 @@ export PRINT_HELP_PYSCRIPT BROWSER := poetry run python -c "$$BROWSER_PYSCRIPT" -ensure-pure-cbor2: ## ensures cbor2 is installed with pure Python implementation - @poetry run python -c "from importlib.metadata import version; \ - print(version('cbor2'))" > .cbor2_version - @poetry run python -c "import cbor2, inspect; \ - print('Checking cbor2 implementation...'); \ - decoder_path = inspect.getfile(cbor2.CBORDecoder); \ - using_c_ext = decoder_path.endswith('.so'); \ - print(f'Implementation path: {decoder_path}'); \ - print(f'Using C extension: {using_c_ext}'); \ - exit(1 if using_c_ext else 0)" || \ - (echo "Reinstalling cbor2 with pure Python implementation..." && \ - poetry run pip uninstall -y cbor2 && \ - CBOR2_BUILD_C_EXTENSION=0 poetry run pip install --no-binary cbor2 "cbor2==$$(cat .cbor2_version)" --force-reinstall && \ - rm .cbor2_version) - help: @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) -cov: ensure-pure-cbor2 ## check code coverage +cov: ## check code coverage poetry run pytest -n 4 --cov pycardano cov-html: cov ## check code coverage and generate an html report @@ -69,7 +54,7 @@ clean-test: ## remove test and coverage artifacts rm -fr cov_html/ rm -fr .pytest_cache -test: ensure-pure-cbor2 ## runs tests +test: ## runs tests poetry run pytest -vv -n 4 test-integration: ## runs integration tests @@ -78,7 +63,7 @@ test-integration: ## runs integration tests test-single: ## runs tests with "single" markers poetry run pytest -s -vv -m single -qa: ensure-pure-cbor2 ## runs static analyses +qa: ## runs static analyses poetry run flake8 pycardano poetry run mypy --install-types --non-interactive pycardano poetry run black --check . @@ -92,6 +77,6 @@ docs: ## build the documentation poetry run sphinx-build docs/source docs/build/html $(BROWSER) docs/build/html/index.html -release: clean qa test format ensure-pure-cbor2 ## build dist version and release to pypi +release: clean qa test format ## build dist version and release to pypi poetry build poetry publish \ No newline at end of file diff --git a/README.md b/README.md index 40f5a84a..590b2c03 100644 --- a/README.md +++ b/README.md @@ -57,11 +57,13 @@ Install the library using [pip](https://pip.pypa.io/en/stable/): `pip install pycardano` -#### Install cbor2 pure python implementation (Optional) +#### cbor2 [cbor2](https://github.com/agronholm/cbor2/tree/master) is a dependency of pycardano. It is used to encode and decode CBOR data. It has two implementations: one is pure Python and the other is C, which is installed by default. The C implementation is faster, but it is less flexible than the pure Python implementation. -For some users, the C implementation may not work properly when deserializing cbor data. For example, the order of inputs of a transaction isn't guaranteed to be the same as the order of inputs in the original transaction (details could be found in [this issue](https://github.com/Python-Cardano/pycardano/issues/311)). This would result in a different transaction hash when the transaction is serialized again. For users who encounter this issue, we recommend to use the pure Python implementation of cbor2. You can do so by running [ensure_pure_cbor2.sh](./ensure_pure_cbor2.sh), which inspect the cbor2 installed in the running environment and force install pure python implementation if necessary. +For some users, the C implementation may not work properly when deserializing cbor data. For example, the order of inputs of a transaction isn't guaranteed to be the same as the order of inputs in the original transaction (details could be found in [this issue](https://github.com/Python-Cardano/pycardano/issues/311)). This would result in a different transaction hash when the transaction is serialized again. + +To solve this problem, a fork of cbor2 is created at [cbor2pure](https://github.com/cffls/cbor2pure). This fork removes C extension and only uses pure python for cbor decoding. By default, for correctness, pycardano uses cbor2pure in decoding. However, if speed is preferred over accuracy, users can set `CBOR_C_EXETENSION=1` in their environment, and the default C extension would be used instead. ```bash ensure_pure_cbor2.sh diff --git a/ensure_pure_cbor2.sh b/ensure_pure_cbor2.sh deleted file mode 100755 index 9c50a210..00000000 --- a/ensure_pure_cbor2.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash -# Script to ensure cbor2 is installed with pure Python implementation - -set -x - -# Check if poetry is available, otherwise use python directly -if command -v poetry &> /dev/null; then - PYTHON="poetry run python" -else - PYTHON="python" -fi - -echo "Checking cbor2 version..." -$PYTHON -c "from importlib.metadata import version; print(version('cbor2'))" > .cbor2_version -CBOR2_VERSION=$(cat .cbor2_version) -echo "Found cbor2 version: $CBOR2_VERSION" - -echo "Checking cbor2 implementation..." -$PYTHON -c " -import cbor2, inspect, sys -decoder_path = inspect.getfile(cbor2.CBORDecoder) -using_c_ext = decoder_path.endswith('.so') -print(f'Implementation path: {decoder_path}') -print(f'Using C extension: {using_c_ext}') -sys.exit(1 if using_c_ext else 0) -" - -if [ $? -ne 0 ]; then - echo "Reinstalling cbor2 with pure Python implementation..." - $PYTHON -m pip uninstall -y cbor2 || uv pip uninstall cbor2 - CBOR2_BUILD_C_EXTENSION=0 $PYTHON -m pip install --no-binary cbor2 "cbor2==$CBOR2_VERSION" --force-reinstall || CBOR2_BUILD_C_EXTENSION=0 uv pip install --no-binary cbor2 "cbor2==$CBOR2_VERSION" --force-reinstall - echo "Successfully reinstalled cbor2 with pure Python implementation" -else - echo "Already using pure Python implementation of cbor2" -fi - -# Clean up -rm -f .cbor2_version diff --git a/poetry.lock b/poetry.lock index c0dd4358..0eb28037 100644 --- a/poetry.lock +++ b/poetry.lock @@ -262,6 +262,21 @@ files = [ {file = "cbor2-5.7.0.tar.gz", hash = "sha256:3f6d843f4db4d0ec501c46453c22a4fbebb1abfb5b740e1bcab34c615cd7406b"}, ] +[[package]] +name = "cbor2pure" +version = "5.7.2" +description = "CBOR (de)serializer with extensive tag support" +optional = false +python-versions = ">=3.9" +groups = ["main"] +files = [ + {file = "cbor2pure-5.7.2-py3-none-any.whl", hash = "sha256:cc3ea93afc3d92789343cdaa7c3f75802f56beecf6272ee6069e04a237641d4f"}, + {file = "cbor2pure-5.7.2.tar.gz", hash = "sha256:96bbd679260777c4e2075fe00a28a16a297207862d7e7ff80ab529c91c35925e"}, +] + +[package.dependencies] +cbor2 = "*" + [[package]] name = "certifi" version = "2025.8.3" @@ -2541,4 +2556,4 @@ type = ["pytest-mypy"] [metadata] lock-version = "2.1" python-versions = "^3.9.1" -content-hash = "ba802fdd52ec008c3af72f5c7b8765d18ba0875feab49b9a78c6b94cfffae680" +content-hash = "03156c658c4cb55fd0abc906c448aa1b0569a09e8321b00e32bd47a6b0bc7c1e" diff --git a/pycardano/__init__.py b/pycardano/__init__.py index 46d24da2..d1cea09e 100644 --- a/pycardano/__init__.py +++ b/pycardano/__init__.py @@ -1,5 +1,12 @@ # flake8: noqa +import os + +if os.getenv("CBOR_C_EXETENSION", "0") == "1": + import cbor2 +else: + import cbor2pure as cbor2 + from .address import * from .backend import * from .certificate import * diff --git a/pycardano/address.py b/pycardano/address.py index 8d660995..b255d663 100644 --- a/pycardano/address.py +++ b/pycardano/address.py @@ -14,7 +14,7 @@ from typing import Optional, Type, Union import base58 -import cbor2 +from pycardano import cbor2 from cbor2 import CBORTag from typing_extensions import override diff --git a/pycardano/backend/blockfrost.py b/pycardano/backend/blockfrost.py index 1d25a6f8..11faf058 100644 --- a/pycardano/backend/blockfrost.py +++ b/pycardano/backend/blockfrost.py @@ -5,7 +5,7 @@ from fractions import Fraction from typing import Dict, List, Optional, Union -import cbor2 +from pycardano import cbor2 from blockfrost import ApiError, ApiUrls, BlockFrostApi from blockfrost.utils import Namespace diff --git a/pycardano/backend/cardano_cli.py b/pycardano/backend/cardano_cli.py index 4c4c0da7..6769570e 100644 --- a/pycardano/backend/cardano_cli.py +++ b/pycardano/backend/cardano_cli.py @@ -12,7 +12,7 @@ from pathlib import Path from typing import Dict, List, Optional, Union -import cbor2 +from pycardano import cbor2 import docker from cachetools import Cache, LRUCache, TTLCache, func from docker.errors import APIError diff --git a/pycardano/plutus.py b/pycardano/plutus.py index 83c66705..f2cfef79 100644 --- a/pycardano/plutus.py +++ b/pycardano/plutus.py @@ -10,7 +10,7 @@ from hashlib import sha256 from typing import Any, List, Optional, Type, Union -import cbor2 +from pycardano import cbor2 from cbor2 import CBORTag from nacl.encoding import RawEncoder from nacl.hash import blake2b diff --git a/pycardano/serialization.py b/pycardano/serialization.py index 385fc120..b7975bea 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -31,7 +31,7 @@ get_type_hints, ) -import cbor2 +from pycardano import cbor2 from pycardano.logging import logger diff --git a/pycardano/transaction.py b/pycardano/transaction.py index da3deb56..816011db 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -6,7 +6,7 @@ from dataclasses import dataclass, field from typing import Any, Callable, List, Optional, Type, Union -import cbor2 +from pycardano import cbor2 from cbor2 import CBORTag from nacl.encoding import RawEncoder from nacl.hash import blake2b diff --git a/pycardano/utils.py b/pycardano/utils.py index 00c5f6a4..6b9554ea 100644 --- a/pycardano/utils.py +++ b/pycardano/utils.py @@ -6,7 +6,7 @@ import sys from typing import Dict, List, Optional, Tuple, Union -import cbor2 +from pycardano import cbor2 from nacl.encoding import RawEncoder from nacl.hash import blake2b diff --git a/pyproject.toml b/pyproject.toml index 83f0e1f9..bf45e5eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,6 +40,7 @@ ogmios = ">=1.4.2" requests = ">=2.32.3" websockets = ">=13.0" base58 = ">=2.1.0" +cbor2pure = ">=5.7.2" [tool.poetry.group.dev.dependencies] pytest = ">=8.2.0" diff --git a/test/pycardano/test_serialization.py b/test/pycardano/test_serialization.py index 607d3d18..a872d913 100644 --- a/test/pycardano/test_serialization.py +++ b/test/pycardano/test_serialization.py @@ -18,7 +18,7 @@ get_origin, ) -import cbor2 +from pycardano import cbor2 import pytest from cbor2 import CBORTag from frozenlist import FrozenList From 75d054dc04774d89a1f1fd758bc39199a36dc944 Mon Sep 17 00:00:00 2001 From: Jerry Date: Wed, 17 Dec 2025 14:06:21 +0900 Subject: [PATCH 2/6] Fix workflow and test --- .github/workflows/main.yml | 3 --- .github/workflows/publish.yml | 3 --- integration-test/run_tests.sh | 1 - pycardano/__init__.py | 2 +- pycardano/address.py | 2 +- pycardano/backend/blockfrost.py | 2 +- pycardano/backend/cardano_cli.py | 2 +- pycardano/plutus.py | 2 +- pycardano/serialization.py | 1 - pycardano/transaction.py | 4 ++-- pycardano/utils.py | 2 +- test/pycardano/test_serialization.py | 2 +- 12 files changed, 9 insertions(+), 17 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a67ddebd..e1e78afa 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,9 +28,6 @@ jobs: - name: Install dependencies run: | poetry install - - name: Ensure pure cbor2 is installed - run: | - make ensure-pure-cbor2 - name: Run unit tests run: | poetry run pytest --doctest-modules --ignore=examples --cov=pycardano --cov-config=.coveragerc --cov-report=xml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index ef489fcd..c451ff40 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -24,9 +24,6 @@ jobs: - name: Install dependencies run: | poetry install - - name: Ensure pure cbor2 is installed - run: | - make ensure-pure-cbor2 - name: Lint with flake8 run: | poetry run flake8 pycardano diff --git a/integration-test/run_tests.sh b/integration-test/run_tests.sh index 11ea5242..8d32d0f9 100755 --- a/integration-test/run_tests.sh +++ b/integration-test/run_tests.sh @@ -6,7 +6,6 @@ set -o pipefail ROOT=$(pwd) poetry install -C .. -make ensure-pure-cbor2 -f ../Makefile #poetry run pip install ogmios ########## diff --git a/pycardano/__init__.py b/pycardano/__init__.py index d1cea09e..671b36fa 100644 --- a/pycardano/__init__.py +++ b/pycardano/__init__.py @@ -5,7 +5,7 @@ if os.getenv("CBOR_C_EXETENSION", "0") == "1": import cbor2 else: - import cbor2pure as cbor2 + import cbor2pure as cbor2 # type: ignore[no-redef] from .address import * from .backend import * diff --git a/pycardano/address.py b/pycardano/address.py index b255d663..f6bd6e33 100644 --- a/pycardano/address.py +++ b/pycardano/address.py @@ -14,10 +14,10 @@ from typing import Optional, Type, Union import base58 -from pycardano import cbor2 from cbor2 import CBORTag from typing_extensions import override +from pycardano import cbor2 from pycardano.crypto.bech32 import decode, encode from pycardano.exception import ( DecodingException, diff --git a/pycardano/backend/blockfrost.py b/pycardano/backend/blockfrost.py index 11faf058..dc9c5cca 100644 --- a/pycardano/backend/blockfrost.py +++ b/pycardano/backend/blockfrost.py @@ -5,10 +5,10 @@ from fractions import Fraction from typing import Dict, List, Optional, Union -from pycardano import cbor2 from blockfrost import ApiError, ApiUrls, BlockFrostApi from blockfrost.utils import Namespace +from pycardano import cbor2 from pycardano.address import Address from pycardano.backend.base import ( ALONZO_COINS_PER_UTXO_WORD, diff --git a/pycardano/backend/cardano_cli.py b/pycardano/backend/cardano_cli.py index 6769570e..248ebe1d 100644 --- a/pycardano/backend/cardano_cli.py +++ b/pycardano/backend/cardano_cli.py @@ -12,11 +12,11 @@ from pathlib import Path from typing import Dict, List, Optional, Union -from pycardano import cbor2 import docker from cachetools import Cache, LRUCache, TTLCache, func from docker.errors import APIError +from pycardano import cbor2 from pycardano.address import Address from pycardano.backend.base import ( ALONZO_COINS_PER_UTXO_WORD, diff --git a/pycardano/plutus.py b/pycardano/plutus.py index f2cfef79..ecaff67d 100644 --- a/pycardano/plutus.py +++ b/pycardano/plutus.py @@ -10,12 +10,12 @@ from hashlib import sha256 from typing import Any, List, Optional, Type, Union -from pycardano import cbor2 from cbor2 import CBORTag from nacl.encoding import RawEncoder from nacl.hash import blake2b from typeguard import typechecked +from pycardano import cbor2 from pycardano.exception import DeserializeException, InvalidArgumentException from pycardano.hash import DATUM_HASH_SIZE, SCRIPT_HASH_SIZE, DatumHash, ScriptHash from pycardano.nativescript import NativeScript diff --git a/pycardano/serialization.py b/pycardano/serialization.py index b7975bea..1bdd26f1 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -32,7 +32,6 @@ ) from pycardano import cbor2 - from pycardano.logging import logger # Remove the semantic decoder for 258 (CBOR tag for set) as we care about the order of elements diff --git a/pycardano/transaction.py b/pycardano/transaction.py index 816011db..5d6a5cc1 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -6,12 +6,12 @@ from dataclasses import dataclass, field from typing import Any, Callable, List, Optional, Type, Union -from pycardano import cbor2 from cbor2 import CBORTag from nacl.encoding import RawEncoder from nacl.hash import blake2b from pprintpp import pformat +from pycardano import cbor2 from pycardano.address import Address from pycardano.certificate import Certificate from pycardano.exception import InvalidDataException @@ -105,7 +105,7 @@ def __add__(self, other: Asset) -> Asset: def __iadd__(self, other: Asset) -> Asset: new_item = self + other - self.data = new_item.data + self.data = new_item.data # type: ignore[has-type] return self.normalize() def __sub__(self, other: Asset) -> Asset: diff --git a/pycardano/utils.py b/pycardano/utils.py index 6b9554ea..dbeba198 100644 --- a/pycardano/utils.py +++ b/pycardano/utils.py @@ -6,10 +6,10 @@ import sys from typing import Dict, List, Optional, Tuple, Union -from pycardano import cbor2 from nacl.encoding import RawEncoder from nacl.hash import blake2b +from pycardano import cbor2 from pycardano.backend.base import ChainContext from pycardano.hash import SCRIPT_DATA_HASH_SIZE, SCRIPT_HASH_SIZE, ScriptDataHash from pycardano.plutus import COST_MODELS, CostModels, Datum, RedeemerMap, Redeemers diff --git a/test/pycardano/test_serialization.py b/test/pycardano/test_serialization.py index a872d913..6e51c769 100644 --- a/test/pycardano/test_serialization.py +++ b/test/pycardano/test_serialization.py @@ -18,7 +18,6 @@ get_origin, ) -from pycardano import cbor2 import pytest from cbor2 import CBORTag from frozenlist import FrozenList @@ -34,6 +33,7 @@ TransactionWitnessSet, VerificationKey, VerificationKeyWitness, + cbor2, ) from pycardano.exception import ( DeserializeException, From aa0f36e549be46d2dc47dbcf754e7874cbacbca5 Mon Sep 17 00:00:00 2001 From: Jerry Date: Thu, 18 Dec 2025 21:05:00 +0900 Subject: [PATCH 3/6] Address comments --- README.md | 2 +- pycardano/__init__.py | 7 ------- pycardano/address.py | 2 +- pycardano/backend/blockfrost.py | 2 +- pycardano/backend/cardano_cli.py | 2 +- pycardano/cbor2.py | 16 ++++++++++++++++ pycardano/plutus.py | 2 +- pycardano/serialization.py | 2 +- pycardano/transaction.py | 2 +- pycardano/utils.py | 2 +- test/pycardano/test_serialization.py | 2 +- 11 files changed, 25 insertions(+), 16 deletions(-) create mode 100644 pycardano/cbor2.py diff --git a/README.md b/README.md index 590b2c03..b6281b5f 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ It has two implementations: one is pure Python and the other is C, which is inst For some users, the C implementation may not work properly when deserializing cbor data. For example, the order of inputs of a transaction isn't guaranteed to be the same as the order of inputs in the original transaction (details could be found in [this issue](https://github.com/Python-Cardano/pycardano/issues/311)). This would result in a different transaction hash when the transaction is serialized again. -To solve this problem, a fork of cbor2 is created at [cbor2pure](https://github.com/cffls/cbor2pure). This fork removes C extension and only uses pure python for cbor decoding. By default, for correctness, pycardano uses cbor2pure in decoding. However, if speed is preferred over accuracy, users can set `CBOR_C_EXETENSION=1` in their environment, and the default C extension would be used instead. +To solve this problem, a fork of cbor2 is created at [cbor2pure](https://github.com/cffls/cbor2pure). This fork removes C extension and only uses pure python for cbor decoding. By default, for correctness, pycardano uses cbor2pure in decoding. However, if speed is preferred over accuracy, users can set `CBOR_C_EXTENSION=1` in their environment, and the default C extension would be used instead. ```bash ensure_pure_cbor2.sh diff --git a/pycardano/__init__.py b/pycardano/__init__.py index 671b36fa..46d24da2 100644 --- a/pycardano/__init__.py +++ b/pycardano/__init__.py @@ -1,12 +1,5 @@ # flake8: noqa -import os - -if os.getenv("CBOR_C_EXETENSION", "0") == "1": - import cbor2 -else: - import cbor2pure as cbor2 # type: ignore[no-redef] - from .address import * from .backend import * from .certificate import * diff --git a/pycardano/address.py b/pycardano/address.py index f6bd6e33..a95db141 100644 --- a/pycardano/address.py +++ b/pycardano/address.py @@ -17,7 +17,7 @@ from cbor2 import CBORTag from typing_extensions import override -from pycardano import cbor2 +from pycardano.cbor2 import cbor2 from pycardano.crypto.bech32 import decode, encode from pycardano.exception import ( DecodingException, diff --git a/pycardano/backend/blockfrost.py b/pycardano/backend/blockfrost.py index dc9c5cca..97a57c5d 100644 --- a/pycardano/backend/blockfrost.py +++ b/pycardano/backend/blockfrost.py @@ -8,7 +8,6 @@ from blockfrost import ApiError, ApiUrls, BlockFrostApi from blockfrost.utils import Namespace -from pycardano import cbor2 from pycardano.address import Address from pycardano.backend.base import ( ALONZO_COINS_PER_UTXO_WORD, @@ -16,6 +15,7 @@ GenesisParameters, ProtocolParameters, ) +from pycardano.cbor2 import cbor2 from pycardano.exception import TransactionFailedException from pycardano.hash import SCRIPT_HASH_SIZE, DatumHash, ScriptHash from pycardano.nativescript import NativeScript diff --git a/pycardano/backend/cardano_cli.py b/pycardano/backend/cardano_cli.py index 248ebe1d..b2daa25a 100644 --- a/pycardano/backend/cardano_cli.py +++ b/pycardano/backend/cardano_cli.py @@ -16,7 +16,6 @@ from cachetools import Cache, LRUCache, TTLCache, func from docker.errors import APIError -from pycardano import cbor2 from pycardano.address import Address from pycardano.backend.base import ( ALONZO_COINS_PER_UTXO_WORD, @@ -24,6 +23,7 @@ GenesisParameters, ProtocolParameters, ) +from pycardano.cbor2 import cbor2 from pycardano.exception import ( CardanoCliError, PyCardanoException, diff --git a/pycardano/cbor2.py b/pycardano/cbor2.py new file mode 100644 index 00000000..95b3b379 --- /dev/null +++ b/pycardano/cbor2.py @@ -0,0 +1,16 @@ +""" +Conditional cbor2 import module. + +This module provides a centralized location for importing cbor2, +with support for both the C extension (cbor2) and pure Python (cbor2pure) versions. +Set the environment variable CBOR_C_EXTENSION=1 to use the C extension. +""" + +import os + +if os.getenv("CBOR_C_EXTENSION", "0") == "1": + import cbor2 +else: + import cbor2pure as cbor2 # type: ignore[no-redef] + +__all__ = ["cbor2"] diff --git a/pycardano/plutus.py b/pycardano/plutus.py index ecaff67d..27b524f1 100644 --- a/pycardano/plutus.py +++ b/pycardano/plutus.py @@ -15,7 +15,7 @@ from nacl.hash import blake2b from typeguard import typechecked -from pycardano import cbor2 +from pycardano.cbor2 import cbor2 from pycardano.exception import DeserializeException, InvalidArgumentException from pycardano.hash import DATUM_HASH_SIZE, SCRIPT_HASH_SIZE, DatumHash, ScriptHash from pycardano.nativescript import NativeScript diff --git a/pycardano/serialization.py b/pycardano/serialization.py index 1bdd26f1..a76c122f 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -31,7 +31,7 @@ get_type_hints, ) -from pycardano import cbor2 +from pycardano.cbor2 import cbor2 from pycardano.logging import logger # Remove the semantic decoder for 258 (CBOR tag for set) as we care about the order of elements diff --git a/pycardano/transaction.py b/pycardano/transaction.py index 5d6a5cc1..b71a6e97 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -11,8 +11,8 @@ from nacl.hash import blake2b from pprintpp import pformat -from pycardano import cbor2 from pycardano.address import Address +from pycardano.cbor2 import cbor2 from pycardano.certificate import Certificate from pycardano.exception import InvalidDataException from pycardano.governance import ProposalProcedure, VotingProcedures diff --git a/pycardano/utils.py b/pycardano/utils.py index dbeba198..dec51462 100644 --- a/pycardano/utils.py +++ b/pycardano/utils.py @@ -9,8 +9,8 @@ from nacl.encoding import RawEncoder from nacl.hash import blake2b -from pycardano import cbor2 from pycardano.backend.base import ChainContext +from pycardano.cbor2 import cbor2 from pycardano.hash import SCRIPT_DATA_HASH_SIZE, SCRIPT_HASH_SIZE, ScriptDataHash from pycardano.plutus import COST_MODELS, CostModels, Datum, RedeemerMap, Redeemers from pycardano.serialization import NonEmptyOrderedSet, default_encoder diff --git a/test/pycardano/test_serialization.py b/test/pycardano/test_serialization.py index 6e51c769..53af7bf8 100644 --- a/test/pycardano/test_serialization.py +++ b/test/pycardano/test_serialization.py @@ -33,8 +33,8 @@ TransactionWitnessSet, VerificationKey, VerificationKeyWitness, - cbor2, ) +from pycardano.cbor2 import cbor2 from pycardano.exception import ( DeserializeException, InvalidKeyTypeException, From d025eefb875887cc5994f3aa75e6a3b026dbf51e Mon Sep 17 00:00:00 2001 From: Jerry Date: Thu, 18 Dec 2025 21:40:41 +0900 Subject: [PATCH 4/6] Remove cbor module from public --- pycardano/cbor2.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/pycardano/cbor2.py b/pycardano/cbor2.py index 95b3b379..e7e28f8d 100644 --- a/pycardano/cbor2.py +++ b/pycardano/cbor2.py @@ -12,5 +12,3 @@ import cbor2 else: import cbor2pure as cbor2 # type: ignore[no-redef] - -__all__ = ["cbor2"] From 4a7b63bfdba8f1c709f09f24df58151ab4c2e626 Mon Sep 17 00:00:00 2001 From: Jerry Date: Thu, 18 Dec 2025 22:01:00 +0900 Subject: [PATCH 5/6] Remove cbor2 module to cbor to avoid name conflict --- pycardano/address.py | 2 +- pycardano/backend/blockfrost.py | 2 +- pycardano/backend/cardano_cli.py | 2 +- pycardano/{cbor2.py => cbor.py} | 0 pycardano/plutus.py | 2 +- pycardano/serialization.py | 2 +- pycardano/transaction.py | 2 +- pycardano/utils.py | 2 +- test/pycardano/test_serialization.py | 2 +- 9 files changed, 8 insertions(+), 8 deletions(-) rename pycardano/{cbor2.py => cbor.py} (100%) diff --git a/pycardano/address.py b/pycardano/address.py index a95db141..0b4290ab 100644 --- a/pycardano/address.py +++ b/pycardano/address.py @@ -17,7 +17,7 @@ from cbor2 import CBORTag from typing_extensions import override -from pycardano.cbor2 import cbor2 +from pycardano.cbor import cbor2 from pycardano.crypto.bech32 import decode, encode from pycardano.exception import ( DecodingException, diff --git a/pycardano/backend/blockfrost.py b/pycardano/backend/blockfrost.py index 97a57c5d..b7b11bad 100644 --- a/pycardano/backend/blockfrost.py +++ b/pycardano/backend/blockfrost.py @@ -15,7 +15,7 @@ GenesisParameters, ProtocolParameters, ) -from pycardano.cbor2 import cbor2 +from pycardano.cbor import cbor2 from pycardano.exception import TransactionFailedException from pycardano.hash import SCRIPT_HASH_SIZE, DatumHash, ScriptHash from pycardano.nativescript import NativeScript diff --git a/pycardano/backend/cardano_cli.py b/pycardano/backend/cardano_cli.py index b2daa25a..61398c8f 100644 --- a/pycardano/backend/cardano_cli.py +++ b/pycardano/backend/cardano_cli.py @@ -23,7 +23,7 @@ GenesisParameters, ProtocolParameters, ) -from pycardano.cbor2 import cbor2 +from pycardano.cbor import cbor2 from pycardano.exception import ( CardanoCliError, PyCardanoException, diff --git a/pycardano/cbor2.py b/pycardano/cbor.py similarity index 100% rename from pycardano/cbor2.py rename to pycardano/cbor.py diff --git a/pycardano/plutus.py b/pycardano/plutus.py index 27b524f1..98036de6 100644 --- a/pycardano/plutus.py +++ b/pycardano/plutus.py @@ -15,7 +15,7 @@ from nacl.hash import blake2b from typeguard import typechecked -from pycardano.cbor2 import cbor2 +from pycardano.cbor import cbor2 from pycardano.exception import DeserializeException, InvalidArgumentException from pycardano.hash import DATUM_HASH_SIZE, SCRIPT_HASH_SIZE, DatumHash, ScriptHash from pycardano.nativescript import NativeScript diff --git a/pycardano/serialization.py b/pycardano/serialization.py index a76c122f..bc6e1cec 100644 --- a/pycardano/serialization.py +++ b/pycardano/serialization.py @@ -31,7 +31,7 @@ get_type_hints, ) -from pycardano.cbor2 import cbor2 +from pycardano.cbor import cbor2 from pycardano.logging import logger # Remove the semantic decoder for 258 (CBOR tag for set) as we care about the order of elements diff --git a/pycardano/transaction.py b/pycardano/transaction.py index b71a6e97..fa3f1666 100644 --- a/pycardano/transaction.py +++ b/pycardano/transaction.py @@ -12,7 +12,7 @@ from pprintpp import pformat from pycardano.address import Address -from pycardano.cbor2 import cbor2 +from pycardano.cbor import cbor2 from pycardano.certificate import Certificate from pycardano.exception import InvalidDataException from pycardano.governance import ProposalProcedure, VotingProcedures diff --git a/pycardano/utils.py b/pycardano/utils.py index dec51462..ddb50ff5 100644 --- a/pycardano/utils.py +++ b/pycardano/utils.py @@ -10,7 +10,7 @@ from nacl.hash import blake2b from pycardano.backend.base import ChainContext -from pycardano.cbor2 import cbor2 +from pycardano.cbor import cbor2 from pycardano.hash import SCRIPT_DATA_HASH_SIZE, SCRIPT_HASH_SIZE, ScriptDataHash from pycardano.plutus import COST_MODELS, CostModels, Datum, RedeemerMap, Redeemers from pycardano.serialization import NonEmptyOrderedSet, default_encoder diff --git a/test/pycardano/test_serialization.py b/test/pycardano/test_serialization.py index 53af7bf8..256caf62 100644 --- a/test/pycardano/test_serialization.py +++ b/test/pycardano/test_serialization.py @@ -34,7 +34,7 @@ VerificationKey, VerificationKeyWitness, ) -from pycardano.cbor2 import cbor2 +from pycardano.cbor import cbor2 from pycardano.exception import ( DeserializeException, InvalidKeyTypeException, From d1c151d1aba0c188fc0c524c2cd946a3aff13d13 Mon Sep 17 00:00:00 2001 From: Jerry Date: Thu, 18 Dec 2025 22:08:48 +0900 Subject: [PATCH 6/6] fix linter --- pycardano/cbor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pycardano/cbor.py b/pycardano/cbor.py index e7e28f8d..ddd6e2ab 100644 --- a/pycardano/cbor.py +++ b/pycardano/cbor.py @@ -9,6 +9,6 @@ import os if os.getenv("CBOR_C_EXTENSION", "0") == "1": - import cbor2 + import cbor2 # noqa: F401 else: - import cbor2pure as cbor2 # type: ignore[no-redef] + import cbor2pure as cbor2 # type: ignore # noqa: F401