Skip to content

Add datetime types to Cachetools' TTLCache and TLRUCache classes #14585

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

saroad2
Copy link

@saroad2 saroad2 commented Aug 17, 2025

At the moment, the type hints for these classes only include float types. However, if you look at the documentation for both TTLCache and TLRUCache, one can see that you can use datetime and timedelta as your timer and ttl respectively.

Because of the lack of these types in the type annotations, tools like mypy fail when trying to use datetime and timedelta, which prevents people from using this capability when enforcing type safety.

This PR fixes this problem by overriding the relevant functions typing and supporting these classes.

@saroad2
Copy link
Author

saroad2 commented Aug 17, 2025

Fixes #8459

Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
Copy link
Collaborator

srittau commented Aug 18, 2025

I think a better approach would be to make TTLCache, TLRUCache, and _TimedCache generic over the time type. This would not only make the code more readable, it would also prevent the union return types.

@saroad2
Copy link
Author

saroad2 commented Aug 18, 2025

Hi @srittau ! Thank you for your review.
I think I might have a better idea, but it requires some changes to cachetools itself.
I'll open an issue in cachetools to see if my idea makes sense to the maintainers, and then I'll implement it there.

@saroad2 saroad2 marked this pull request as draft August 18, 2025 15:52
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