-
Notifications
You must be signed in to change notification settings - Fork 131
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
Allow various methods to accept a union of Expr, int, and/or str #152
base: master
Are you sure you want to change the base?
Conversation
I don't like the idea of mixing abstraction layers like this. Sure it's convenient but there is a subtle problem when trying to convert between representations of data. Consider: With this PR if you're not fluent in TEAL/PyTEAL it will be hard to tell if However using There are a couple of things you can do right now to make the experience better:
I'd be on board if the Union allowed |
I could say the same thing with explicit To me, your comment addresses If this PR was trying to turn |
After thinking about this, I think you have a point. |
Currently, any method that expects a
pyteal.Bytes
orpyteal.Int
object will throw an exception when astr
orint
is given. This can make some method calls more verbose than they really need to be. For example,App.globalGet("Key")
is code that one would reasonable expect to work, but since"key"
is astr
and notpyteal.Bytes
, it throws an exception .This PR implements a function,
get_teal_type
that will return apyteal.Int
orpyteal.Bytes
object given aint
orstr
, respectively. As demonstrated in the test,App.globalGet("Key")
now behaves the same asApp.globalGet(Bytes("Key"))
.Caveats
allow_redefinition = True
was added tomypy.ini
to prevent mypy from complaining about incorrect types being passed down. Sinceget_teal_type
will always return a valid type or throw an exception this isn't a legitimate concern. It's possible that the implementation of these calls could be properly casted to appease mypy, but I think allowing redefinition is a more elegant solution. The reason mypy doesn't allow redefinition by default is to improve readability, but having a bunch of casts would likely hurt readability more than help it.I also put this function in it's own file because I wasn't sure where else it would fit. If there's a preexisting file that this could go into, we should probably do so.