Skip to content

Commit bd94974

Browse files
clean up top-level rester init
1 parent 9625174 commit bd94974

3 files changed

Lines changed: 36 additions & 42 deletions

File tree

mp_api/client/core/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def validate_api_key(api_key: str | None = None) -> str | None:
6565
api_key = PMG_SETTINGS.get("PMG_MAPI_KEY")
6666

6767
if not api_key:
68+
# The web server requires the client to initialize without an API key.
69+
# Only warn the user if the API key cannot be identified to permit
70+
# the web server to run.
6871
warnings.warn(
6972
"No API key found, please set explicitly or in "
7073
"the `MP_API_KEY` environment variable.",

mp_api/client/mprester.py

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@
5959
**GENERIC_RESTERS,
6060
}
6161

62+
TOP_LEVEL_RESTERS = [
63+
"molecules/core",
64+
"materials/core",
65+
"_general_store",
66+
"_messages",
67+
"_user_settings",
68+
"doi",
69+
]
70+
6271

6372
class MPRester:
6473
"""Access the new Materials Project API."""
@@ -181,44 +190,23 @@ def __init__(
181190
# Nested rested are then setup to be loaded dynamically with custom __getattr__ functions.
182191
self._all_resters = list(RESTER_LAYOUT.values())
183192

184-
# Instantiate top level molecules and materials resters and set them as attributes
185-
core_suffix = ["molecules/core", "materials/core"]
186-
187-
core_resters = {
188-
rest_name.split("/")[0]: lazy_rester(
189-
api_key=self.api_key,
190-
endpoint=self.endpoint,
191-
include_user_agent=self._include_user_agent,
192-
session=self.session,
193-
use_document_model=self.use_document_model,
194-
headers=self.headers,
195-
mute_progress_bars=self.mute_progress_bars,
196-
)
197-
for rest_name, lazy_rester in RESTER_LAYOUT.items()
198-
if rest_name in core_suffix
199-
}
200-
201-
# Set remaining top level resters, or get an attribute-class name mapping
202-
203-
for attr, rester in core_resters.items():
204-
setattr(self, attr, rester)
205-
206-
# set generics TODO!: refactor
207-
generic_resters = {
208-
rest_name: lazy_rester(
209-
api_key=self.api_key,
210-
endpoint=self.endpoint,
211-
include_user_agent=self._include_user_agent,
212-
session=self.session,
213-
use_document_model=self.use_document_model,
214-
headers=self.headers,
215-
mute_progress_bars=self.mute_progress_bars,
216-
)
217-
for rest_name, lazy_rester in GENERIC_RESTERS.items()
218-
}
219-
220-
for attr, rester in generic_resters.items():
221-
setattr(self, attr, rester)
193+
# Instantiate top level core molecules, materials, and DOI resters, as well
194+
# as the sunder resters to allow the web server to work.
195+
for rest_name, lazy_rester in (RESTER_LAYOUT | GENERIC_RESTERS).items():
196+
if rest_name in TOP_LEVEL_RESTERS:
197+
setattr(
198+
self,
199+
rest_name.split("/")[0],
200+
lazy_rester(
201+
api_key=self.api_key,
202+
endpoint=self.endpoint,
203+
include_user_agent=self._include_user_agent,
204+
session=self.session,
205+
use_document_model=self.use_document_model,
206+
headers=self.headers,
207+
mute_progress_bars=self.mute_progress_bars,
208+
),
209+
)
222210

223211
@property
224212
def contribs(self):
@@ -268,7 +256,11 @@ def __getattr__(self, attr):
268256
)
269257

270258
def __dir__(self):
271-
return dir(MPRester) + self._deprecated_attributes + ["materials", "molecules"]
259+
return (
260+
dir(MPRester)
261+
+ self._deprecated_attributes
262+
+ [r.split("/", 1)[0] for r in TOP_LEVEL_RESTERS if not r.startswith("_")]
263+
)
272264

273265
def get_task_ids_associated_with_material_id(
274266
self, material_id: str, calc_types: list[CalcType] | None = None

tests/client/core/test_utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""Test client core utilities."""
22

3-
from packaging.version import parse as parse_version
43
import pytest
54

6-
from mp_api.client.core.exceptions import MPRestError
5+
from mp_api.client.core.exceptions import MPRestError, MPRestWarning
76
from mp_api.client.core.utils import LazyImport
87

98

@@ -145,7 +144,7 @@ def test_api_key_validation(monkeypatch: pytest.MonkeyPatch):
145144
with pytest.raises(MPRestError, match="32 characters"):
146145
validate_api_key("invalid_key")
147146

148-
with pytest.raises(MPRestError, match="Please obtain a valid"):
147+
with pytest.warns(MPRestWarning, match="No API key found"):
149148
validate_api_key()
150149

151150
junk_api_key = "a" * 32

0 commit comments

Comments
 (0)