Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/upgrade_dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
matrix:
os: ['ubuntu-latest']
package: ["mp-api"]
python-version: ["3.11", "3.12", "3.13", "3.14"]
python-version: ["3.12", "3.13", "3.14"]
steps:
- uses: actions/checkout@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

[![testing](https://github.com/materialsproject/api/actions/workflows/testing.yml/badge.svg?branch=main)](https://github.com/materialsproject/api/actions?query=workflow%3Atesting+branch%3Amain)
[![codecov](https://codecov.io/gh/materialsproject/api/branch/main/graph/badge.svg)](https://codecov.io/gh/materialsproject/api)
![python](https://img.shields.io/badge/Python-3.11+-blue.svg?logo=python&logoColor=white)
![python](https://img.shields.io/badge/Python-3.12+-blue.svg?logo=python&logoColor=white)

This repository is the development environment for the new Materials Project API. A core client implementation will reside here. For information on how to use the API, please see the updated [documentation](https://docs.materialsproject.org/downloading-data/how-do-i-download-the-materials-project-database).
1 change: 0 additions & 1 deletion mp_api/_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ def _normalize(doc, field: str):
if k not in ("_page", "_sort_fields", "chunk_size", "fields")
}
for sort_field in [sort_fields] if isinstance(sort_fields, str) else sort_fields:

asc = search_method(
_page=1,
_sort_fields=sort_field,
Expand Down
38 changes: 35 additions & 3 deletions mp_api/client/core/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@

from __future__ import annotations

from functools import cached_property
import logging
from functools import cached_property, lru_cache
from importlib import import_module
from importlib.metadata import version
from itertools import chain
from typing import TYPE_CHECKING, ForwardRef, get_args

from emmet.core.utils import jsanitize
from pydantic import BaseModel, create_model
from pydantic import (
BaseModel,
ValidationError,
create_model,
field_validator,
)

if TYPE_CHECKING:
from collections.abc import Iterator
from typing import Any

from pydantic.fields import FieldInfo

logger = logging.getLogger(__name__)


@lru_cache(20)
def _warn_failed_field_validation(logger: logging.Logger, msg: str):
logger.warning(msg)


class _DictLikeAccess(BaseModel):
"""Define a pydantic mix-in which permits dict-like access to model fields."""
Expand Down Expand Up @@ -47,7 +61,7 @@ def __str__(self) -> str:
return (
f"{self.__class__.__name__}(\n"
+ "\n".join(
f" {k} ({annos[k]}) : {getattr(self,k)}" for k in populated_fields
f" {k} ({annos[k]}) : {getattr(self, k)}" for k in populated_fields
)
+ "\n)"
)
Expand All @@ -56,6 +70,24 @@ def __repr__(self) -> str:
"""Match output of str()."""
return self.__str__()

@field_validator("*", mode="wrap")
@classmethod
def ignore_invalid(cls, value, default_validator, info) -> Any:
try:
return default_validator(value)
except ValidationError:
emmet_version = version("emmet-core")
annotation = cls.model_fields[info.field_name].annotation
_warn_failed_field_validation(
logger,
f"Failed validation on field: '{info.field_name}', received type: '{type(value)}', expected type: '{annotation}'. "
"Field value set to 'None', re-run query with 'document_model=False' to skip validation. "
f"Local 'emmet-core' version is: '{emmet_version}', run 'mpr = MPRester(); mpr.get_emmet_version(mpr.version)' to "
"check the live API server for schema version mismatches and upgrade 'emmet-core' if needed.",
)

return None


def _generate_returned_model(
doc: dict[str, Any],
Expand Down
39 changes: 24 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"
requires = ["hatchling", "uv-dynamic-versioning"]
build-backend = "hatchling.build"

[tool.hatch.version]
source = "uv-dynamic-versioning"

[tool.hatch.build.targets.wheel]
only-include = ["mp_api"]
dev-mode-dirs = ["."]

[tool.hatch.build.targets.sdist]
include = [
"mp_api",
"tests",
"pyproject.toml",
]

[project]
name = "mp-api"
dynamic = ["version"]
requires-python = ">=3.12"
description = "API Client for the Materials Project"
license = "BSD-3-Clause-LBNL"
authors = [
{ name = "The Materials Project", email = "feedback@materialsproject.org" },
]
description = "API Client for the Materials Project"
readme = "README.md"
requires-python = ">=3.11"
license = "BSD-3-Clause-LBNL"

classifiers = [
"Programming Language :: Python :: 3",
"Development Status :: 4 - Beta",
Expand All @@ -19,18 +34,18 @@ classifiers = [
"Operating System :: OS Independent",
"Topic :: Scientific/Engineering",
]

dependencies = [
"pymatgen>2024.2.20",
"typing-extensions>=3.7.4.1",
"requests>=2.23.0",
"monty>=2024.12.10",
"emmet-core>=0.87.1",
"emmet-core>=0.87.3rc2,<0.87.4",
"boto3",
"orjson >= 3.10,<4",
"pyarrow >= 20.0.0",
"deltalake>=1.4.0,<1.6.0",
]
dynamic = ["version"]

[project.optional-dependencies]
mcp = ["fastmcp"]
Expand All @@ -50,7 +65,7 @@ contribs = [
]
all = [
"custodian",
"emmet-core[all]>=0.87.1",
"emmet-core[all]>=0.87.3rc2,<0.87.4",
"fastmcp",
"flask",
]
Expand All @@ -75,12 +90,6 @@ docs = ["sphinx"]
[project.scripts]
mpmcp = "mp_api.mcp.server:_run_mp_mcp_server"

[tool.setuptools.packages.find]
include = ["mp_api*"]
namespaces = true

[tool.setuptools_scm]

[tool.pytest.ini_options]
minversion = "6.0"
addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"]
Expand Down
179 changes: 0 additions & 179 deletions requirements/requirements-ubuntu-latest_py3.11.txt

This file was deleted.

Loading
Loading