Skip to main content

eth-pydantic-types: Pydantic Types for Ethereum

Project description

eth-pydantic-types

The types in this package are pydantic types for Ethereum inspired from eth-typing.

Hash

HashBytes{n} and HashStr{n} are good types to use when your hex values are sized. Both types serialize to string in the JSON schema. Use HashBytes types when you want types to serialize to bytes in the Pydantic core schema and HashStr types when you want to serialize to str in the core Pydantic schema.

from pydantic import BaseModel

from eth_pydantic_types import HashBytes32, HashStr20

# When serializing to JSON, both types are hex strings.
class Transaction(BaseModel):
    tx_hash: HashBytes32  # Will be bytes
    address: HashStr20  # Will be str


# NOTE: I am able to pass an int-hash as the value and it will
#  get validated and type-coerced.
tx = Transaction(
    tx_hash=0x1031f0c9ac54dcb64b4f121a27957c14263c5cb49ed316d568e41e19c34d7b28,
    address=0x1031f0c9ac54dcb64b4f121a27957c14263c5cb4,
)

HexBytes

A thin-wrapper around an already thin-wrapper hexbytes.HexBytes. The difference here is that this HexBytes properly serializes. Use HexBytes any place where you would actually use hexbytes.HexBytes. HexBytes serializes to bytes in the Pydantic core schema and string in the JSON schema with a binary format.

from pydantic import BaseModel
from eth_pydantic_types import HexBytes

class MyStorage(BaseModel):
    cid: HexBytes

# NOTE: We are able to pass a hex-str for a HexBytes value.
storage = MyStorage(cid="0x123")

Address

Use the Address class for working with checksummed-addresses. Addresses get validated and checksummed in model construction. Addresses serialize to str in the Pydantic core schema and string in the JSON schema with a binary format.

from pydantic import BaseModel
from eth_pydantic_types import Address

class Account(BaseModel):
    address: Address

# NOTE: The address ends up checksummed
#   ("0x0837207e343277CBd6c114a45EC0e9Ec56a1AD84")
account = Account(address="0x837207e343277cbd6c114a45ec0e9ec56a1ad84")

HexStr

Use hex str when you only care about un-sized hex strings. The HexStr type serializes to str in the Pydantic core schema and a string in the JSON schema with a binary format.

from eth_pydantic_types import HexStr
from pydantic import BaseModel

class Tx(BaseModel):
    data: HexStr

tx = Tx(data="0x0123")

Bip122Uri

Use BIP-122 URIs in your models by annotating with the Bip122Uri type. This type serializes to a str in the Pydantic core schema as well as a string in the JSON schema, however the individual hashes are validated.

from eth_pydantic_types import Bip122Uri
from pydantic import BaseModel

class Message(BaseModel):
    path: Bip122Uri

message = Message(
    path=(
        "blockchain://d4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
        "/block/752820c0ad7abc1200f9ad42c4adc6fbb4bd44b5bed4667990e64565102c1ba6"
    )
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

eth-pydantic-types-0.1.0a5.tar.gz (65.7 kB view details)

Uploaded Source

Built Distribution

eth_pydantic_types-0.1.0a5-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file eth-pydantic-types-0.1.0a5.tar.gz.

File metadata

  • Download URL: eth-pydantic-types-0.1.0a5.tar.gz
  • Upload date:
  • Size: 65.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/42.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.1.0 tqdm/4.66.1 importlib-metadata/7.0.0 keyring/24.3.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.13

File hashes

Hashes for eth-pydantic-types-0.1.0a5.tar.gz
Algorithm Hash digest
SHA256 4392ab016b4d3a5831517b17082a122268b4425f45ea2faf25d275a7cf79b927
MD5 bb3dabf06e9ae54a7d682dfcf36e5e8c
BLAKE2b-256 d30b0c9cda0c692c6586bf87f0d2e1cf89cfe80f1d51cbf49af2121593e014b6

See more details on using hashes here.

File details

Details for the file eth_pydantic_types-0.1.0a5-py3-none-any.whl.

File metadata

  • Download URL: eth_pydantic_types-0.1.0a5-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/42.0 requests/2.31.0 requests-toolbelt/1.0.0 urllib3/2.1.0 tqdm/4.66.1 importlib-metadata/7.0.0 keyring/24.3.0 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.13

File hashes

Hashes for eth_pydantic_types-0.1.0a5-py3-none-any.whl
Algorithm Hash digest
SHA256 3a2f37a19a13e4660071c9d3f7931aa139d36af94b0a4aaf340d44c44ae9e577
MD5 0f6466acd5d2499df996a5dee0a08161
BLAKE2b-256 94502237759bd9267d09c318f8c58deed1003c169370d4d95b07d140b8fdc7c3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page