python / python/typeshed

Cachetools timer with datetime.now

Open
#8,459 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

A followup to the issue presented in #6953, I'm running into mypy errors when implementing a cache using the mechanism as defined in the cachetools documentation.

Cachetools documentation for TRLUCache with example that matches the implementation below.

My implementation
def expires_in_ttu(_key, value, now):
    expires_in = value[1]
    return now + timedelta(seconds=expires_in)

cache = TLRUCache(maxsize=10, ttu=expires_in_ttu, timer=datetime.now)

The mypy error:

Argument "timer" to "TLRUCache" has incompatible type "Callable[[Optional[tzinfo]], datetime]"; expected "Callable[[], float]"

Workaround:

We can use time.monotonic instead of datetime.now, but:
- the example in the docs use datetime.now
- datetime.now is arguably more familiar to most developers

Versions
  • cachetools: 5.2.0
  • types-cachetools: 5.2.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by comparing the TLRUCache typing in types-cachetools with the documented datetime.now example and the behavior described in issue #6953. Reproduce the reported mypy error using the versions listed, then verify that the documented timer usage is accepted or that the documentation and typing agree.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.