-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
base: master
Are you sure you want to change the base?
Add fetchart typing #5213
Conversation
ea2debf
to
2562146
Compare
Add typings to fetchart. there are a few rough edges requiring `cast` and enums could be better represented, but this is a start.
2562146
to
7f45347
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.
Mostly looks good. Added a couple of small comments, mentioned the risks of using naked assert
s, and mentioned three issues in lines 1476-1490 that non-strict mypy is not flagging for some reason.
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 |
bd0294e
to
e04b058
Compare
e04b058
to
1544a0f
Compare
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. |
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.
Types look great! Added a few minor comments and included a consideration regarding the Candidate
class design
|
||
MATCH_EXACT = 0 | ||
MATCH_FALLBACK = 1 | ||
_log: Logger |
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.
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
- 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
Candidate
is the main guy here so I feel like it should be made a bit more relevant by keeping all related logic.- Judging by the class names, the difference between
ExactCandidate
andInexactCandidate
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.
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.
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
.
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 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.
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 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 @classmethod
s could be a good candidate (pun intended)?
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 |
Add typings to fetchart. there are a few rough edges requiring
cast
and enums could be better represented, but this is a start.