refactor: use classmethod for alternate constructors (v1 + v2) - #117
Merged
Conversation
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
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
force-pushed
the
cursor/v2-classmethod-constructors-24fb
branch
from
June 18, 2026 10:31
c1a4856 to
b493691
Compare
gregnazario
marked this pull request as ready for review
June 18, 2026 10:31
Contributor
There was a problem hiding this comment.
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@staticmethodto@classmethodacross v1 + v2. - Updated BCS
Deserializableprotocol/ABC surfaces and all implementations to the classmethod shape. - Updated v2 developer guide example (
v2/CLAUDE.md) to reflect the newdeserialize(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.
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 on lines
+362
to
366
| @classmethod | ||
| def deserialize(cls, deserializer: Deserializer) -> StructTag: | ||
| address = deserializer.struct(AccountAddress) | ||
| module = deserializer.str() | ||
| name = deserializer.str() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Applies the idiomatic Python refactor from the original PR across both SDKs:
aptos_sdk_v2/aptos_sdk/v2mirror)aptos_sdk, excluding the v2 mirror)Alternate constructors and factory methods now use
@classmethodinstead of@staticmethod:generate(),load(),from_str(),from_hex(),from_key(),parse(), etc.deserialize()(BCS protocol + all implementing types)natural(),for_*address derivation helpersdeserializeon 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)AptosCLIWrapperhelpersPackagePublisherpayload/chunk helpersAptosTokenClient.create_collection_payload()/mint_token_payload()Metadata.get_aptos_header_val()Scope
v2/src/aptos_sdk_v2/+aptos_sdk/v2/mirroraptos_sdk/v1 modules (account, address, crypto, transactions, type tags, token client, etc.)v2/CLAUDE.mddeveloper guide example updatedVerification
make lint,make test-coverage(162 tests),make test-spec(248 scenarios)make test(414 tests),make lint, mypy clean.github/scripts/check_v2_sync.sh— mirror in syncNotes
This is a non-breaking API change: callers invoke these methods the same way (
Class.method(...)). Behavior is unchanged; only the decorator and internalcls(...)usage differ.