Skip to content

Commit

Permalink
Use default_factory for list Pydantic attributes (#343)
Browse files Browse the repository at this point in the history
### Description
A cleanup item that came up today. Context:
#250 (comment)


### Checklist

- [ ] I have read [the contributing
guide](https://github.com/dbt-labs/dbt-semantic-interfaces/blob/main/CONTRIBUTING.md)
and understand what's expected of me
- [ ] I have signed the
[CLA](https://docs.getdbt.com/docs/contributor-license-agreements)
- [ ] This PR includes tests, or tests are not required/relevant for
this PR
- [ ] I have run `changie new` to [create a changelog
entry](https://github.com/dbt-labs/dbt-semantic-interfaces/blob/main/CONTRIBUTING.md#adding-a-changelog-entry)
  • Loading branch information
courtneyholcomb authored Sep 4, 2024
1 parent 85ad757 commit 47f831e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 14 deletions.
3 changes: 2 additions & 1 deletion dbt_semantic_interfaces/implementations/elements/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dbt_semantic_interfaces.implementations.metadata import PydanticMetadata
from dbt_semantic_interfaces.references import MeasureReference
from dbt_semantic_interfaces.type_enums import AggregationType
from dsi_pydantic_shim import Field


class PydanticNonAdditiveDimensionParameters(HashableBaseModel):
Expand All @@ -21,7 +22,7 @@ class PydanticNonAdditiveDimensionParameters(HashableBaseModel):

# Optional Fields
window_choice: AggregationType = AggregationType.MIN
window_groupings: List[str] = []
window_groupings: List[str] = Field(default_factory=list)


class PydanticMeasureAggregationParameters(HashableBaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)
from dbt_semantic_interfaces.protocols import ProtocolHint
from dbt_semantic_interfaces.protocols.project_configuration import ProjectConfiguration
from dsi_pydantic_shim import validator
from dsi_pydantic_shim import Field, validator


class PydanticProjectConfiguration(HashableBaseModel, ModelWithMetadataParsing, ProtocolHint[ProjectConfiguration]):
Expand All @@ -30,10 +30,10 @@ class PydanticProjectConfiguration(HashableBaseModel, ModelWithMetadataParsing,
def _implements_protocol(self) -> ProjectConfiguration:
return self

time_spine_table_configurations: List[PydanticTimeSpineTableConfiguration] = []
time_spine_table_configurations: List[PydanticTimeSpineTableConfiguration] = Field(default_factory=list)
metadata: Optional[PydanticMetadata] = None
dsi_package_version: PydanticSemanticVersion = UNKNOWN_VERSION_SENTINEL
time_spines: List[PydanticTimeSpine] = []
time_spines: List[PydanticTimeSpine] = Field(default_factory=list)

@validator("dsi_package_version", always=True)
@classmethod
Expand Down
7 changes: 4 additions & 3 deletions dbt_semantic_interfaces/implementations/saved_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
SavedQuery,
SavedQueryQueryParams,
)
from dsi_pydantic_shim import Field


class PydanticSavedQueryQueryParams(HashableBaseModel, ProtocolHint[SavedQueryQueryParams]):
Expand All @@ -28,8 +29,8 @@ def _implements_protocol(self) -> SavedQueryQueryParams:
return self

metrics: List[str]
group_by: List[str] = []
order_by: List[str] = []
group_by: List[str] = Field(default_factory=list)
order_by: List[str] = Field(default_factory=list)
limit: Optional[int] = None
where: Optional[PydanticWhereFilterIntersection] = None

Expand All @@ -46,4 +47,4 @@ def _implements_protocol(self) -> SavedQuery:
description: Optional[str] = None
metadata: Optional[PydanticMetadata] = None
label: Optional[str] = None
exports: List[PydanticExport] = []
exports: List[PydanticExport] = Field(default_factory=list)
3 changes: 2 additions & 1 deletion dbt_semantic_interfaces/implementations/semantic_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dbt_semantic_interfaces.implementations.saved_query import PydanticSavedQuery
from dbt_semantic_interfaces.implementations.semantic_model import PydanticSemanticModel
from dbt_semantic_interfaces.protocols import ProtocolHint, SemanticManifest
from dsi_pydantic_shim import Field


class PydanticSemanticManifest(HashableBaseModel, ProtocolHint[SemanticManifest]):
Expand All @@ -22,4 +23,4 @@ def _implements_protocol(self) -> SemanticManifest:
semantic_models: List[PydanticSemanticModel]
metrics: List[PydanticMetric]
project_configuration: PydanticProjectConfiguration
saved_queries: List[PydanticSavedQuery] = []
saved_queries: List[PydanticSavedQuery] = Field(default_factory=list)
6 changes: 3 additions & 3 deletions dbt_semantic_interfaces/implementations/semantic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def _implements_protocol(self) -> SemanticModel:
node_relation: PydanticNodeRelation

primary_entity: Optional[str]
entities: Sequence[PydanticEntity] = []
measures: Sequence[PydanticMeasure] = []
dimensions: Sequence[PydanticDimension] = []
entities: Sequence[PydanticEntity] = Field(default_factory=list)
measures: Sequence[PydanticMeasure] = Field(default_factory=list)
dimensions: Sequence[PydanticDimension] = Field(default_factory=list)
label: Optional[str] = None

metadata: Optional[PydanticMetadata]
Expand Down
3 changes: 2 additions & 1 deletion dbt_semantic_interfaces/implementations/time_spine.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
TimeSpinePrimaryColumn,
)
from dbt_semantic_interfaces.type_enums import TimeGranularity
from dsi_pydantic_shim import Field


class PydanticTimeSpinePrimaryColumn(HashableBaseModel, ProtocolHint[TimeSpinePrimaryColumn]): # noqa: D101
Expand Down Expand Up @@ -42,4 +43,4 @@ def _implements_protocol(self) -> TimeSpine:

node_relation: PydanticNodeRelation
primary_column: PydanticTimeSpinePrimaryColumn
custom_granularities: Sequence[PydanticTimeSpineCustomGranularityColumn] = []
custom_granularities: Sequence[PydanticTimeSpineCustomGranularityColumn] = Field(default_factory=list)
7 changes: 5 additions & 2 deletions tests/test_implements_satisfy_protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
from dbt_semantic_interfaces.implementations.project_configuration import (
PydanticProjectConfiguration,
)
from dbt_semantic_interfaces.implementations.saved_query import PydanticSavedQuery
from dbt_semantic_interfaces.implementations.saved_query import (
PydanticSavedQuery,
PydanticSavedQueryQueryParams,
)
from dbt_semantic_interfaces.implementations.semantic_manifest import (
PydanticSemanticManifest,
)
Expand Down Expand Up @@ -123,7 +126,7 @@

SAVED_QUERY_STRATEGY = builds(
PydanticSavedQuery,
group_by=from_type(List[str]),
query_params=builds(PydanticSavedQueryQueryParams),
where=from_type(List[PydanticWhereFilter]),
description=OPTIONAL_STR_STRATEGY,
metadata=OPTIONAL_METADATA_STRATEGY,
Expand Down

0 comments on commit 47f831e

Please sign in to comment.