-
Notifications
You must be signed in to change notification settings - Fork 19
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
Proposes the use of Python's Dataclasses #12
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
- Feature Name: python-dataclasses | ||
- Start Date: 2020-11-10 | ||
- RFC PR: [amundsen-io/rfcs#12](https://github.com/amundsen-io/rfcs/pull/12) (after opening the RFC PR, update this with a link to it and update the file name) | ||
- Amundsen Issue: [amundsen-io/amundsen#0000](https://github.com/amundsen-io/amundsen/issues/0000) (leave this empty for now) | ||
|
||
# Leverage Python's (native) Dataclasses instead of attrs (a third party package) | ||
|
||
## Summary | ||
|
||
- Python 3.7 introduced [dataclasses (PEP557)](https://docs.python.org/3/library/dataclasses.html) .Dataclasses can be a convenient way to generate classes whose primary goal is to contain values. | ||
- The design of dataclasses is based on the pre-existing attr.s library, and are basically a slimmed-down version of attrs. | ||
|
||
- In fact `Hynek Schlawack`, the very same author of attrs, helped with the writing of PEP557. | ||
|
||
## Motivation | ||
|
||
We are relying on `attrs` for (de-)serializing data for each request within Amundsen. | ||
`attrs` itself is a big package with so many different features available that we don't even need. | ||
By using Python's native dataclasses we can have one less dependency with almost the same features `attrs` provide that we intend to use. | ||
|
||
## Guide-level Explanation (aka Product Details) | ||
|
||
- The details about the dataclasses can be found here: https://docs.python.org/3/library/dataclasses.html | ||
- Example of a dataclass: | ||
```python | ||
from dataclasses import dataclass, asdict | ||
|
||
@dataclass | ||
class InventoryItem: | ||
"""Class for keeping track of an item in inventory.""" | ||
name: str | ||
unit_price: float | ||
quantity_on_hand: int = 0 | ||
|
||
def total_cost(self) -> float: | ||
return self.unit_price * self.quantity_on_hand | ||
|
||
inventory = InventoryItem(name='alpha', unit_price=20.0, quantity_on_hand=5) | ||
|
||
# OR directly from a dict | ||
data = {"name": 'alpha', "unit_price": 20.0, "quantity_on_hand": 5} | ||
inventory_alt = InventoryItem(**data) | ||
|
||
# Can access fields directly by dot notations | ||
assert inventory_alt == "alpha" | ||
|
||
# and then convert it back to dict | ||
asdict(inventory_alt) | ||
``` | ||
|
||
## UI/UX-level Explanation | ||
|
||
Nothing changes on the UI/UX. | ||
|
||
## Reference-level Explanation (aka Technical Details) | ||
|
||
- The primary change will happen in amundsencommon repositories where we are defining the models. I believe no other major change | ||
will be required in any other repository. | ||
|
||
- Python 3.6 can still be supported as long as it's needed through backported to py36 ([ericvsmith/dataclasses](https://github.com/ericvsmith/dataclasses)). | ||
|
||
## Drawbacks | ||
|
||
- One of the main thing to consider is the Python 3.6 support. If we go for the dataclasses, we will not be supporting | ||
Python 3.6 anymore. The minimum version requirement for Python for Amundsen will become 3.7. | ||
|
||
- `attrs` supports validation which is not there in Python dataclasses by design. Even though we are not using validations | ||
a lot (in fact, there is only one place in User model where we are using attrs' validation), but still we need to keep | ||
this in mind while making this change. This can be easily replicated within the `__init__` method of a dataclass if needed. | ||
|
||
|
||
## Alternatives | ||
Another package that supports the validation on Python's dataclasses is [Pydantic](https://pydantic-docs.helpmanual.io/). | ||
Again, I personally believe we are not doing any kind of validations in the serializer/deserializers, so not to make things | ||
complex, instead look for a simpler and cleaner solution. | ||
|
||
|
||
## Prior art | ||
N/A | ||
|
||
## Unresolved questions | ||
|
||
- Should we aim for having as less dependencies as possible? That includes the third party packages. | ||
- Do we think of a case where we'll be doing heavy data validations, and if those can not be done with dataclasses? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is ok. |
||
|
||
## Future possibilities | ||
|
||
- We'll be leveraging the native python features, and hence will be learning one new feature i.e., dataclass. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.