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

Unabled to defind metadata with Nested types #167

Open
boomjuice opened this issue Oct 27, 2021 · 1 comment
Open

Unabled to defind metadata with Nested types #167

boomjuice opened this issue Oct 27, 2021 · 1 comment

Comments

@boomjuice
Copy link

I defined a dataclass as follow.

@dataclass
class ImportData:
      data_list: t.List[t.List[str]] = field(metadata={'allow_none': True})

and how do i defind the attrs of str in nested types?
with older version, i defind the attrs like this.

class ImportDataSchema(Schema):
    data_list = fields.List(fields.List(fields.Str(allow_none=True)))
@boomjuice boomjuice changed the title Unabled to defiend metadata with Nested types Unabled to defind metadata with Nested types Oct 27, 2021
@dairiki
Copy link
Collaborator

dairiki commented Jan 13, 2023

For your specific example, I think this will work:

@dataclass
class ImportData:
      data_list: t.List[t.List[t.Optional[str]]]

Using the Optional inner type will automatically set allow_none on the field.
(You could equivalently write t.Union[str, None] — or, in python 3.10 or above, str | None — in place of t.Optional[str].)

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

2 participants