Skip to content

bring in code from maggma.api to emmet.api #1267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Aug 18, 2025

Conversation

kbuma
Copy link
Contributor

@kbuma kbuma commented Aug 12, 2025

The code in maggma.api is only used by emmet. Bringing that code in here so that it does not need to be maintained as part of maggma.

Contributor Checklist

  • I have run the tests locally and they passed.
  • I have added tests, or extended existing tests, to cover any new features or bugs fixed in this PR

@codecov-commenter
Copy link

codecov-commenter commented Aug 12, 2025

Codecov Report

❌ Patch coverage is 87.85425% with 120 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.81%. Comparing base (199cd05) to head (1bbea7f).
⚠️ Report is 26 commits behind head on main.

Files with missing lines Patch % Lines
emmet-api/emmet/api/resource/submission.py 80.26% 30 Missing ⚠️
emmet-api/emmet/api/resource/s3_url.py 54.90% 23 Missing ⚠️
emmet-api/emmet/api/resource/read_resource.py 83.76% 19 Missing ⚠️
emmet-api/emmet/api/models.py 77.27% 10 Missing ⚠️
emmet-api/emmet/api/API.py 82.22% 8 Missing ⚠️
emmet-api/emmet/api/resource/core.py 84.44% 7 Missing ⚠️
emmet-api/emmet/api/resource/post_resource.py 89.65% 6 Missing ⚠️
emmet-api/emmet/api/resource/aggregation.py 90.00% 5 Missing ⚠️
emmet-api/emmet/api/utils.py 95.29% 4 Missing ⚠️
emmet-api/emmet/api/resource/utils.py 94.00% 3 Missing ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1267      +/-   ##
==========================================
- Coverage   88.92%   88.81%   -0.11%     
==========================================
  Files         161      179      +18     
  Lines       16545    17461     +916     
==========================================
+ Hits        14712    15508     +796     
- Misses       1833     1953     +120     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@tschaume tschaume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kbuma! I removed the maggma[api] dependency in setup.py, reran the dependency upgrades, and merged it into the branch for this PR. That way the tests in this PR run with the updated dependencies. Looks like there are some maggma imports left, and some dependencies from maggma missing (e.g. uvicorn). Could you update the setup.py accordingly? I can regenerate the dependency files for testing afterwards. Thanks!

@esoteric-ephemera
Copy link
Collaborator

@tschaume should be ready for final review

@kbuma kbuma requested a review from tschaume August 15, 2025 17:02
Copy link
Member

@tschaume tschaume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Left some minor comments for you to consider.

Just to confirm, this PR shouldn't break the API, right? Does it depend on a new emmet-core release? Ping me when you had the chance to look through my suggestions, and I'll try to bring up the API in the docker-compose stack to make sure it still works with web. Thanks!


@field_validator("errors", mode="before")
@classmethod
def check_consistency(cls, v, values: ValidationInfo):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v: Any ?

def default_meta(cls, v: Any, values: ValidationInfo):
if v is None or hasattr(v, "model_dump"):
v = Meta().model_dump() # type: ignore[call-arg]
if v.get("total_doc", None) is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second arg None to get isn't needed I think. get returns None by default.

Comment on lines +82 to +85
if getattr(values, "data", None) is not None:
v["total_doc"] = len(values.data)
else:
v["total_doc"] = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be shortened to inline v["total_doc"] = ... if ... else ...

Comment on lines +55 to +60
remainder = {
k: v
for query in queries
for k, v in query.items()
if k not in ["criteria", "properties"]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be removed if using sub_query.pop() in preceding for loop and if it's ok to modify queries.

Comment on lines +88 to +93
remainder = {
k: v
for query in queries
for k, v in query.items()
if k not in ["criteria", "properties", "facets"]
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove by using pop() above?

def api_sanitize(
pydantic_model: BaseModel,
fields_to_leave: str | None = None,
allow_dict_msonable=False,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allow_dict_msonable: bool = False

Comment on lines +170 to +172
if field_type is not None and allow_dict_msonable:
if lenient_issubclass(field_type, MSONable):
field_type = allow_msonable_dict(field_type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names of arg ('allow_dict_msonable) vs function (allow_msonable_dict`) tripped me up for a second here :) Maybe it can be changed to be more distinguishable.

if lenient_issubclass(field_type, MSONable):
field_type = allow_msonable_dict(field_type)
else:
for sub_type in get_args(field_type):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're only going down two levels looking for msonable dicts?

if v.get("@class", "") != monty_cls.__name__:
errors.append("@class")

if len(errors) > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if errors should suffice here, right?

Comment on lines +513 to +537
def get_flat_models_from_model(
model: BaseModel, known_models: set[BaseModel] = set()
) -> set[BaseModel]:
"""Get all sub-models from a pydantic model.

Args:
model (BaseModel): Pydantic model
known_models (set[BaseModel]) : set of identified pydantic sub-models

Returns:
(set[BaseModel]): Set of pydantic models
"""
known_models = set()

def get_sub_models(model: Any):
if lenient_issubclass(model, BaseModel):
known_models.add(model)
for field_info in model.model_fields.values():
get_sub_models(field_info.annotation)
else:
for type_anno in get_args(model):
get_sub_models(type_anno)

get_sub_models(model)
return known_models
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does pydantic have a built-in function for this?

@kbuma
Copy link
Contributor Author

kbuma commented Aug 18, 2025

@tschaume
Because 2 methods were moved to emmet.core.utils it requires a new emmet-core build.

We purposefully did not try to improve the code of the files moved over from maggma in order to reduce risk. You'll see we've tried not to modify the contents when possible. Let us know if you'd like us to do that. Otherwise, we will forego addressing the suggestions as part of this PR and address when we do the next set of changes related to removing the Stores.

@tschaume tschaume self-requested a review August 18, 2025 21:13
Copy link
Member

@tschaume tschaume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kbuma Thanks, makes sense! I don't think my code changes need to be addressed as part of this PR then. Could you make sure to transfer my comments to the new PR related to removing Stores?

@tsmathis tsmathis merged commit bef2856 into materialsproject:main Aug 18, 2025
10 checks passed
@kbuma kbuma deleted the maggma-api-refactor branch August 18, 2025 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants