Replace data_schema classes with more resilient ones - #2
Open
nonowazu wants to merge 1 commit into
Open
Conversation
This changes out all the classes in `ida/data_schema.py` with dataclasses with several benefits:
* These are drop-in replacements to the existing classes
* You avoid a multable default argument issue with every argument that's `foo=[]`
At current, if you instantate a class with the default, a second class with the default, append to the first instance's list, it will appear in the second instance; `default_factory` avoids this
* You get `__repr__` for free so if you're examining in the console or just `print(foo)`, you'll get poentially more useful output
* Not relevant here I don't think, but you get `__eq__` for free too
* Gets rid of `# type:` comments since python has had relevant types for the classes for probably a decade now
* Removes a `pass` in `DefinedData` that was actually useless
I checked to make sure that nothing is attempting to mutate things like `instances` as a feature instead of a bug and I didn't see any at first glance.
Author
|
As a note, if you don't accept this PR (perfectly fine - your repo and all), at least fix the mutable shared default by swapping it out with def __init__(self, some_arg: list[X] = []):
self.some_arg = some_argWith something like def __init__(self, some_arg: Optional[list[X]] = None):
self.some_arg = some_arg if some_arg is not None else []The reason for the gross ternary here is because some people may pass a (falsy) list reference expecting to be able to mutate that upstream reference and get it for free in the object. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This changes out all the classes in
ida/data_schema.pywith dataclasses with several benefits:These are drop-in replacements to the existing classes
You avoid a mutable default argument issue with every argument that's
foo=[]At current, if you instantiate a class with the default, a second class with the default, append to the first instance's list, it will appear in the second instance;
default_factoryavoids thisYou get
__repr__for free so if you're examining in the console or justprint(foo), you'll get poentially more useful outputNot relevant here I don't think, but you get
__eq__for free tooGets rid of
# type:comments since python has had relevant types for the classes for probably a decade nowRemoves a
passinDefinedDatathat was actually uselessI checked to make sure that nothing is attempting to mutate things like
instancesas a feature instead of a bug and I didn't see any at first glance.Obviously, feedback etc appreciated.