Skip to content

Commit

Permalink
Update max values in settings (#875)
Browse files Browse the repository at this point in the history
* Set max value for api client settings

* Add upper bound on ID list queries

* Linting

* Spelling
  • Loading branch information
munrojm authored Dec 6, 2023
1 parent 5351c9b commit 0a1561a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions mp_api/client/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
from mp_api.client import __file__ as root_dir

PMG_SETTINGS = _load_pmg_settings()
_NUM_PARALLEL_REQUESTS = PMG_SETTINGS.get("MPRESTER_NUM_PARALLEL_REQUESTS", 8)
_MAX_RETRIES = PMG_SETTINGS.get("MPRESTER_MAX_RETRIES", 3)
_NUM_PARALLEL_REQUESTS = min(PMG_SETTINGS.get("MPRESTER_NUM_PARALLEL_REQUESTS", 8), 8)
_MAX_RETRIES = min(PMG_SETTINGS.get("MPRESTER_MAX_RETRIES", 3), 3)
_MUTE_PROGRESS_BAR = PMG_SETTINGS.get("MPRESTER_MUTE_PROGRESS_BARS", False)
_MAX_HTTP_URL_LENGTH = PMG_SETTINGS.get("MPRESTER_MAX_HTTP_URL_LENGTH", 2000)
_MAX_LIST_LENGTH = min(PMG_SETTINGS.get("MPRESTER_MAX_LIST_LENGTH", 40000), 40000)

try:
CPU_COUNT = cpu_count()
Expand Down Expand Up @@ -82,5 +83,9 @@ class MAPIClientSettings(BaseSettings):
"0.54.0", description="Minimum compatible version of emmet-core for the client."
)

MAX_LIST_LENGTH: int = Field(
_MAX_LIST_LENGTH, description="Maximum length of query parameter list"
)

class Config:
env_prefix = "MPRESTER_"
8 changes: 8 additions & 0 deletions mp_api/client/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from pydantic._internal._utils import lenient_issubclass
from pydantic.fields import FieldInfo

from mp_api.client.core.settings import MAPIClientSettings


def validate_ids(id_list: list[str]):
"""Function to validate material and task IDs.
Expand All @@ -23,6 +25,12 @@ def validate_ids(id_list: list[str]):
Returns:
id_list: Returns original ID list if everything is formatted correctly.
"""
if len(id_list) >= MAPIClientSettings().MAX_LIST_LENGTH:
raise ValueError(
"List of material/molecule IDs provided is too long. Consider removing the ID filter to automatically pull"
" data for all IDs and filter locally."
)

pattern = "(mp|mvc|mol|mpcule)-.*"

for entry in id_list:
Expand Down

0 comments on commit 0a1561a

Please sign in to comment.