Skip to content

Commit

Permalink
feat: pydantic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Oct 12, 2023
1 parent 798874a commit 4c3511a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.2.0
rev: v4.5.0
hooks:
- id: check-yaml

Expand All @@ -10,24 +10,24 @@ repos:
- id: isort

- repo: https://github.com/psf/black
rev: 23.3.0
rev: 23.9.1
hooks:
- id: black
name: black

- repo: https://github.com/pycqa/flake8
rev: 6.0.0
rev: 6.1.0
hooks:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
rev: v1.6.0
hooks:
- id: mypy
additional_dependencies: [types-setuptools, pydantic==1.10.4]

- repo: https://github.com/executablebooks/mdformat
rev: 0.7.14
rev: 0.7.17
hooks:
- id: mdformat
additional_dependencies: [mdformat-gfm, mdformat-frontmatter]
Expand Down
32 changes: 17 additions & 15 deletions ape_vyper/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from ape.logging import logger
from ape.types import ContractSourceCoverage, ContractType, SourceTraceback, TraceFrame
from ape.utils import GithubClient, cached_property, get_relative_path
from eth_pydantic_types import HexBytes
from eth_utils import is_0x_prefixed
from ethpm_types import ASTNode, HexBytes, PackageManifest, PCMap, SourceMapItem
from ethpm_types import ASTNode, PackageManifest, PCMap, SourceMapItem
from ethpm_types.ast import ASTClassification
from ethpm_types.contract_type import SourceMap
from ethpm_types.source import ContractSource, Function, SourceLocation
Expand Down Expand Up @@ -266,7 +267,9 @@ def import_remapping(self) -> Dict[str, Dict]:
dependencies[remapping] = dependency

for name, ct in (dependency.contract_types or {}).items():
interfaces[f"{key}/{name}.json"] = {"abi": [x.dict() for x in ct.abi]}
interfaces[f"{key}/{name}.json"] = {
"abi": [x.model_dump(mode="json") for x in ct.abi]
}

return interfaces

Expand Down Expand Up @@ -316,7 +319,7 @@ def classify_ast(_node: ASTNode):
}
for name, output in output_items.items():
# De-compress source map to get PC POS map.
ast = ASTNode.parse_obj(result["sources"][source_id]["ast"])
ast = ASTNode.model_validate(result["sources"][source_id]["ast"])
classify_ast(ast)

# Track function offsets.
Expand All @@ -333,7 +336,7 @@ def classify_ast(_node: ASTNode):
evm = output["evm"]
bytecode = evm["deployedBytecode"]
opcodes = bytecode["opcodes"].split(" ")
compressed_src_map = SourceMap(__root__=bytecode["sourceMap"])
compressed_src_map = SourceMap(root=bytecode["sourceMap"])
src_map = list(compressed_src_map.parse())[1:]

pcmap = (
Expand Down Expand Up @@ -501,7 +504,7 @@ def _profile(_name: str, _full_name: str):
# function_name -> (pc, location)
pending_statements: Dict[str, List[Tuple[int, SourceLocation]]] = {}

for pc, item in contract_source.pcmap.__root__.items():
for pc, item in contract_source.pcmap.root.items():
pc_int = int(pc)
if pc_int < 0:
continue
Expand Down Expand Up @@ -531,8 +534,7 @@ def _profile(_name: str, _full_name: str):
continue

if location:
function = contract_source.lookup_function(location)
if not function:
if not (function := contract_source.lookup_function(location)):
# Not sure if this happens.
continue

Expand Down Expand Up @@ -696,7 +698,7 @@ def trace_source(
if source_contract_type := self.project_manager._create_contract_source(contract_type):
return self._get_traceback(source_contract_type, trace, calldata)

return SourceTraceback.parse_obj([])
return SourceTraceback.model_validate([])

def _get_traceback(
self,
Expand All @@ -705,10 +707,10 @@ def _get_traceback(
calldata: HexBytes,
previous_depth: Optional[int] = None,
) -> SourceTraceback:
traceback = SourceTraceback.parse_obj([])
traceback = SourceTraceback.model_validate([])
method_id = HexBytes(calldata[:4])
completed = False
pcmap = PCMap.parse_obj({})
pcmap = PCMap.model_validate({})

for frame in trace:
if frame.op in CALL_OPCODES:
Expand Down Expand Up @@ -767,7 +769,7 @@ def _get_traceback(
is_non_payable_hit = False
if next_frame and next_frame.op == "SSTORE":
push_location = tuple(loc["location"]) # type: ignore
pcmap = PCMap.parse_obj({next_frame.pc: {"location": push_location}})
pcmap = PCMap.model_validate({next_frame.pc: {"location": push_location}})

elif next_frame and next_frame.op in _RETURN_OPCODES:
completed = True
Expand Down Expand Up @@ -931,7 +933,7 @@ def _get_pcmap(bytecode: Dict) -> PCMap:
src_info = bytecode["sourceMapFull"]
pc_data = {pc: {"location": ln} for pc, ln in src_info["pc_pos_map"].items()}
if not pc_data:
return PCMap.parse_obj({})
return PCMap.model_validate({})

# Apply other errors.
errors = src_info["error_map"]
Expand Down Expand Up @@ -988,7 +990,7 @@ def _get_pcmap(bytecode: Dict) -> PCMap:
else:
pc_data[err_pc] = {"dev": f"dev: {error_str}", "location": location}

return PCMap.parse_obj(pc_data)
return PCMap.model_validate(pc_data)


def _get_legacy_pcmap(ast: ASTNode, src_map: List[SourceMapItem], opcodes: List[str]):
Expand Down Expand Up @@ -1078,7 +1080,7 @@ def _get_legacy_pcmap(ast: ASTNode, src_map: List[SourceMapItem], opcodes: List[
item["dev"] = f"dev: {RuntimeErrorType.USER_ASSERT.value}"
break

return PCMap.parse_obj(dict(pc_map_list))
return PCMap.model_validate(dict(pc_map_list))


def _find_non_payable_check(src_map: List[SourceMapItem], opcodes: List[str]) -> Optional[int]:
Expand Down Expand Up @@ -1140,7 +1142,7 @@ def _extend_return(function: Function, traceback: SourceTraceback, last_pc: int,
location = return_ast.line_numbers

last_lineno = max(0, location[2] - 1)
for frameset in traceback.__root__[::-1]:
for frameset in traceback.root[::-1]:
if frameset.end_lineno is not None:
last_lineno = frameset.end_lineno
break
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer
],
"lint": [
"black>=23.3.0,<24", # Auto-formatter and linter
"mypy>=0.991,<1", # Static type analyzer
"black>=23.9.1,<24", # Auto-formatter and linter
"mypy>=1.6.0,<2", # Static type analyzer
"types-setuptools", # Needed due to mypy typeshed
"flake8>=6.0.0,<7", # Style linter
"flake8>=6.1.0,<7", # Style linter
"isort>=5.10.1", # Import sorting linter
"mdformat>=0.7.16", # Auto-formatter for markdown
"mdformat>=0.7.17", # Auto-formatter for markdown
"mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown
"mdformat-frontmatter>=0.4.1", # Needed for frontmatters-style headers in issue templates
],
Expand Down

0 comments on commit 4c3511a

Please sign in to comment.