Skip to content

Add option to pythongen to use less inclusive types #464

@cmungall

Description

@cmungall

In the personinfo example (https://github.com/linkml/linkml/blob/main/examples/PersonSchema/personinfo/personinfo.py) we end up with

@dataclass
class Person(NamedThing):
    ...
    has_employment_history: Optional[Union[Union[dict, "EmploymentEvent"], List[Union[dict, "EmploymentEvent"]]]] = empty_list()

This is nice as it allows us to be very loose when instantiating objects. However, it is hard to read. And it also doesn't inform either us or our IDE of what the 'normalized' form of the object is

E,g,

if I do

p = Person('x', has_employment_history={'is_current': 1})

it will normalize this from a single dict to a list of objects

>>> type(p.has_employment_history)
<class 'list'>

I am not sure if we need this level of looseness in creation - it is handy to convert from a dict to an object but auto-converting objects to lists seems to encourage bad use. TBD: check if this is required for jsonld strategy

Should we:

  • add an option to have stricter constructors in pythongen
  • leave as is, and focus on pydanticgen (@kevinschaper ) as the preferred method for making an OM?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestgenerator-dataclassesPython classes were generated by dataclass (we are mostly switching to Pydantic, though)generator-python

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions