Skip to content

Commit

Permalink
typing improvements: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bilbeyt committed Jul 5, 2023
1 parent 74349a4 commit 6d532f0
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ ignored-classes=
ignored-modules=

# List of members which are set dynamically and missed by pylint inference system
generated-members=ape.reverts
generated-members=ape.reverts,web3.types.TxReceipt.status
12 changes: 5 additions & 7 deletions beamer/agent/chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def fill_request(request: Request, context: Context) -> None:
context.logger.info(
"Filled request",
request=request,
txn_hash=receipt.transactionHash.hex(), # type: ignore
txn_hash=receipt.transactionHash.hex(),
token=token.functions.symbol().call(),
)

Expand Down Expand Up @@ -452,7 +452,7 @@ def claim_request(request: Request, context: Context) -> None:
context.logger.info(
"Claimed request",
request=request,
txn_hash=receipt.transactionHash.hex(), # type: ignore
txn_hash=receipt.transactionHash.hex(),
)


Expand Down Expand Up @@ -520,7 +520,7 @@ def maybe_challenge(claim: Claim, context: Context) -> bool:
context.logger.info(
"Challenged claim",
claim=claim,
txn_hash=receipt.transactionHash.hex(), # type: ignore
txn_hash=receipt.transactionHash.hex(),
)

return True
Expand Down Expand Up @@ -770,9 +770,7 @@ def _withdraw(claim: Claim, context: Context) -> None:
return

claim.transaction_pending = True
context.logger.info(
"Withdrew", claim=claim.id, txn_hash=receipt.transactionHash.hex() # type: ignore
)
context.logger.info("Withdrew", claim=claim.id, txn_hash=receipt.transactionHash.hex())


def _invalidate(request: Request, claim: Claim, context: Context) -> None:
Expand All @@ -790,5 +788,5 @@ def _invalidate(request: Request, claim: Claim, context: Context) -> None:
request=request.id,
fill_id=claim.fill_id,
claim=claim.id,
txn_hash=receipt.transactionHash.hex(), # type: ignore
txn_hash=receipt.transactionHash.hex(),
)
4 changes: 2 additions & 2 deletions beamer/config/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,5 +404,5 @@ def write(
log.error("Transaction failed", exc=exc)
sys.exit(1)
else:
txhash = receipt.transactionHash.hex() # type: ignore
log.info("Transaction sent", block=receipt.blockNumber, txhash=txhash) # type: ignore
txhash = receipt.transactionHash.hex()
log.info("Transaction sent", block=receipt.blockNumber, txhash=txhash)
2 changes: 1 addition & 1 deletion beamer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def transact(
else:
break

if receipt.status == 0: # type: ignore
if receipt.status == 0:
raise TransactionFailed("unknown error")
return receipt

Expand Down
30 changes: 30 additions & 0 deletions plugins/mypy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Optional, Callable
from mypy.nodes import Var, SymbolTableNode, MDEF
from mypy.plugin import Plugin, AnalyzeTypeContext


class TxReceiptAttributePlugin(Plugin):

def get_type_analyze_hook(self, fullname: str) -> Optional[Callable]:
def func(ctx: AnalyzeTypeContext) -> None:
symbol_table = ctx.api.lookup_fully_qualified('web3.types.TxReceipt')
names = symbol_table.node.names
type_items = symbol_table.node.typeddict_type.items
for attr_name, attr_type in type_items.items():
if getattr(attr_type, "items", None):
node = Var(attr_name, attr_type)
node.info = attr_type.items[0].type
else:
node = Var(attr_name, attr_type)
node.info = attr_type.type
names[node.name] = SymbolTableNode(MDEF, node)
breakpoint()
return symbol_table.node.typeddict_type

if 'TxReceipt' in fullname:
return func
return None


def plugin(version: str) -> Plugin:
return TxReceiptAttributePlugin
20 changes: 20 additions & 0 deletions plugins/pylint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from typing import TYPE_CHECKING

from astroid import MANAGER, Call

if TYPE_CHECKING:
from pylint.lint import PyLinter

print("plugin loaded")

def register(linter: "PyLinter") -> None:
"""This required method auto registers the checker during initialization.
:param linter: The linter to register the checker to.
"""
print('register')

def transform(node):
print('transform ran')

MANAGER.register_transform(Call, transform)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ warn_unused_ignores = true
strict_equality = true
disallow_untyped_defs = true
explicit_package_bases = true
plugins = "plugins.mypy"

[[tool.mypy.overrides]]
module = ["beamer.tests.*"]
Expand Down
4 changes: 4 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def func():
pass

func()

0 comments on commit 6d532f0

Please sign in to comment.