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

Already on GitHub? Sign in to your account

Hot to get field alias name from Struct? #672

Closed
Olegt0rr opened this issue Apr 22, 2024 · 3 comments
Closed

Hot to get field alias name from Struct? #672

Olegt0rr opened this issue Apr 22, 2024 · 3 comments

Comments

@Olegt0rr
Copy link
Contributor

Olegt0rr commented Apr 22, 2024

Question

  1. Create struct
from msgspec import Struct, field

class HelpIssue(Struct, kw_only=True):
    user_id: int | None = field(default=None, name="userId")
  1. Then I need to get name from field user_id
struct_field_name = "user_id"
struct_field = getattr(HelpIssue, struct_field_name)

print(struct_field.name) 
# Expected: "userId"
# Received: AttributeError: 'member_descriptor' object has no attribute 'name'
@uwinx
Copy link

uwinx commented Apr 22, 2024

@Olegt0rr, I think inspect.type_info will do the right job for you.

from msgspec.inspect import type_info

# to access "user_id" field's metadata
field_meta = type_info(HelpIssue).fields[0]
assert field_meta.name == "user_id"
assert field_meta.encode_name == "userId"

References: https://jcristharif.com/msgspec/inspect.html

@Olegt0rr
Copy link
Contributor Author

@uwinx, thanks!

I've also found this way:

for name, encode_name in zip(
    struct.__struct_fields__,
    struct.__struct_encode_fields__, 
):
    ...

@jcrist
Copy link
Owner

jcrist commented Apr 23, 2024

msgspec.inspect.type_info is one way, but it's a bit overkill for this use case. And __struct_encode_fields__ is technically non-public (and may be removed in the future). The best way to get this information is to use msgspec.struct.fields (docs). You're looking for the encode_name attribute on FieldInfo in the result:

In [3]: class Example(msgspec.Struct):
   ...:     user_id: int = msgspec.field(name="userId")
   ...: 

In [4]: msgspec.structs.fields(Example)
Out[4]: (FieldInfo(name='user_id', encode_name='userId', type=<class 'int'>, default=NODEFAULT, default_factory=NODEFAULT),)

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

No branches or pull requests

3 participants