Skip to content

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 🙌

@fbidu fbidu force-pushed the fbidu/qualname-key branch from efd0131 to 3718c14 Compare May 17, 2024 20:01
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



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.

2 participants