-
Notifications
You must be signed in to change notification settings - Fork 1
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
refactor: get_poetry_deps
leverages common.PoetryPackage
#18
Conversation
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.
Sorry, I can't put my full attention right now, but it's looking awesome :)
poetry_to_pre_commit/common.py
Outdated
def __str__(self) -> str: | ||
# Since the object is frozen, we can cache the result. But applying | ||
# `lru_cache` on `__str__` makes trouble, so we use a cached property. | ||
return self._str | ||
|
||
def __lt__(self, value: object) -> bool: | ||
return str(self) < str(value) | ||
|
||
def __eq__(self, value: object) -> bool: | ||
return str(self) == str(value) | ||
|
||
def __hash__(self) -> int: | ||
return hash(str(self)) | ||
|
||
@cached_property | ||
def _str(self) -> str: | ||
if self.extras: | ||
extras = ",".join(sorted(self.extras)) | ||
return f"{self.name}[{extras}]=={self.version}" | ||
else: | ||
return f"{self.name}=={self.version}" |
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.
Hm? I'm not sure I understand why we need that ? dataclasses already provide this, no?
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.
__hash__
is needed because extras
is unhashable.
__lt__
allows to sort a set of PoetryPackage
.
And if you ovewrite __lt__
, it makes sense to overwrite __eq__
too :-)
I added comments next to __lt__
and __hash__
.
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.
Hm ?
>>> {frozenset(["a", "b"])}
{frozenset({'a', 'b'})}
>>> import dataclasses
>>> @dataclasses.dataclass(frozen=True)
... class X:
... extras: frozenset[str] = dataclasses.field(default_factory=frozenset)
...
>>> {X()}
{X(extras=frozenset())}
Frozensets of strings are hashable, no ?
__lt__
allows to sort a set ofPoetryPackage
You can usedataclass(order=True)
for this
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.
Damn. The problem was that I didn't correctly build the objects in the tests, I was using set
and not frozenset
, and this mislead me. I removed __hash__
.
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.
You can use dataclass(order=True) for this
Nice! No more dunder methods - beside __str__
- in common.PoetryPackage
:-)
5405828
to
96c2947
Compare
Also add `extras` field to `common.PoetryPackage`. This allows to clean `update_or_remove_additional_deps` in `sync_hooks_additional_dependencies.py`
96c2947
to
979bc38
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.
Congratulations :)
Also add
extras
field tocommon.PoetryPackage
. This allows to cleanupdate_or_remove_additional_deps
insync_hooks_additional_dependencies.py
Successful PR Checklist:
PR label(s):