Skip to content

Commit

Permalink
chore: utilize flake8-pydantic (#2199)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey authored Aug 1, 2024
1 parent 7caaf4d commit 5f8bbfe
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ repos:
rev: 7.1.0
hooks:
- id: flake8
additional_dependencies: [flake8-breakpoint, flake8-print, flake8-pydantic]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.11.0
rev: v1.11.1
hooks:
- id: mypy
additional_dependencies: [
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ exclude =
build
.eggs
tests/integration/cli/projects
ignore = E704,W503,PYD002
per-file-ignores =
# Need signal handler before imports
src/ape/__init__.py: E402
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"lint": [
"black>=24.4.2,<25", # Auto-formatter and linter
"mypy>=1.11.0,<2", # Static type analyzer
"mypy>=1.11.1,<2", # Static type analyzer
"types-PyYAML", # Needed due to mypy typeshed
"types-requests", # Needed due to mypy typeshed
"types-setuptools", # Needed due to mypy typeshed
Expand All @@ -33,6 +33,7 @@
"flake8>=7.1.0,<8", # Style linter
"flake8-breakpoint>=1.1.0,<2", # Detect breakpoints left in code
"flake8-print>=4.0.1,<5", # Detect print statements left in code
"flake8-pydantic", # For detecting issues with Pydantic models
"isort>=5.13.2,<6", # Import sorting linter
"mdformat>=0.7.17", # Auto-formatter for markdown
"mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown
Expand Down
4 changes: 3 additions & 1 deletion src/ape/api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ def __init__(self, *args, **kwargs):
Note: The actual dependency classes are decoded later.
"""

deployment_data: dict[str, dict[str, list[DeploymentConfig]]] = Field({}, alias="deployments")
deployment_data: dict[str, dict[str, list[DeploymentConfig]]] = Field(
default_factory=dict, alias="deployments"
)
"""
Data for deployed contracts from the project.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/ape/api/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DependencyAPI(BaseInterfaceModel):
The package-name of the dependency.
"""

config_override: dict = Field({}, repr=False)
config_override: dict = Field(default_factory=dict, repr=False)
"""
Set different config than what Ape can deduce.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/ape/api/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class BlockAPI(BaseInterfaceModel):
The preceeding block's hash.
"""
parent_hash: Any = Field(
EMPTY_BYTES32, alias="parentHash"
default=EMPTY_BYTES32, alias="parentHash"
) # NOTE: genesis block has no parent hash

"""
Expand Down
12 changes: 6 additions & 6 deletions src/ape/api/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class TransactionAPI(BaseInterfaceModel):
such as typed-transactions from `EIP-1559 <https://eips.ethereum.org/EIPS/eip-1559>`__.
"""

chain_id: Optional[int] = Field(0, alias="chainId")
receiver: Optional[AddressType] = Field(None, alias="to")
sender: Optional[AddressType] = Field(None, alias="from")
gas_limit: Optional[int] = Field(None, alias="gas")
chain_id: Optional[int] = Field(default=0, alias="chainId")
receiver: Optional[AddressType] = Field(default=None, alias="to")
sender: Optional[AddressType] = Field(default=None, alias="from")
gas_limit: Optional[int] = Field(default=None, alias="gas")
nonce: Optional[int] = None # NOTE: `Optional` only to denote using default behavior
value: int = 0
data: HexBytes = HexBytes("")
Expand All @@ -63,9 +63,9 @@ class TransactionAPI(BaseInterfaceModel):
max_priority_fee: Optional[int] = None

# If left as None, will get set to the network's default required confirmations.
required_confirmations: Optional[int] = Field(None, exclude=True)
required_confirmations: Optional[int] = Field(default=None, exclude=True)

signature: Optional[TransactionSignature] = Field(None, exclude=True)
signature: Optional[TransactionSignature] = Field(default=None, exclude=True)

model_config = ConfigDict(populate_by_name=True)

Expand Down
6 changes: 3 additions & 3 deletions src/ape_ethereum/ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,15 @@ class Block(BlockAPI):

gas_limit: int = Field(alias="gasLimit")
gas_used: int = Field(alias="gasUsed")
base_fee: int = Field(0, alias="baseFeePerGas")
base_fee: int = Field(default=0, alias="baseFeePerGas")
difficulty: int = 0
total_difficulty: int = Field(0, alias="totalDifficulty")
total_difficulty: int = Field(default=0, alias="totalDifficulty")
uncles: list[HexBytes] = []

# Type re-declares.
hash: Optional[HexBytes] = None
parent_hash: HexBytes = Field(
EMPTY_BYTES32, alias="parentHash"
default=EMPTY_BYTES32, alias="parentHash"
) # NOTE: genesis block has no parent hash

@field_validator(
Expand Down
24 changes: 12 additions & 12 deletions src/ape_ethereum/transactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ class StaticFeeTransaction(BaseTransaction):
Transactions that are pre-EIP-1559 and use the ``gasPrice`` field.
"""

gas_price: Optional[int] = Field(None, alias="gasPrice")
max_priority_fee: Optional[int] = Field(None, exclude=True) # type: ignore
type: int = Field(TransactionType.STATIC.value, exclude=True)
max_fee: Optional[int] = Field(None, exclude=True) # type: ignore
gas_price: Optional[int] = Field(default=None, alias="gasPrice")
max_priority_fee: Optional[int] = Field(default=None, exclude=True) # type: ignore
type: int = Field(default=TransactionType.STATIC.value, exclude=True)
max_fee: Optional[int] = Field(default=None, exclude=True) # type: ignore

@model_validator(mode="before")
@classmethod
Expand All @@ -136,8 +136,8 @@ class AccessListTransaction(StaticFeeTransaction):
transactions are similar to legacy transaction with an added access list functionality.
"""

gas_price: Optional[int] = Field(None, alias="gasPrice")
type: int = Field(TransactionType.ACCESS_LIST.value)
gas_price: Optional[int] = Field(default=None, alias="gasPrice")
type: int = TransactionType.ACCESS_LIST.value
access_list: list[AccessList] = Field(default_factory=list, alias="accessList")

@field_validator("type")
Expand All @@ -152,9 +152,9 @@ class DynamicFeeTransaction(BaseTransaction):
and ``maxPriorityFeePerGas`` fields.
"""

max_priority_fee: Optional[int] = Field(None, alias="maxPriorityFeePerGas") # type: ignore
max_fee: Optional[int] = Field(None, alias="maxFeePerGas") # type: ignore
type: int = Field(TransactionType.DYNAMIC.value)
max_priority_fee: Optional[int] = Field(default=None, alias="maxPriorityFeePerGas")
max_fee: Optional[int] = Field(default=None, alias="maxFeePerGas")
type: int = TransactionType.DYNAMIC.value
access_list: list[AccessList] = Field(default_factory=list, alias="accessList")

@field_validator("type")
Expand All @@ -168,13 +168,13 @@ class SharedBlobTransaction(DynamicFeeTransaction):
`EIP-4844 <https://eips.ethereum.org/EIPS/eip-4844>`__ transactions.
"""

max_fee_per_blob_gas: int = Field(0, alias="maxFeePerBlobGas")
blob_versioned_hashes: list[HexBytes] = Field([], alias="blobVersionedHashes")
max_fee_per_blob_gas: int = Field(default=0, alias="maxFeePerBlobGas")
blob_versioned_hashes: list[HexBytes] = Field(default_factory=list, alias="blobVersionedHashes")

"""
Overridden because EIP-4844 states it cannot be nil.
"""
receiver: AddressType = Field(ZERO_ADDRESS, alias="to")
receiver: AddressType = Field(default=ZERO_ADDRESS, alias="to")

@field_validator("max_fee_per_blob_gas", mode="before")
@classmethod
Expand Down
4 changes: 2 additions & 2 deletions tests/functional/test_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def test_data_when_contains_whitespace():


def test_model_dump_excludes_none_values():
txn = StaticFeeTransaction()
txn = StaticFeeTransaction(sender=None)
txn.value = 1000000
actual = txn.model_dump()
assert "value" in actual
Expand All @@ -285,7 +285,7 @@ def test_model_dump_access_list():
],
}
]
txn = AccessListTransaction(access_list=access_list)
txn = AccessListTransaction(access_list=access_list, sender=None)
actual = txn.model_dump(exclude_none=True, by_alias=True)
assert actual is not None

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/cli/test_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_test(setup_pytester, integ_project, pytester, eth_tester_provider):
from ape.logging import logger

logger.set_level("DEBUG")
result = pytester.runpytest_subprocess(timeout=30)
result = pytester.runpytest_subprocess(timeout=120)
try:
result.assert_outcomes(passed=passed, failed=failed), "\n".join(result.outlines)
except ValueError:
Expand All @@ -189,7 +189,7 @@ def test_test(setup_pytester, integ_project, pytester, eth_tester_provider):
def test_uncaught_txn_err(setup_pytester, integ_project, pytester, eth_tester_provider):
_ = eth_tester_provider # Ensure using EthTester for this test.
setup_pytester(integ_project)
result = pytester.runpytest_subprocess(timeout=30)
result = pytester.runpytest_subprocess(timeout=120)
expected = """
contract_in_test.setNumber(5, sender=owner)
E ape.exceptions.ContractLogicError: Transaction failed.
Expand Down Expand Up @@ -280,7 +280,7 @@ def test_gas_when_estimating(geth_provider, setup_pytester, integ_project, pytes
geth_account.transfer(geth_account, "1 wei") # Force a clean block.
with integ_project.temp_config(**cfg):
passed, failed = setup_pytester(integ_project)
result = pytester.runpytest_subprocess(timeout=30)
result = pytester.runpytest_subprocess(timeout=120)
run_gas_test(result, passed, failed)


Expand Down

0 comments on commit 5f8bbfe

Please sign in to comment.