Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: adds signed and unsigned ints to list of types #11

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9f7f14d
feat: adds signed and unsigned ints to list of types
bitwise-constructs Jun 12, 2024
492fd3e
chore: begins tests for int
bitwise-constructs Jun 12, 2024
91f0b12
fix: fixes mypy issues
bitwise-constructs Jun 12, 2024
50c3bbc
chore: removes test
bitwise-constructs Jun 12, 2024
2b2269c
fix: fixes issue with signed/unsigned validation check
bitwise-constructs Jun 12, 2024
18e505f
chore: proper snake casing
bitwise-constructs Jun 12, 2024
6dec172
chore: fixes name on Int types to remove reference to Hash
bitwise-constructs Jun 12, 2024
2a5d8d6
feat!: cleaned up codebase around managing multiple sized types.
bitwise-constructs Jun 15, 2024
db7b546
refactor: in progress - combining hex.py and hash.py hex strings/byte…
bitwise-constructs Jun 19, 2024
4a92f24
chore: cleans up abi types and stray comments from old hash string an…
bitwise-constructs Jun 19, 2024
2541026
feat: adds remaining int types and updates hexbytes version
bitwise-constructs Jun 26, 2024
88e2478
test: broken build but need to get this approach reviewed - issue wit…
bitwise-constructs Jul 3, 2024
6440db5
feat: adds in full collection of abi-allowed int and bytes sizes
bitwise-constructs Jul 11, 2024
5c0a1be
fix: typo fix
bitwise-constructs Jul 11, 2024
3981395
fix: uses typing_extensions.TypeAlias in stead of typing.TypeAlias
bitwise-constructs Jul 11, 2024
1440c29
fix: uses typing_extensions.Annotated instead of typing.Annotated
bitwise-constructs Jul 11, 2024
8501431
chore: updating doc strings and removing unnecessary method
bitwise-constructs Aug 21, 2024
2808750
chore: merge
bitwise-constructs Aug 21, 2024
d2e45d8
Merge branch 'main' of https://github.com/ApeWorX/eth-pydantic-types …
bitwise-constructs Oct 14, 2024
5ba7b41
feat: use enriched HexBytes instead of imported one
bitwise-constructs Dec 16, 2024
fac6f71
Merge branch 'main' of github.com:ApeWorX/eth-pydantic-types into fea…
bitwise-constructs Dec 18, 2024
0ddc42d
chore: lint
bitwise-constructs Dec 18, 2024
02ecb45
chore: lin[d]t truffles
bitwise-constructs Dec 18, 2024
b9aa930
chore: typo fix
bitwise-constructs Dec 18, 2024
8ddb9cc
feat: fix tests
bitwise-constructs Dec 18, 2024
4fe3cfd
chore: updates pins
bitwise-constructs Dec 19, 2024
ff72ed7
feat: flip the evaluation of signed/unsigned range to do true conditi…
bitwise-constructs Dec 19, 2024
3ec3cfe
refactor: cleaner way to have addresses keep a static schema pattern/…
bitwise-constructs Dec 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/commitlint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.10"

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
contents: write

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.10"

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/prtitle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
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.10"

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.10"

Expand Down
18 changes: 9 additions & 9 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
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.10"

Expand All @@ -42,10 +42,10 @@ jobs:
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.10"

Expand All @@ -63,18 +63,18 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: [3.8, 3.9, "3.10", "3.11", "3.12"]
python-version: [3.9, "3.10", "3.11", "3.12"]

env:
GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -94,10 +94,10 @@ jobs:
# fail-fast: true
#
# 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.10"
#
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 24.3.0
rev: 24.8.0
bitwise-constructs marked this conversation as resolved.
Show resolved Hide resolved
hooks:
- id: black
name: black

- repo: https://github.com/pycqa/flake8
rev: 7.0.0
rev: 7.1.1
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
rev: v1.11.1
bitwise-constructs marked this conversation as resolved.
Show resolved Hide resolved
hooks:
- id: mypy
additional_dependencies: [types-setuptools, pydantic]
Expand Down
3 changes: 2 additions & 1 deletion eth_pydantic_types/_error.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Callable
from collections.abc import Callable
from typing import Any

from pydantic_core import PydanticCustomError

Expand Down
5 changes: 2 additions & 3 deletions eth_pydantic_types/address.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, ClassVar, Optional, Tuple, cast
from typing import Annotated, Any, ClassVar, Optional, cast

from eth_typing import ChecksumAddress
from eth_utils import is_checksum_address, to_checksum_address
Expand All @@ -8,7 +8,6 @@
str_schema,
with_info_before_validator_function,
)
from typing_extensions import Annotated

from .hex import HexStr20

Expand All @@ -26,7 +25,7 @@ class Address(HexStr20):
"""

schema_pattern: ClassVar[str] = ADDRESS_PATTERN
schema_examples: ClassVar[Tuple[str, ...]] = (
schema_examples: ClassVar[tuple[str, ...]] = (
"0x0000000000000000000000000000000000000000", # Zero address
"0x02c84e944F97F4A4f60221e6fb5d5DbAE49c7aaB", # Leading zero
"0xa5a13f62ce1113838e0d9b4559b8caf5f76463c0", # Trailing zero
Expand Down
6 changes: 3 additions & 3 deletions eth_pydantic_types/bip122.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum
from functools import cached_property
from typing import Any, Optional, Tuple
from typing import Any, Optional

from pydantic_core import CoreSchema
from pydantic_core.core_schema import (
Expand Down Expand Up @@ -50,7 +50,7 @@ def __eth_pydantic_validate__(cls, value: Any, info: Optional[ValidationInfo] =
return f"{cls.prefix}{genesis_hash[2:]}/{block_keyword.value}/{block_hash[2:]}"

@classmethod
def parse(cls, value: str) -> Tuple[str, Bip122UriType, str]:
def parse(cls, value: str) -> tuple[str, Bip122UriType, str]:
protocol_suffix = value.replace(cls.prefix, "")
protocol_parsed = protocol_suffix.split("/")
if len(protocol_parsed) != 3:
Expand All @@ -68,7 +68,7 @@ def parse(cls, value: str) -> Tuple[str, Bip122UriType, str]:
)

@cached_property
def parsed(self) -> Tuple[str, Bip122UriType, str]:
def parsed(self) -> tuple[str, Bip122UriType, str]:
return self.parse(self)

@property
Expand Down
25 changes: 13 additions & 12 deletions eth_pydantic_types/hex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, ClassVar, Optional, Tuple, Union, cast
from typing import Any, ClassVar, Optional, Union, cast

from eth_typing import HexStr as EthTypingHexStr
from eth_utils import add_0x_prefix
Expand Down Expand Up @@ -38,7 +38,7 @@
class BaseHex:
size: ClassVar[int] = 0
Copy link
Member

Choose a reason for hiding this comment

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

So I am working on core right, and I recently realized I really need a HexInt class.
Basically it allows any hex-str or or bytes but always validates into an integer.

I am going to make one in core ape for now but eventually we can use this work to double-import.

schema_pattern: ClassVar[str] = schema_pattern
schema_examples: ClassVar[Tuple[str, ...]] = schema_examples
schema_examples: ClassVar[tuple[str, ...]] = schema_examples

@classmethod
def __get_pydantic_json_schema__(cls, core_schema, handler):
Expand Down Expand Up @@ -81,6 +81,11 @@ def validate_size(cls, value: bytes) -> bytes:


class BoundHexBytes(HexBytes):
"""
Use when receiving ``hexbytes.HexBytes`` values and a specific size is required.
Includes a pydantic validator and serializer.
"""

size: ClassVar[int] = 32

@classmethod
Expand All @@ -92,12 +97,6 @@ def __get_pydantic_core_schema__(cls, value, handle=None) -> CoreSchema:
schema["serialization"] = hex_serializer
return schema

@classmethod
def __eth_pydantic_validate__(
cls, value: Any, info: Optional[ValidationInfo] = None
) -> BaseHexBytes:
return cls(cls.validate_size(HexBytes(value)))

@classmethod
def validate_size(cls, value: bytes) -> bytes:
str_size = cls.size * 2
Expand All @@ -124,8 +123,9 @@ def __eth_pydantic_validate__(cls, value):

@classmethod
def from_bytes(cls, data: bytes) -> "BaseHexStr":
hex_str = data.hex()
return cls(hex_str if hex_str.startswith("0x") else hex_str)
hex_value = data.hex()
hex_str = hex_value if hex_value.startswith("0x") else f"0x{hex_value}"
return cls(hex_str)

@classmethod
def validate_hex(cls, data: Union[bytes, str, int]):
Expand All @@ -136,7 +136,8 @@ def validate_hex(cls, data: Union[bytes, str, int]):
return validate_hex_str(data)

elif isinstance(data, int):
return BaseHexBytes(data).hex()
hex_value = BaseHexBytes(data).hex()
return hex_value if hex_value.startswith("0x") else f"0x{hex_value}"

raise HexValueError(data)

Expand Down Expand Up @@ -172,7 +173,7 @@ def from_bytes(cls, data: bytes) -> "HexStr":


class BoundHexStr(BaseHexStr):
"""A hex string value, typically from a hash."""
"""A hex string value, typically from a hash, that is required to be a specific size."""

size: ClassVar[int] = 32
calculate_schema: ClassVar[bool] = True
Expand Down
8 changes: 7 additions & 1 deletion eth_pydantic_types/serializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from pydantic_core.core_schema import plain_serializer_function_ser_schema

hex_serializer = plain_serializer_function_ser_schema(function=lambda x: x.hex())

def serialize_hex(value: bytes):
hex_value = value.hex()
return hex_value if hex_value.startswith("0x") else f"0x{hex_value}"


hex_serializer = plain_serializer_function_ser_schema(function=serialize_hex)
7 changes: 4 additions & 3 deletions eth_pydantic_types/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Callable, Dict, Optional, Sized, Tuple, TypeVar, cast
from collections.abc import Sized
from typing import Any, Callable, Optional, TypeVar, cast

from pydantic import WithJsonSchema
from pydantic_core.core_schema import bytes_schema
Expand All @@ -11,7 +12,7 @@
class WithBytesSchema(WithJsonSchema):
def __init__(self, **kwargs):
mode = kwargs.pop("mode", None)
schema = cast(Dict[str, Any], bytes_schema(**kwargs))
schema = cast(dict[str, Any], bytes_schema(**kwargs))
super().__init__(schema, mode=mode)


Expand Down Expand Up @@ -90,7 +91,7 @@ def get_hash_pattern(str_size: int) -> str:
return f"^0x[a-fA-F0-9]{{{str_size}}}$"


def get_hash_examples(str_size: int) -> Tuple[str, str, str, str]:
def get_hash_examples(str_size: int) -> tuple[str, str, str, str]:
zero_hash = f"0x{'0' * str_size}"
leading_zero = f"0x01{'1e' * ((str_size - 1) // 2)}"
trailing_zero = f"0x{'1e' * ((str_size - 1) // 2)}10"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ write_to = "eth_pydantic_types/version.py"
# character.
[tool.black]
line-length = 100
target-version = ['py38', 'py39', 'py310']
target-version = ['py39', 'py310', 'py311', 'py312']
include = '\.pyi?$'

[tool.pytest.ini_options]
Expand Down
17 changes: 8 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"pytest-mock", # For creating mocks
"hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer
"hypothesis-jsonschema==0.19.0", # JSON Schema fuzzer extension
"eth-hash[pycryptodome]", # For backends to work
],
"lint": [
"black>=24.3.0,<25", # Auto-formatter and linter
"mypy>=1.9.0,<2", # Static type analyzer
"black>=24.8.0,<25", # Auto-formatter and linter
"mypy>=1.11.1,<2", # Static type analyzer
"types-setuptools", # Needed for mypy type shed
"flake8>=7.0.0,<8", # Style linter
"flake8>=7.1.1,<8", # Style linter
"flake8-breakpoint>=1.1.0,<2", # Detect breakpoints left in code
"flake8-print>=5.0.0,<6", # Detect print statements left in code
"isort>=5.10.1,<6", # Import sorting linter
Expand Down Expand Up @@ -74,13 +75,12 @@
include_package_data=True,
install_requires=[
"hexbytes>=1.2.0,<2",
"eth-hash[pycryptodome]>=0.5.2,<1",
"eth-utils>=2.2.0,<5",
"eth-typing>=3.5.0,<5",
"pydantic>=2.4.2,<3",
"eth-utils>=2.3.1,<6",
"eth-typing>=3.5.2,<6",
"pydantic>=2.5.2,<3",
"typing_extensions>=4.8.0,<5",
],
python_requires=">=3.8,<4",
python_requires=">=3.9,<4",
extras_require=extras_require,
py_modules=["eth_pydantic_types"],
license="Apache-2.0",
Expand All @@ -97,7 +97,6 @@
"Operating System :: POSIX",
"Operating System :: Microsoft :: Windows",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Expand Down
3 changes: 2 additions & 1 deletion tests/test_hex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from eth_utils import to_hex
from hexbytes import HexBytes as BaseHexBytes
from pydantic import BaseModel, ValidationError

Expand All @@ -19,7 +20,7 @@ def test_hexbytes(value):

# The end result, the value is a hexbytes.HexBytes
assert actual.value == BaseHexBytes(value)
assert actual.value.hex() == "0a"
assert to_hex(actual.value) == "0x0a"
assert isinstance(actual.value, bytes)
assert isinstance(actual.value, BaseHexBytes)

Expand Down
Loading