Skip to content

Add new msgspec.structs module - #282

Closed
jcrist wants to merge 1 commit into
mainfrom
structs-info
Closed

jcrist wants to merge 1 commit into
mainfrom
structs-info

Conversation

@jcrist

@jcrist jcrist commented Jan 28, 2023

Copy link
Copy Markdown
Member

This adds a new msgspec.structs module. The purpose of this module is to include struct-related utilities that aren't worth surfacing to the top-level namespace, but don't really belong anywhere else either.

So far this just includes a new msgspec.structs.info function (and return types) for inspecting information about a struct type. This is a shallow-version of the full-featured msgspec.inspect.type_info function:

  • It only works for struct types
  • It doesn't recurse into the field types

Most use cases where you're inspecting msgspec types you probably want to use msgspec.inspect.type_info instead.

(Maybe) fixes #275.

This adds a new `msgspec.structs` module. The purpose of this module is
to include struct-related utilities that aren't worth surfacing to the
top-level namespace, but don't really belong anywhere else either.

So far this just includes a new `msgspec.structs.info` function (and
return types) for inspecting information about a struct type. This is a
shallow-version of the full-featured `msgspec.inspect.type_info`
function:

- It only works for struct types
- It doesn't recurse into the field types

Most use cases where you're inspecting msgspec types you probably want
to use `msgspec.inspect.type_info` instead.
@Goldziher

Copy link
Copy Markdown

Hi, so Im testing this now. I think I can simply use the work-around I use, which is to access the __annotations__ field on the class and use the values from there. So thanks a lot for your work! But I don't think this one is really crucial. Up to you to decide of course.

@jcrist

jcrist commented Feb 19, 2023

Copy link
Copy Markdown
Member Author

With #328 exposing all struct class-level configuration, I'm going to close and rework this to something like:

def fields(struct_instance_or_class) -> tuple[FieldInfo]:
    ...

class FieldInfo:
    name: str
    encode_name: str
    type: Any
    default: Any = None

@jcrist jcrist closed this Feb 19, 2023
@jcrist
jcrist deleted the structs-info branch February 20, 2023 02:31
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.

expose annotation on field value

2 participants