Skip to content

Expose class to get_tag hook #857

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Expose class to get_tag hook #857

wants to merge 2 commits into from

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