Skip to content

make fields typed as lists optional #1858

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

Merged
merged 1 commit into from
Jul 9, 2024

Conversation

miguelgrinberg
Copy link
Collaborator

@miguelgrinberg miguelgrinberg commented Jul 4, 2024

I'm trying to type the examples, and another common issue I'm seeing is related to lists, either those created with multi=True or by using a Nested field.

The issue is with the state of the required option of the field. When using the non-typed field creation option this isn't a problem. Both Text(multi=True) and Nested(SomeDoc) work well with their default setting of required=False.

When using the new type hint definitions, however, the default for a typed field is to be required, and to make a field not required you have to wrap the type with Optional. This is okay for a scalar field, but for a list it creates confusion, because in terms of Python typing it is expected that an empty list is a valid value for a required field, but a required DSL field considers an empty list the same as None and does not allow it.

With this change, I'm changing the field creation logic to make sure that any fields that are created via type hints have required=False even when Optional is not given in the type hint, so that they work in the expected way and allow the empty list as a valid value.

No behavior changes are made for fields that are created without type hints.

@miguelgrinberg
Copy link
Collaborator Author

Closing. I have included this change in #1859

@miguelgrinberg miguelgrinberg deleted the optional-typed-lists branch July 5, 2024 14:22
@miguelgrinberg miguelgrinberg restored the optional-typed-lists branch July 9, 2024 12:59
@miguelgrinberg miguelgrinberg reopened this Jul 9, 2024
Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM.

@miguelgrinberg miguelgrinberg merged commit 619db4d into elastic:main Jul 9, 2024
32 checks passed
@miguelgrinberg miguelgrinberg deleted the optional-typed-lists branch July 9, 2024 13:29
github-actions bot pushed a commit that referenced this pull request Jul 9, 2024
miguelgrinberg added a commit that referenced this pull request Jul 9, 2024
(cherry picked from commit 619db4d)

Co-authored-by: Miguel Grinberg <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 8.x Backport to 8.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants