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

Make BaseCache an ABC #683

Merged
merged 13 commits into from
Mar 5, 2023

Conversation

padraic-shafer
Copy link
Contributor

What do these changes do?

BaseCache is now an abstract base class. Attempting to instantiate a BaseCache object raises a TypeError.

Are there changes in behavior for the user?

Generally not, because users have always been expected to create cache objects using a subclass of BaseCache.

Now a TypeError is raised at the point of attempted cache creation, if the user attempts to instantiate a BaseCache object. Previously a NotImplementedError was raised at the point where an abstract method was called.

Related issue number

#676

Checklist

  • [ X] I think the code is well written
  • [ X] Unit tests for the changes exist
  • [N/A ] Documentation reflects the changes
  • [ X] Add a new news fragment into the CHANGES folder
    • name it <issue_id>.<type> (e.g. 588.bugfix)
    • if you don't have an issue_id change it to the pr id after creating the PR
    • ensure type is one of the following:
      • .feature: Signifying a new feature.
      • .bugfix: Signifying a bug fix.
      • .doc: Signifying a documentation improvement.
      • .removal: Signifying a deprecation or removal of public API.
      • .misc: A ticket has been closed, but it is not of interest to users.
    • Make sure to use full sentences with correct case and punctuation, for example: Fix issue with non-ascii contents in doctest text files.

@codecov
Copy link

codecov bot commented Mar 4, 2023

Codecov Report

Merging #683 (db33461) into master (2f9d314) will decrease coverage by 0.05%.
The diff coverage is 97.43%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #683      +/-   ##
==========================================
- Coverage   99.67%   99.63%   -0.05%     
==========================================
  Files          35       35              
  Lines        3748     3793      +45     
==========================================
+ Hits         3736     3779      +43     
- Misses         12       14       +2     
Flag Coverage Δ
unit 99.63% <97.43%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
tests/utils.py 95.74% <94.87%> (-4.26%) ⬇️
aiocache/backends/memcached.py 100.00% <100.00%> (ø)
aiocache/backends/memory.py 100.00% <100.00%> (ø)
aiocache/backends/redis.py 99.14% <100.00%> (ø)
aiocache/base.py 99.27% <100.00%> (+0.03%) ⬆️
tests/ut/conftest.py 100.00% <100.00%> (ø)
tests/ut/test_base.py 100.00% <100.00%> (ø)
tests/ut/test_decorators.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2f9d314...db33461. Read the comment docs.

aiocache/base.py Outdated Show resolved Hide resolved
aiocache/base.py Outdated Show resolved Hide resolved
CHANGES.rst Outdated Show resolved Hide resolved


class AbstractBaseCache(BaseCache[str]):
"""BaseCache that can be mocked for NotImplementedError tests"""
Copy link
Member

@Dreamsorcerer Dreamsorcerer Mar 5, 2023

Choose a reason for hiding this comment

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

I feel like these 2 classes are rather awkward, but I'll merge it for now.

I don't think there's any value in testing the logic of abstract methods, and I'm wondering if the other tests would still be testing the same thing if we just use the memory cache instead.. If so, we could then drop both of these classes.

Copy link
Member

@Dreamsorcerer Dreamsorcerer Mar 5, 2023

Choose a reason for hiding this comment

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

Also introduces a new mypy error:
https://github.com/aio-libs/aiocache/actions/runs/4333749520/jobs/7567135931

Curiously, only on the first method, though it looks like it should happen on all of them. Maybe because it doesn't have an await... Because it's the only annotated method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also introduces a new mypy error: https://github.com/aio-libs/aiocache/actions/runs/4333749520/jobs/7567135931

Because this class exists purely for tests, to allow mocks without throwing ABC TypeErrors, I think adding # type: ignore[safe-super] would be appropriate here.

Copy link
Member

Choose a reason for hiding this comment

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

I realise that, it's just a side note to the first comment about whether we can just remove them if they are not testing anything useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like these 2 classes are rather awkward, but I'll merge it for now.

Yeah, it's a bit awkward because the tests aren't being performed directly on BaseCache, and because of how many abstract methods need to be defined on the derived class. However the definitions of AbstractBaseCache and ConcreteBaseCache are trivial and transparent, so at least no new complexity is added.

I don't think there's any value in testing the logic of abstract methods, and I'm wondering if the other tests would still be testing the same thing if we just use the memory cache instead.. If so, we could then drop both of these classes.

On the contrary, I really like how the logic of what's defined in BaseCache gets tested independently with the (abstract) base class.

The derived classes used for the backend implementations use more resources and add enough "side effects" that proper testing of the underlying logic would require repeating these tests with all 3 backends. And even then, it would perhaps be an open question of whether the BaseCache functionality extends to new backends.

Keeping these tests of BaseCache separate from tests of the backends seems much cleaner and more robust. Previously this was a bit simpler because BaseCache was not formally abstract--it had unimplemented methods intended to be override, but instantiating BaseCache did not raise an exception.

Well, in any case, I'm open to finding a cleaner test solution, but I favor keeping the BaseCache tests separate from the backend tests of the same methods.

@Dreamsorcerer Dreamsorcerer merged commit d3fb375 into aio-libs:master Mar 5, 2023
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.

None yet

2 participants