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

Add fetchart typing #5213

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

wisp3rwind
Copy link
Member

Add typings to fetchart. there are a few rough edges requiring cast and enums could be better represented, but this is a start.

beetsplug/fetchart.py Outdated Show resolved Hide resolved
@wisp3rwind wisp3rwind force-pushed the fetchart-typing branch 3 times, most recently from ea2debf to 2562146 Compare April 28, 2024 10:58
Add typings to fetchart. there are a few rough edges requiring `cast`
and enums could be better represented, but this is a start.
Copy link
Member

@snejus snejus left a comment

Choose a reason for hiding this comment

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

Mostly looks good. Added a couple of small comments, mentioned the risks of using naked asserts, and mentioned three issues in lines 1476-1490 that non-strict mypy is not flagging for some reason.

beetsplug/fetchart.py Show resolved Hide resolved
beetsplug/fetchart.py Show resolved Hide resolved
beetsplug/fetchart.py Outdated Show resolved Hide resolved
beetsplug/fetchart.py Outdated Show resolved Hide resolved
beetsplug/fetchart.py Outdated Show resolved Hide resolved
beetsplug/fetchart.py Outdated Show resolved Hide resolved
beetsplug/fetchart.py Show resolved Hide resolved
beetsplug/fetchart.py Outdated Show resolved Hide resolved
beetsplug/fetchart.py Outdated Show resolved Hide resolved
beetsplug/fetchart.py Show resolved Hide resolved
@wisp3rwind
Copy link
Member Author

Ok, I think I addressed everything. I went quite far in trying to express things via types, at least for this being Python.

There's one more thing that this reminded me of (but which is out of scope in this PR): At some point, the ImageAction should really be turned into a bitfield/flags rather than an enum (also, this enum might even move into the artresizer module), cf. #4133 (comment)

@wisp3rwind
Copy link
Member Author

I've just seen #5152, which I've completely missed up to now, and which is badly conflicting with what I'm doing here. However, while it's great that there's some momentum on the issue that #5152 tackles, I'm not convinced of the approach.

In contrast, what I'm doing here would fit nicely with eventually pushing the multiple-action image conversion completely to the artresizer.

Copy link
Member

@snejus snejus left a comment

Choose a reason for hiding this comment

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

Types look great! Added a few minor comments and included a consideration regarding the Candidate class design

beetsplug/fetchart.py Show resolved Hide resolved
beetsplug/fetchart.py Show resolved Hide resolved

MATCH_EXACT = 0
MATCH_FALLBACK = 1
_log: Logger
Copy link
Member

Choose a reason for hiding this comment

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

Regarding the design: it seems like Candidate has no other use except for running validation. The attributes that the object has are only required until the end of the validate method, at which point it becomes ValidatedCandidate and becomes either ExactCandidate or InexactCandidate. A couple of points here

  1. It looks to me this should be just a function or a method instead which does not need to hold any state. See the Middle Man code smell
  2. Candidate is the main guy here so I feel like it should be made a bit more relevant by keeping all related logic.
  3. Judging by the class names, the difference between ExactCandidate and InexactCandidate may not be significant enough to justify the need to create two classes.

All in all, I think the validation looks like a great use case for a factory method like

class Candidate:
    @classmethod
    def from_match(cls, _log: Logger, source: ArtSource, ...) -> Self:
        # validate
        ...
        return cls(source=source, path=path)  # return a new Candidate

This drops the need for the ValidatedCandidate since now Candidate must be valid in order to be created.

The resize method from InexactCandidate can be a method on Candidate - which does nothing if the size already meets the expectations.

Copy link
Member Author

Choose a reason for hiding this comment

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

The point of the ExactCandidate/InexactCandidate split was to avoid assertions in their .resize() method (since only for InexactCandidate we always know the size). I think encoding that using the type system does require different types (or generics).

I'll see what I can achieve by refactoring Candidate.

Copy link
Member Author

Choose a reason for hiding this comment

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

You might be right that this can be simplified: We split the process of finding, downloading, validating and resizing art into separate function, but only ever run them one after each other anyway.

Copy link
Member

Choose a reason for hiding this comment

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

You might be right that this can be simplified: We split the process of finding, downloading, validating and resizing art into separate function, but only ever run them one after each other anyway.

In such case, maybe a couple of @classmethods could be a good candidate (pun intended)?

beetsplug/fetchart.py Show resolved Hide resolved
@Dr-Blank
Copy link
Contributor

I've just seen #5152, which I've completely missed up to now, and which is badly conflicting with what I'm doing here. However, while it's great that there's some momentum on the issue that #5152 tackles, I'm not convinced of the approach.

In contrast, what I'm doing here would fit nicely with eventually pushing the multiple-action image conversion completely to the artresizer.

The only issue with pushing multiple flags is that image operations are not mutually exclusive. Reformating affects size.

For e.g., the size of the PNG image changes drastically when converted to JPEG. So if my configuration is to always use JPEG images of max size 5 MB, the flags for a 15 MB PNG would be [REFORMAT, RESIZE] but after reformat if the size would drop to below 5 MB, the RESIZE would be unnecessary, hence a evaluation is needed after every step.

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.

3 participants