Skip to content

refactor: use classmethod for alternate constructors (v1 + v2) - #117

Merged
gregnazario merged 4 commits into
mainfrom
cursor/v2-classmethod-constructors-24fb
Jun 18, 2026
Merged

refactor: use classmethod for alternate constructors (v1 + v2)#117
gregnazario merged 4 commits into
mainfrom
cursor/v2-classmethod-constructors-24fb

Conversation

@gregnazario

@gregnazario gregnazario commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Summary

Applies the idiomatic Python refactor from the original PR across both SDKs:

  • v2 SDK (aptos_sdk_v2 / aptos_sdk/v2 mirror)
  • v1 SDK (aptos_sdk, excluding the v2 mirror)

Alternate constructors and factory methods now use @classmethod instead of @staticmethod:

  • generate(), load(), from_str(), from_hex(), from_key(), parse(), etc.
  • deserialize() (BCS protocol + all implementing types)
  • natural(), for_* address derivation helpers
  • Abstract deserialize on key/signature base classes (v2)

Left as @staticmethod: utility methods that are not constructors, e.g.:

  • Serializer.sequence_serializer()
  • PrivateKey.format_private_key() / parse_hex_input() (v1)
  • AptosCLIWrapper helpers
  • PackagePublisher payload/chunk helpers
  • AptosTokenClient.create_collection_payload() / mint_token_payload()
  • Metadata.get_aptos_header_val()

Scope

  • v2/src/aptos_sdk_v2/ + aptos_sdk/v2/ mirror
  • aptos_sdk/ v1 modules (account, address, crypto, transactions, type tags, token client, etc.)
  • v2/CLAUDE.md developer guide example updated

Verification

  • v1: make lint, make test-coverage (162 tests), make test-spec (248 scenarios)
  • v2: make test (414 tests), make lint, mypy clean
  • .github/scripts/check_v2_sync.sh — mirror in sync

Notes

This is a non-breaking API change: callers invoke these methods the same way (Class.method(...)). Behavior is unchanged; only the decorator and internal cls(...) usage differ.

Open in Web Open in Cursor 

@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.61386% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.20%. Comparing base (20b8faa) to head (9e1b987).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
v2/src/aptos_sdk_v2/types/type_tag.py 92.00% 4 Missing ⚠️
aptos_sdk/account_address.py 96.15% 1 Missing ⚠️
aptos_sdk/authenticator.py 96.15% 1 Missing ⚠️
aptos_sdk/transactions.py 97.61% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main     #117   +/-   ##
=======================================
  Coverage   93.20%   93.20%           
=======================================
  Files          56       56           
  Lines        5183     5183           
=======================================
  Hits         4831     4831           
  Misses        352      352           
Flag Coverage Δ
v1-sdk 90.36% <98.92%> (ø)
v2-sdk 97.02% <98.22%> (ø)
Files with missing lines Coverage Δ
aptos_sdk/account.py 100.00% <100.00%> (ø)
aptos_sdk/aptos_token_client.py 95.80% <100.00%> (ø)
aptos_sdk/asymmetric_crypto_wrapper.py 87.33% <100.00%> (ø)
aptos_sdk/bcs.py 92.81% <100.00%> (ø)
aptos_sdk/ed25519.py 91.00% <100.00%> (ø)
aptos_sdk/secp256k1_ecdsa.py 85.16% <100.00%> (ø)
aptos_sdk/type_tag.py 100.00% <100.00%> (ø)
v2/src/aptos_sdk_v2/account/account.py 100.00% <100.00%> (ø)
v2/src/aptos_sdk_v2/bcs/protocols.py 100.00% <ø> (ø)
v2/src/aptos_sdk_v2/crypto/authentication_key.py 96.29% <100.00%> (ø)
... and 14 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cursor cursor Bot changed the title refactor(v2): use classmethod for alternate constructors refactor: use classmethod for alternate constructors (v1 + v2) Jun 17, 2026
@gregnazario
gregnazario requested a review from Copilot June 18, 2026 10:30
cursoragent and others added 3 commits June 18, 2026 06:31
Convert @staticmethod factory methods (generate, from_*, deserialize,
natural, etc.) to @classmethod across the v2 SDK for more idiomatic
Python. Utility methods that are not constructors (e.g.
Serializer.sequence_serializer) remain staticmethod.

Sync aptos_sdk/v2 mirror and update CLAUDE.md developer guide.

Co-authored-by: Greg Nazario <greg@gnazar.io>
Fix CI fmt check: collapse for_named_token return to a single line per
root repo ruff formatting rules.

Co-authored-by: Greg Nazario <greg@gnazar.io>
Apply the same idiomatic Python refactor to the v1 SDK: convert
@staticmethod factory methods (generate, load, from_*, parse,
deserialize, natural, etc.) to @classmethod.

Utility methods that are not constructors remain @staticmethod:
- Serializer.sequence_serializer
- PrivateKey.format_private_key / parse_hex_input
- AptosCLIWrapper helpers
- PackagePublisher payload/chunk helpers
- AptosTokenClient.create_collection_payload / mint_token_payload
- Metadata.get_aptos_header_val

Co-authored-by: Greg Nazario <greg@gnazar.io>
@gregnazario
gregnazario force-pushed the cursor/v2-classmethod-constructors-24fb branch from c1a4856 to b493691 Compare June 18, 2026 10:31
@gregnazario
gregnazario marked this pull request as ready for review June 18, 2026 10:31
@gregnazario
gregnazario requested a review from a team as a code owner June 18, 2026 10:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors alternate constructors and factory/deserialization methods across both the v1 and v2 Aptos Python SDKs to use @classmethod (and cls(...)) instead of @staticmethod, aligning constructors with idiomatic Python while keeping call sites unchanged (Class.method(...)).

Changes:

  • Converted constructor-like helpers (e.g., generate(), from_str(), from_hex(), parse(), deserialize()) from @staticmethod to @classmethod across v1 + v2.
  • Updated BCS Deserializable protocol/ABC surfaces and all implementations to the classmethod shape.
  • Updated v2 developer guide example (v2/CLAUDE.md) to reflect the new deserialize(cls, ...) pattern.

Reviewed changes

Copilot reviewed 39 out of 39 changed files in this pull request and generated no comments.

Show a summary per file
File Description
v2/src/aptos_sdk_v2/types/type_tag.py Switches type-tag constructors/deserializers to @classmethod.
v2/src/aptos_sdk_v2/types/chain_id.py Uses cls(...) in deserialize.
v2/src/aptos_sdk_v2/types/account_address.py Converts parsing + derivation helpers to @classmethod.
v2/src/aptos_sdk_v2/transactions/signed_transaction.py Uses cls(...) in deserialize.
v2/src/aptos_sdk_v2/transactions/raw_transaction.py Converts raw-txn deserialization helpers to @classmethod.
v2/src/aptos_sdk_v2/transactions/payload.py Converts payload factories/deserializers to @classmethod.
v2/src/aptos_sdk_v2/transactions/authenticator.py Converts authenticator deserializers to @classmethod.
v2/src/aptos_sdk_v2/crypto/single_key.py Converts union-key/signature deserializers to @classmethod.
v2/src/aptos_sdk_v2/crypto/secp256k1.py Converts key/signature factories + deserializers to @classmethod.
v2/src/aptos_sdk_v2/crypto/keys.py Updates abstract deserialize APIs to @classmethod.
v2/src/aptos_sdk_v2/crypto/ed25519.py Converts key/signature factories + deserializers to @classmethod.
v2/src/aptos_sdk_v2/crypto/authentication_key.py Converts from_public_key to @classmethod.
v2/src/aptos_sdk_v2/bcs/protocols.py Updates Deserializable protocol to @classmethod deserialize.
v2/src/aptos_sdk_v2/account/account.py Converts account factory methods to @classmethod.
v2/CLAUDE.md Updates documentation example for classmethod deserialize.
aptos_sdk/v2/types/type_tag.py Mirrors v2 TypeTag classmethod refactor.
aptos_sdk/v2/types/chain_id.py Mirrors v2 ChainId.deserialize as classmethod.
aptos_sdk/v2/types/account_address.py Mirrors v2 AccountAddress classmethod parsing/derivation.
aptos_sdk/v2/transactions/signed_transaction.py Mirrors v2 SignedTransaction.deserialize as classmethod.
aptos_sdk/v2/transactions/raw_transaction.py Mirrors v2 raw-txn classmethod deserialization helpers.
aptos_sdk/v2/transactions/payload.py Mirrors v2 payload classmethod factories/deserializers.
aptos_sdk/v2/transactions/authenticator.py Mirrors v2 authenticator classmethod deserializers.
aptos_sdk/v2/crypto/single_key.py Mirrors v2 union-key/signature classmethod deserializers.
aptos_sdk/v2/crypto/secp256k1.py Mirrors v2 secp256k1 classmethod factories/deserializers.
aptos_sdk/v2/crypto/keys.py Mirrors v2 abstract classmethod deserialize APIs.
aptos_sdk/v2/crypto/ed25519.py Mirrors v2 ed25519 classmethod factories/deserializers.
aptos_sdk/v2/crypto/authentication_key.py Mirrors v2 AuthenticationKey.from_public_key as classmethod.
aptos_sdk/v2/bcs/protocols.py Mirrors v2 Deserializable protocol classmethod deserialize.
aptos_sdk/v2/account/account.py Mirrors v2 account classmethod factories.
aptos_sdk/type_tag.py Converts v1 type-tag deserializers/parsers to classmethods.
aptos_sdk/transactions.py Converts v1 transaction-related deserializers/factories to classmethods.
aptos_sdk/secp256k1_ecdsa.py Converts v1 secp256k1 ECDSA key/signature factories/deserializers to classmethods.
aptos_sdk/ed25519.py Converts v1 ed25519 key/signature factories/deserializers to classmethods.
aptos_sdk/bcs.py Updates v1 Deserializable protocol to classmethod deserialize.
aptos_sdk/authenticator.py Converts v1 authenticator deserializers to classmethods.
aptos_sdk/asymmetric_crypto_wrapper.py Converts wrapper deserializers/factories to classmethods.
aptos_sdk/aptos_token_client.py Converts JSON “parse” factories to classmethods.
aptos_sdk/account.py Converts account factory/load helpers to classmethods.
aptos_sdk/account_address.py Converts parsing/derivation/deserialize helpers to classmethods.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@gregnazario
gregnazario requested a review from Copilot June 18, 2026 10:44
@gregnazario
gregnazario merged commit 9fb9b55 into main Jun 18, 2026
14 checks passed
@gregnazario
gregnazario deleted the cursor/v2-classmethod-constructors-24fb branch June 18, 2026 10:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 39 out of 39 changed files in this pull request and generated 2 comments.

Comment thread aptos_sdk/type_tag.py
Comment on lines 327 to 331
if letter == "<":
inner_tags, index = StructTag._from_str_internal(type_tag, index)
inner_tags, index = cls._from_str_internal(type_tag, index)
elif letter == ",":
split = name.split("::")
tag = TypeTag(
Comment thread aptos_sdk/type_tag.py
Comment on lines +362 to 366
@classmethod
def deserialize(cls, deserializer: Deserializer) -> StructTag:
address = deserializer.struct(AccountAddress)
module = deserializer.str()
name = deserializer.str()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants