(RUF012) Fixed mutable class Defaults- Task 1 #10283
(RUF012) Fixed mutable class Defaults- Task 1 #10283cdrini merged 24 commits intointernetarchive:masterfrom
Conversation
for more information, see https://pre-commit.ci
|
I closed the previous draft pull request because I ran into some errors on that branch and couldn't continue working on it. I created a new branch and am still working on it. |
|
Hello @RayBB, What happened: Clarification: |
|
@ananyakaligal I need to catchup on many other of the issues before digging into this one. For now lets leave it as a draft and I'll follow up with you when I can evaluate 👍 |
|
@RayBB Thankyou, please do tag me after you evaluate it because the rule hasn't been enabled yet, and I can do it later. |
|
I think that openlibrary/plugins/worksearch/schemes/editions.py will need to be changed as part of this PR. |
|
Copying this from the other PR: In general the approach we'll want is:
|
|
@RayBB Could you review this and let me know if any changes are required. |
RayBB
left a comment
There was a problem hiding this comment.
General idea looks good to me. Left a few notes to on comments to restore (restore all please).
for more information, see https://pre-commit.ci
cdrini
left a comment
There was a problem hiding this comment.
Awesome, nice work @ananyakaligal ! The approach here seems good ; a few pieces of feedback for keeping the type information and some small bug fixes 👍
| facet_rewrites: dict[tuple[str, str], str | Callable[[], str]] | ||
| def __init__(self): | ||
| # Set of queries that define the universe of this scheme | ||
| self.universe = set() |
There was a problem hiding this comment.
Let's bring back the type information here, otherwise it doesn't know what the set contains.
| self.universe = set() | |
| self.universe: set[str] = set() |
And so-on.
Also, let's change universe back to a list ; we shouldn't need it to be a set.
| facet_rewrites: dict[tuple[str, str], str | Callable[[], str]] = {} | ||
| def __init__(self): | ||
| super().__init__() | ||
| self._universe = {'type:author'} |
There was a problem hiding this comment.
These shouldn't have underscore prefixes, or they won't override the baseclass values!
| self._universe = {'type:author'} | |
| self.universe = {'type:author'} |
| ('has_fulltext', 'true'): f'ebook_access:[{get_fulltext_min()} TO *]', | ||
| ('has_fulltext', 'false'): f'ebook_access:[* TO {get_fulltext_min()}]', |
There was a problem hiding this comment.
These will need their lambdas back, because the value needs to be computed on a per-user basis.
…ambda has_fulltext
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
RayBB
left a comment
There was a problem hiding this comment.
I've just gone through and made a couple tweaks but overall I think this is ready and it's a big improvement.
First, I made facet_fields and default_fetched_fields class variables because they must be based on how they're used. And I made them frozen sets so they can't be modified.
Second, I enabled the ruff rule since this is the last PR. From that I found small things to fix with component_mapping, types, and an array that should be a set.
Although this series of PRs for this rule have been a lot of work I certainly have learned a lot and I do think it's an improvement for the code base.
Hoping that @cdrini and I can review and merge this one Wednesday!
|
@RayBB and I have been flipping back and forth between the two options here:
I think long term we're leaning towards the second option, which better matches the intended meaning of these fields, which is that they are immutable, and biting the cost of the awkward python syntax required. BUT since this solution comes with a number of risks that we fear might make this take a long time to resolve/come to a conclusion, for this PR, we have decided to disable the ruff rule for these specific files, and revert them to their original state, so that we can unblock this PR and merge it in. Thank you so much for your patience @ananyakaligal ! This one turned out to be significantly more subtle/intricate than any one of us anticipated :P And thank you so much @RayBB for diligently keeping things moving and making updates and progress as we discovered more about this problem space! @RayBB is also kindly taking on the work of reverting the SearchScheme files to make this ready-to-merge! |
cdrini
left a comment
There was a problem hiding this comment.
This looks perfect!!! Thank you so much @RayBB and @ananyakaligal !
Part of #10196
NOTE: These are NOT automated changes, they are done by hand
Prevents accidental modifications of mutable class attributes at the instance level.
Aligns with project style guidelines enforced by Ruff.
Technical
Updated mutable class attributes in 3 files to use typing.ClassVar for clarity and correctness.
Added ClassVar annotations to mutable class attributes to ensure they are treated as class-level attributes.
Ensured proper type hints for clarity and type safety.
Testing
Ran ruff check --select RUF012 to confirm no further warnings.
Stakeholders
@RayBB