Skip to content

Commit ea83e73

Browse files
authored
Migrate data models to pydantic (#267)
This PR introduces `pydantic` to handle all validation, parsing and serialising.
1 parent 222a5af commit ea83e73

24 files changed

+868
-947
lines changed

datacommons_client/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from datacommons_client.endpoints.base import API
44
from datacommons_client.endpoints.node import NodeEndpoint
55
from datacommons_client.endpoints.observation import ObservationEndpoint
6-
from datacommons_client.endpoints.payloads import ObservationDate
76
from datacommons_client.endpoints.resolve import ResolveEndpoint
7+
from datacommons_client.models.observation import ObservationDate
88
from datacommons_client.utils.dataframes import add_entity_names_to_observations_dataframe
99
from datacommons_client.utils.decorators import requires_pandas
1010
from datacommons_client.utils.error_handling import NoDataForPropertyError
@@ -184,7 +184,7 @@ def observations_dataframe(
184184
filter_facet_ids=facets)
185185

186186
# Convert the observations to a DataFrame
187-
df = pd.DataFrame(observations.to_observation_records())
187+
df = pd.DataFrame(observations.to_observation_records().model_dump())
188188

189189
# Add entity names to the DataFrame
190190
df = add_entity_names_to_observations_dataframe(

datacommons_client/endpoints/node.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from datacommons_client.endpoints.base import API
88
from datacommons_client.endpoints.base import Endpoint
99
from datacommons_client.endpoints.payloads import NodeRequestPayload
10-
from datacommons_client.endpoints.payloads import normalize_properties_to_string
10+
from datacommons_client.endpoints.payloads import normalize_list_to_string
1111
from datacommons_client.endpoints.response import NodeResponse
1212
from datacommons_client.models.node import Name
1313
from datacommons_client.models.node import Node
@@ -115,10 +115,10 @@ def fetch(
115115

116116
# Create the payload
117117
payload = NodeRequestPayload(node_dcids=node_dcids,
118-
expression=expression).to_dict
118+
expression=expression).to_dict()
119119

120120
# Make the request and return the response.
121-
return NodeResponse.from_json(
121+
return NodeResponse.model_validate(
122122
self.post(payload, all_pages=all_pages, next_token=next_token))
123123

124124
def fetch_property_labels(
@@ -199,7 +199,7 @@ def fetch_property_values(
199199
"""
200200

201201
# Normalize the input to a string (if it's a list), otherwise use the string as is.
202-
properties = normalize_properties_to_string(properties)
202+
properties = normalize_list_to_string(properties)
203203

204204
# Construct the expression based on the direction and constraints.
205205
direction = "->" if out else "<-"

datacommons_client/endpoints/observation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
from datacommons_client.endpoints.base import API
44
from datacommons_client.endpoints.base import Endpoint
5-
from datacommons_client.endpoints.payloads import ObservationDate
65
from datacommons_client.endpoints.payloads import ObservationRequestPayload
7-
from datacommons_client.endpoints.payloads import ObservationSelect
86
from datacommons_client.endpoints.response import ObservationResponse
7+
from datacommons_client.models.observation import ObservationDate
8+
from datacommons_client.models.observation import ObservationSelect
99
from datacommons_client.utils.data_processing import group_variables_by_entity
1010

1111

@@ -58,10 +58,12 @@ def fetch(
5858
entity_expression=entity_expression,
5959
filter_facet_domains=filter_facet_domains,
6060
filter_facet_ids=filter_facet_ids,
61-
).to_dict
61+
).to_dict()
62+
63+
response = self.post(payload)
6264

6365
# Send the request
64-
return ObservationResponse.from_json(self.post(payload))
66+
return ObservationResponse.model_validate(response)
6567

6668
def fetch_observations_by_entity_type(
6769
self,

0 commit comments

Comments
 (0)