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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use qualname to avoid key collisions #257

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

fbidu
Copy link

@fbidu fbidu commented May 17, 2024

Hi there! This PR adds the possibility to use a function's full qualified name while building the cache key, an attribute that has been available since python 3.3 so it is safe to be relied upon here.

This PR comes from a bug hunting I did today whose root cause was key collision. We had a structure that looked roughly like this:

class A:
	
	@classmethod
	def active(cls):
		...

class B

	@classmethod
	def active(cls):
		...

When both active methods are cached, their keys end up colliding because:

In [3]: class B:
   ...:     @classmethod
   ...:     def active(cls):
   ...:         ...
   ...:

In [4]: B.active.__name__
Out[4]: 'active'

but:

In [5]: B.active.__qualname__
Out[5]: 'B.active'

Hope this is useful 馃檶

Copy link
Member

@zzzeek zzzeek left a comment

Choose a reason for hiding this comment

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

this is nice, I wonder if we should just switch to using __qualname__ across the board by default. we'd want to do a bigger version bump for that. but let's start with this

dogpile/cache/util.py Show resolved Hide resolved
dogpile/cache/util.py Outdated Show resolved Hide resolved
@@ -4,7 +4,7 @@
from ..util import langhelpers


def function_key_generator(namespace, fn, to_str=str):
def function_key_generator(namespace, fn, to_str=str, use_qual_name=False):
Copy link
Member

Choose a reason for hiding this comment

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

how about also in:

  • def function_multi_key_generator
  • def kwarg_function_key_generator

return nested


def test_function_key_generator_qualname():
Copy link
Member

Choose a reason for hiding this comment

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

I would break up each test case here and have them come in using pytest.mark.parametrize as a decorator on def test_function_key_generator_qualname. so one function, but the cases come in parameters. each parameter results in one test.

)


def test_function_key_generator():
Copy link
Member

Choose a reason for hiding this comment

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

same here, then also for the multi key and kwarg versions

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