dataclass defaults are accidentally shared
Opened this issue · 0 comments
cmungall commented
defaults with dataclasses are pretty unintuitive IMO. It's a good idea to always instantiate using default_factory, otherwise you end up with things like:
>>> from basin3d.core.models import ObservedProperty, ObservedPropertyVariable
>>> op = ObservedProperty()
>>> op.observed_property_variable.basin3d_id='x'
>>> op
ObservedProperty(datasource_variable='', observed_property_variable=ObservedPropertyVariable(basin3d_id='x', full_name='', categories=[], units=''), sampling_medium='', datasource=DataSource(id='', name='', id_prefix='', location='', credentials={}), datasource_description='')
>>> op2 = ObservedProperty()
>>> op2
ObservedProperty(datasource_variable='', observed_property_variable=ObservedPropertyVariable(basin3d_id='x', full_name='', categories=[], units=''), sampling_medium='', datasource=DataSource(id='', name='', id_prefix='', location='', credentials={}), datasource_description='')
>>> op2.observed_property_variable.basin3d_id = 'y'
>>> op
ObservedProperty(datasource_variable='', observed_property_variable=ObservedPropertyVariable(basin3d_id='y', full_name='', categories=[], units=''), sampling_medium='', datasource=DataSource(id='', name='', id_prefix='', location='', credentials={}), datasource_description='')
better yet, use dataclasses or pydantic generated from linkml :-)