-
Notifications
You must be signed in to change notification settings - Fork 218
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
validate geth_kwargs
with a pydantic
model
#199
validate geth_kwargs
with a pydantic
model
#199
Conversation
07c1d12
to
f4e245f
Compare
geth_kwargs
to a pydantic
modelgeth_kwargs
to a pydantic
model
f4e245f
to
911c04c
Compare
911c04c
to
6cbc919
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changing **geth_kwargs
in the signature to geth_kwargs
changes the API from accepting any number of arguments to only accepting one. Was that an intentional choice? Curious what the tradeoffs you considered were. I think this change would have relatively far-reaching impact, so I'm not against it exactly, but want to make sure we're doing it with purpose and intention.
Indeed!
My assumption/understanding This does introduce the need to collate it as the pydantic model geth_kwargs_model = GethKwargs(key_a=True, key_b="cats", **geth_kwargs) It introduces an extra step, but that step allows all the kwargs to be type-checked. The other option considered was to use a |
Can we do something like: def get_accounts(
data_dir: str, **geth_kwargs: GethKwargs
) -> tuple[str, ...] | tuple[()]:
"""
Returns all geth accounts as tuple of hex encoded strings
>>> get_accounts()
... ('0x...', '0x...')
"""
geth_kwargs = GethKwargs(**geth_kwargs)
geth_kwargs.data_dir = data_dir
geth_kwargs.suffix_args = ["account", "list"]
... |
We could go that way. Internal calls to Edit: thought about it more. I had approached this with the idea that eliminating the passing around of |
194b23f
to
5478dd4
Compare
geth_kwargs
to a pydantic
modelgeth_kwargs
with a pydantic
model
geth_kwargs
with a pydantic
modelgeth_kwargs
with a pydantic
model
78d0aba
to
ee2724c
Compare
663caac
to
a66ed52
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm 👍🏼
) | ||
|
||
|
||
class GethKwargs(BaseModel): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder actually if we should use a pattern like
from pydantic import create_model
# create a type for geth kwargs based off TypedDict so we can use `Unpack[GethKwargs]` in method signatures
class GethKwargs(TypedDict, total=False):
arg1: str
arg2: bool
# etc...
# create the pydantic model for validation (and use this like we are currently using `GethKwargs` in this PR)
GethKwargsModel = create_model('GethKwargsModel', **GethKwargs)
... and then we can use Unpack[GethKwargs]
instead of Any
for typing them in method signatures. Any thoughts on that or alternate ways to not have to use Any
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Kind of pseudocode above... but unpack the kwargs into a pydantic model separately.
kwargs_fields = {k: v for k, v in GethKwargs.__annotations__.items()}
GethKwargsModel = create_model('GethKwargsModel', **kwargs_fields)
Edited to reflect optional kwargs, not required (total=False
and {k: v}
instead of {k, (v, ...)}
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed elsewhere, but validating with a GethKwargs TypedDict causes argument overlap issues in functions such as get_accounts
that have args that are also keys in geth_kwargs
. Shelving for now.
de618bd
to
a66ed52
Compare
What was wrong?
Currently
geth_kwargs
are passed around as adict
which does not allow type checking.I considered using using
TypedDict
, butpydantic
offers more flexibility in allowing the user to add additional values to be passed through togeth
.Added pydantic as a dependency and added
geth/utils/validation.py
to house the newGethKwargs
pydantic model- it's currently just a dump of all the geth commands I could find, will be cleaned up in a future PR.Adding types for
geth/accounts.py
Validate typing of
geth_kwargs
inaccounts.py
andreset.py
and some directly connected functions.Changed return type of functions in
geth/accounts.py
from bytes to string. gethstdout
andstderr
are received as bytes, but otherwise it makes no sense to keep passing them around as bytes within py-geth, just a source of potential confusion. Updated tests that checked for byte-string versions of returned accounts.Bumped
mypy
tov1.10.0
.Add
"eval_type_backport>=0.1.0; python_version < '3.10'"
to install_requires due to pydantic + future annotations issue: future annotations with Python 3.8 cause TypeError pydantic/pydantic#7873How was it fixed?
Todo:
Cute Animal Picture