bpo-45679: add tuple tests with lru_cache to test_functools#29339
bpo-45679: add tuple tests with lru_cache to test_functools#29339rhettinger merged 4 commits intopython:mainfrom
tuple tests with lru_cache to test_functools#29339Conversation
cba8d02 to
9d8a4fd
Compare
|
I appreciate the effort to add tests but am dubious about these. On a cold reading, I find it difficult to tell what guaranteed invariants are being checked or even what the test is doing. If one of these tests ever broke, it would take a while to figure out what the complaint was about. Also, the assertions about the cache_info components seem to be over-specified. We make almost no promises about the cache hits and have more than once changed what counts as a hit. My recommendation is to just add a couple of lines to test_lru_with_types(): I don't think there is a need for a new test method that makes heroic efforts to test in combination that which has already been tested piece by piece. A single additional test will suffice to cover the notion, "that which works for scalars also works in containers". |
Lib/test/test_functools.py
Outdated
| self.assertEqual(cached.cache_info().misses, 2) | ||
|
|
||
| def test_lru_with_container_types_hash_collision(self): | ||
| # https://bugs.python.org/issue45701 |
There was a problem hiding this comment.
We would normally only reference a bug if it pertained to a bug in the lru_cache itself. In this case, the referenced bug was in application code. Linking to it from this from here doesn't add any explanatory value.
|
@rhettinger thanks a lot for the detailed feedback!
# Verify distinct results for equivalent tuples with differing types
cached_repr = self.module.lru_cache(maxsize=maxsize, typed=True)(repr)
self.assertEqual(cached_repr((True, 'string')), "(True, 'string')")
self.assertEqual(cached_repr((1, 'string')), "(1, 'string')")The thing is, it does not not work this way. And that's what we test here 🙂 >>> from functools import lru_cache
>>> r = lru_cache(typed=True)(repr)
>>> r((1, 2))
'(1, 2)'
>>> r((True, 2))
'(1, 2)'
>>> r((1.0, 2))
'(1, 2)'
I took the idea from the similar tests in this module. There're around 50 usages of
I am not sure I follow 🙂 All this being said, I can easily change this to be a simple one / two lines test. But, I'll add some more explanation context to tests' docstrings first. Maybe it will be easier to read / maintain. And possibly we can keep more complex checks. |
Done! I've simplified tests as much as possible. Including:
I hope it is good enough 🙂 |
Yes, now I see. The tested invariant is that typed applies to the container rather than its contents. Would it suffice to add a single test case, test_typed_is_not_recursive(): |
|
Thank you for the PR. |
|
Thanks a lot for the great simplification 👍 |
I've added several extra tests to make sure case in 45679 works as intended.
https://bugs.python.org/issue45701
Context: https://bugs.python.org/issue45679