-
Notifications
You must be signed in to change notification settings - Fork 286
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
RLP fields using annotated class attributes #109
base: main
Are you sure you want to change the base?
Conversation
Dropping support for py3.5 and pypy3 is not backwards compatible. It would require a major version bump. (I'm not necessarily opposed to that, but it does come with costs) Why allow duplicate definitions of attributes? That seems quite surprising to me. |
We won't need to drop support for py3.5 or pypy3 in the code. It's just that if you're using the new annotation syntax for defining RLP fields, you must use Python 3.6.
I don't understand this remark. Could you please give an example? |
Okay, cool. I'll do a deeper review when py3.5 tests are succeeding then 👍
Yeah, I was referring to:
|
Something that I think needs to be addressed before this can be considered is constricting the definition of what a field is. Currently this library (and pull request) has used I'd consider the cleanup of converting all sedes to inherit from a base class and getting all the |
This may be getting somewhat off topic, but in addition to what you said we should also distinguish between |
Okay. I think I just need to find a clever way of skipping the (parametrized) test cases that use annotation syntax on py3.5.
I didn't find a good way to circumvent this. Python stores the annotations in a dictionary, so only one of the fields will actually get considered. The following snippet shows the observed behavior: class Txn(rlp.Serializable):
sender: sedes.text
receiver: sedes.text
sender: sedes.text
amount: big_endian_int
>>> Txn.__annotations__
{'sender': big_endian_int, receiver: sedes.text, amount: big_endian_int} Note that in terms of order, |
I think this is less of an issue with class attributes compared to the fields list as mypy would complain about this (whereas it doesn't for the old |
I think this just comes down to syntax and language constraints. I think that the following two produce identical AST. class A:
field_a: int
field_b: str
field_a: bool
class B:
field_b: str
field_a: bool So as far as I can tell we are unable to enforce this restriction since as far as python is concerned, there is not a duplicate field. It's only our visual inspection of the code makes it appear the field is duplicated when in fact the second declaration is overwriting the first at the lowest level of the language. So 👍 for having a test that shows that this in fact works as expected and as long as we're enforcing the uniqueness of fields at the internal levels I'm 👍 |
What was wrong?
See #44
How was it fixed?
The feature and the desired behavior has been discussed at length in #44. The implementation in this PR is based on PEP 526 -- Syntax for Variable Annotations.
In the
SerializableBase
metaclass, I am inspecting the__annotations__
attribute of RLP classes to filter Sede objects, and pass them as fields while constructing the RLP class.Important notes
TypeError
anymore. For example, the following RLP class definition is considered valid:("sender", "receiver", "amount",)
.Because the syntax for annotated class attributes is not supported in Python < 3.6.
Cute Animal Picture