Skip to content
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

Merged
merged 1 commit into from
May 20, 2024

Conversation

souliane
Copy link
Contributor

@souliane souliane commented May 5, 2024

Also add extras field to common.PoetryPackage. This allows to clean update_or_remove_additional_deps in sync_hooks_additional_dependencies.py

Successful PR Checklist:

  • Tests
    • (not applicable?)
  • Documentation
    • (not applicable?)

PR label(s):

@souliane souliane requested a review from ewjoachim as a code owner May 5, 2024 02:13
Copy link
Owner

@ewjoachim ewjoachim left a 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 :)

Comment on lines 33 to 37
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}"
Copy link
Owner

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?

Copy link
Contributor Author

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__.

Copy link
Owner

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 of PoetryPackage
You can use dataclass(order=True) for this

Copy link
Contributor Author

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__.

Copy link
Contributor Author

@souliane souliane May 6, 2024

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 :-)

@souliane souliane force-pushed the refactor-get-poetry-deps branch 2 times, most recently from 5405828 to 96c2947 Compare May 6, 2024 12:55
Also add `extras` field to `common.PoetryPackage`.
This allows to clean `update_or_remove_additional_deps` in
`sync_hooks_additional_dependencies.py`
@souliane souliane force-pushed the refactor-get-poetry-deps branch from 96c2947 to 979bc38 Compare May 6, 2024 12:58
@souliane souliane requested a review from ewjoachim May 17, 2024 09:24
Copy link
Owner

@ewjoachim ewjoachim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulations :)

@ewjoachim ewjoachim merged commit ae5d346 into ewjoachim:main May 20, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants