Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How to make multivalued field default to empty array rather than None #2957

@manulera

Description

@manulera

Currently, a field like this:

      - input:
          range: SourceInput
          slot_uri: schema:object
          description: >-
            The inputs to this source.
            If the source represents external import of a sequence, it's empty.
          required: false
          multivalued: true
          inlined_as_list: true

Will get transformed into a pydantic model like below, where the default value is None.

input: Optional[list[Union[SourceInput, AssemblyFragment]]] = Field(
        default=None,
        description="""The inputs to this source. If the source represents external import of a sequence, it's empty.""",
        json_schema_extra={"linkml_meta": {"alias": "input", "domain_of": ["Source"], "slot_uri": "schema:object"}},
    )

Instead, you may want the default value to be an empty array (e.g. if this represents a relationship field), so instead of default=None, in pydantic you would want something like default_factory=list.

Is there a supported way for this already? (I don't think it's possible using ifabsent).

This seems like a fairly common use-case. If there is a ready solution, I am happy to contribute the information to the documentation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions