Skip to content

Conversation

@dprbook
Copy link

@dprbook dprbook commented Jun 18, 2025

I had a use case, where I needed the fully qualified name of the class in the get_tag hook.
This change calls the hook function with the class as second, optional param, making this change non-breaking.

Alternatively, the second param could be the module path, but this is more flexible.

Usage

from msgspec import Struct

def get_tag(qualname, cls):
  # This still uses the qualname extraction in simple_qualname, see:
  # https://github.com/jcrist/msgspec/blob/bb5bc8dc75509ec6a2e6e0f0b3359028e584c651/msgspec/_core.c#L6060
  return f"{cls.__module__}.{qualname}"

# or
def get_tag(_, cls):
  # Native Python behavior
  return f"{cls.__module__}.{cls.__qualname__}"
  
# still works
def get_tag(qualname):
  return qualname

class S(Struct, tag=get_tag):
  a: int = 1

Any feedback welcome. Nice lib :)

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.

1 participant