Skip to content

Commit

Permalink
Properly override __new__ for MPRester.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyue Ping Ong committed Oct 2, 2023
1 parent 650df90 commit ffba535
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
18 changes: 8 additions & 10 deletions pymatgen/ext/matproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,19 +376,17 @@ def __new__(cls, *args, **kwargs):
if not api_key:
raise ValueError("Please supply an API key. See https://materialsproject.org/api for details.")

try:
from mp_api.client import MPRester as _MPResterNew
except Exception:
_MPResterNew = _MPResterBasic

if len(api_key) == 32:
rester = _MPResterNew
else:
if len(api_key) != 32:
from pymatgen.ext.matproj_legacy import _MPResterLegacy

rester = _MPResterLegacy
return _MPResterLegacy.__new__(cls)

return rester(*args, **kwargs)
try:
from mp_api.client import MPRester as _MPResterNew

return _MPResterNew.__new__(cls)
except Exception:
return _MPResterBasic.__new__(cls)


class MPRestError(Exception):
Expand Down
4 changes: 2 additions & 2 deletions tests/ext/test_matproj.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,8 +892,8 @@ def test_get_structures(self):
def test_parity_with_mp_api(self):
try:
from mp_api.client import MPRester as MPResterMPAPI
except ImportError:
pytest.importorskip("mp_api.client.MPRester")
except Exception:
pytest.skip("mp_api.client.MPRester cannot be imported for this test.")
mpr_mpapi = MPResterMPAPI(PMG_MAPI_KEY)
# Test summary
mp_data = mpr_mpapi.summary.search(formula="Al2O3")
Expand Down

0 comments on commit ffba535

Please sign in to comment.