Skip to content

Commit 27627d8

Browse files
fixes for submission of projects
1 parent b90dd08 commit 27627d8

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

mp_api/client/contribs/client.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from math import isclose
1717
from pathlib import Path
1818
from tempfile import gettempdir
19-
from typing import TYPE_CHECKING, Literal, cast, overload
19+
from typing import TYPE_CHECKING, Literal, overload
2020
from urllib.parse import urlsplit
2121

2222
import orjson
@@ -69,7 +69,7 @@
6969

7070
if TYPE_CHECKING:
7171
from collections.abc import Generator, Iterable, Sequence
72-
from typing import Any
72+
from typing import Any, cast
7373

7474
from mp_api.client.contribs.types import (
7575
AllIdMap,
@@ -861,7 +861,7 @@ def create_project(
861861
"references": [{"label": "REF", "url": url}],
862862
}
863863
)
864-
resp = self.projects.createProject(project=project.model_dump()).result()
864+
resp = self.projects.createProject(project=project.to_draft()).result()
865865
owner = resp.get("owner")
866866
if owner:
867867
MPCC_LOGGER.info(f"Project `{name}` created with owner `{owner}`")
@@ -1432,12 +1432,13 @@ def _collect_ids_as_sets(
14321432
ret[project] = id_sets
14331433

14341434
project_sets = ret[project]
1435-
cast(set[str], project_sets["ids"]).add(contrib["id"])
1436-
cast(set[str], project_sets["identifiers"]).add(contrib["identifier"])
1435+
if TYPE_CHECKING:
1436+
cast(set[str], project_sets["ids"]).add(contrib["id"])
1437+
cast(set[str], project_sets["identifiers"]).add(contrib["identifier"])
14371438

14381439
if data_id_field:
14391440
data_value = contrib.get("data", {}).get(data_id_field)
1440-
if isinstance(data_value, str):
1441+
if TYPE_CHECKING and isinstance(data_value, str):
14411442
cast(set[str], project_sets[f"{data_id_field}_set"]).add(data_value)
14421443

14431444
for component in components:
@@ -1448,7 +1449,9 @@ def _collect_ids_as_sets(
14481449
if component not in project_sets:
14491450
project_sets[component] = {"ids": set(), "md5s": set()}
14501451

1451-
component_sets = cast(ComponentIdSets, project_sets[component])
1452+
if TYPE_CHECKING:
1453+
component_sets = cast(ComponentIdSets, project_sets[component])
1454+
14521455
for item in component_items:
14531456
if not isinstance(item, dict):
14541457
continue

mp_api/client/contribs/schemas.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,17 @@ def _get_pydantic_from_dataframe(
9898
"""
9999
columns_renamed = {}
100100
columns_to_unit = {}
101+
character_replacements = str.maketrans(
102+
{
103+
"^": "**",
104+
":": "",
105+
"#": "",
106+
}
107+
)
101108
for col in df.columns:
102109
base_name, unit = _get_unit(col)
103110
base_name = _to_camel_case(base_name)
104-
columns_renamed[col] = base_name
111+
columns_renamed[col] = base_name.translate(character_replacements)
105112
if unit:
106113
columns_to_unit[col] = unit
107114

@@ -179,7 +186,19 @@ def flatten_other(cls, d: dict) -> dict[str, str | None]:
179186
@field_serializer("other", mode="plain")
180187
def unflatten_other(self, v: dict[str, str]) -> dict[str, Any]:
181188
"""Unflatten column metadata."""
182-
return unflatten_dict(v)
189+
return unflatten_dict(v or {})
190+
191+
def to_draft(self) -> dict[str, Any]:
192+
"""Strip out fields that cannot be used in creating a project.
193+
194+
The API forbids including `is_public` and `is_approved` when
195+
submitting a project, even if these fields are False.
196+
"""
197+
return {
198+
k: v
199+
for k, v in self.model_dump().items()
200+
if k not in {"is_approved", "is_public"}
201+
}
183202

184203

185204
class ContribMeta(_DictLikeAccess):
@@ -331,6 +350,14 @@ def from_dataframe(
331350
for idx, entry in enumerate(sanitized)
332351
]
333352

353+
def to_submission(self) -> dict[str, Any]:
354+
"""Pop null keys."""
355+
return {
356+
k: v
357+
for k, v in self.model_dump(mode="json").items()
358+
if v is not None and k != "id"
359+
}
360+
334361

335362
class QueryResult(_DictLikeAccess):
336363
"""Result of query_contributions."""

0 commit comments

Comments
 (0)