Skip to content

Commit 0b25745

Browse files
committed
make pybtex optional
1 parent a73d390 commit 0b25745

File tree

6 files changed

+26
-11
lines changed

6 files changed

+26
-11
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ dependencies = [
6262
"palettable>=3.3.3",
6363
"pandas>=2",
6464
"plotly>=4.5.0,<6.0.0",
65-
"pybtex>=0.24.0",
6665
"requests>=2.32",
6766
"ruamel.yaml>=0.17.0",
6867
# scipy<1.14.1 is incompatible with NumPy 2.0 on Windows
@@ -110,6 +109,7 @@ optional = [
110109
"matplotlib>=3.8",
111110
"openbabel-wheel>=3.1.1.20",
112111
"phonopy>=2.33.3",
112+
"pybtex>=0.24.0", # TODO: don't work on MacOS Python 3.13
113113
"seekpath>=2.0.1",
114114
]
115115
# moyopy[interface] includes ase

src/pymatgen/io/cif.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1357,8 +1357,8 @@ def get_bibtex_string(self) -> str:
13571357
"""
13581358
try:
13591359
from pybtex.database import BibliographyData, Entry
1360-
except ImportError:
1361-
raise RuntimeError("Bibliographic data extraction requires pybtex.")
1360+
except ImportError as exc:
1361+
raise RuntimeError("Bibliographic data extraction requires pybtex.") from exc
13621362

13631363
bibtex_keys: dict[str, tuple[str, ...]] = {
13641364
"author": ("_publ_author_name", "_citation_author_name"),

src/pymatgen/util/provenance.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,10 @@
99
from io import StringIO
1010
from typing import TYPE_CHECKING, NamedTuple
1111

12-
from monty.dev import requires
1312
from monty.json import MontyDecoder, MontyEncoder
1413

1514
from pymatgen.core.structure import Molecule, Structure
1615

17-
try:
18-
from pybtex import errors
19-
from pybtex.database.input import bibtex
20-
except ImportError:
21-
pybtex = bibtex = errors = None
22-
2316
if TYPE_CHECKING:
2417
from collections.abc import Sequence
2518
from typing import Any
@@ -37,7 +30,6 @@
3730
MAX_BIBTEX_CHARS: int = 20_000 # maximum number of characters for BibTeX reference
3831

3932

40-
@requires(bibtex is not None, "pybtex is not available")
4133
def is_valid_bibtex(reference: str) -> bool:
4234
"""Use pybtex to validate that a reference is in proper BibTeX format.
4335
@@ -47,6 +39,12 @@ def is_valid_bibtex(reference: str) -> bool:
4739
Returns:
4840
bool: True if reference is valid BibTeX.
4941
"""
42+
try:
43+
from pybtex import errors
44+
from pybtex.database.input import bibtex
45+
except ImportError as exc:
46+
raise ImportError("pybtex is not available") from exc
47+
5048
# str is necessary since pybtex seems to have an issue with unicode.
5149
# The filter expression removes all non-ASCII characters.
5250
str_io = StringIO(reference.encode("ascii", "ignore").decode("ascii"))

tests/analysis/test_cost.py

+8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22

33
from unittest import TestCase
44

5+
import pytest
56
from pytest import approx
67

78
from pymatgen.analysis.cost import CostAnalyzer, CostDBCSV, CostDBElements
89
from pymatgen.util.testing import TEST_FILES_DIR
910

11+
try:
12+
# Not using find_spec because it would error out during import
13+
import pybtex.database # noqa: F401
14+
15+
except ImportError:
16+
pytest.skip("pybtex is not available", allow_module_level=True)
17+
1018
TEST_DIR = f"{TEST_FILES_DIR}/analysis/cost"
1119

1220

tests/io/test_cif.py

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
try:
1515
import pybtex
16+
import pybtex.database
1617
except ImportError:
1718
pybtex = None
1819

@@ -165,6 +166,7 @@ def test_long_loop(self):
165166

166167

167168
class TestCifIO(PymatgenTest):
169+
@pytest.mark.skipif(pybtex is None, reason="pybtex not present")
168170
def test_cif_parser(self):
169171
parser = CifParser(f"{TEST_FILES_DIR}/cif/LiFePO4.cif")
170172
for struct in parser.parse_structures():

tests/util/test_provenance.py

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
from pymatgen.core.structure import Molecule, Structure
1212
from pymatgen.util.provenance import Author, HistoryNode, StructureNL
1313

14+
try:
15+
# Not using find_spec because it would error out during import
16+
import pybtex.database # noqa: F401
17+
18+
except ImportError:
19+
pytest.skip("pybtex is not available", allow_module_level=True)
20+
1421
__author__ = "Anubhav Jain"
1522
__credits__ = "Shyue Ping Ong"
1623
__copyright__ = "Copyright 2012, The Materials Project"

0 commit comments

Comments
 (0)