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

Add support for typing.TypeAliasType #579

Closed
wikiped opened this issue Nov 6, 2023 · 1 comment · Fixed by #606
Closed

Add support for typing.TypeAliasType #579

wikiped opened this issue Nov 6, 2023 · 1 comment · Fixed by #606

Comments

@wikiped
Copy link

wikiped commented Nov 6, 2023

Description

Currently (msgspec version 0.18.4, python 3.12) the following:

import msgspec as ms
from typing import Annotated

NonEmptyStringAlias = Annotated[str, ms.Meta(min_length=1)]
type NonEmptyStringTypeAlias = Annotated[str, ms.Meta(min_length=1)]


class A(ms.Struct):
    name: NonEmptyStringAlias
    
    
class T(ms.Struct):
    name: NonEmptyStringTypeAlias
    
    
print(ms.json.decode(ms.json.encode(A('test')), type=A))

print(ms.json.decode(ms.json.encode(T('test')), type=T))

Will fail on the last line:

A(name='test')
...
TypeError: Type 'NonEmptyStringTypeAlias' is not supported

Considering that:

print(type(NonEmptyStringTypeAlias))
# typing.TypeAliasType

print(type(NonEmptyStringAlias))
# typing._AnnotatedAlias

NonEmptyStringTypeAlias.__value__ is NonEmptyStringAlias
# True

Would it make sense to add support for the typing.TypeAliasType which is created with type statements (since python 3.12)?

@jcrist
Copy link
Owner

jcrist commented Nov 7, 2023

Sure, this is definitely in scope. Adding support for TypeAliasType instances should be fairly straightforward, with the exception of recursive alias definitions (these would require a major refactor of msgspec's internals).

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 a pull request may close this issue.

2 participants