Skip to content

Commit

Permalink
Fix typing_extension ImportError in downstream packages (#3752)
Browse files Browse the repository at this point in the history
* guard from typing_extensions import Self with TYPE_CHECKING

in pymatgen/analysis/elasticity/stress.py, pymatgen/ext/optimade.py, and pymatgen/io/abinit/netcdf.py

* pymatgen/core/__init__.py drop ruff: noqa: PLC0414 and format imports

* remove ruff noqas and fix
  • Loading branch information
janosh committed Apr 12, 2024
1 parent 4adc00f commit f81535f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ci:

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
rev: v0.3.7
hooks:
- id: ruff
args: [--fix, --unsafe-fixes]
Expand Down
5 changes: 4 additions & 1 deletion pymatgen/analysis/elasticity/stress.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
from __future__ import annotations

import math
from typing import TYPE_CHECKING

import numpy as np
from typing_extensions import Self

from pymatgen.core.tensors import SquareTensor

if TYPE_CHECKING:
from typing_extensions import Self

__author__ = "Joseph Montoya"
__copyright__ = "Copyright 2012, The Materials Project"
__credits__ = "Maarten de Jong, Mark Asta, Anubhav Jain"
Expand Down
27 changes: 7 additions & 20 deletions pymatgen/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# ruff: noqa: PLC0414
"""This package contains core modules and classes for representing structures and operations on them."""

from __future__ import annotations
Expand All @@ -10,25 +9,13 @@

from ruamel.yaml import YAML

from pymatgen.core.composition import Composition as Composition
from pymatgen.core.lattice import Lattice as Lattice
from pymatgen.core.operations import SymmOp as SymmOp
from pymatgen.core.periodic_table import DummySpecie as DummySpecie
from pymatgen.core.periodic_table import DummySpecies as DummySpecies
from pymatgen.core.periodic_table import Element as Element
from pymatgen.core.periodic_table import Species as Species
from pymatgen.core.periodic_table import get_el_sp as get_el_sp
from pymatgen.core.sites import PeriodicSite as PeriodicSite
from pymatgen.core.sites import Site as Site
from pymatgen.core.structure import IMolecule as IMolecule
from pymatgen.core.structure import IStructure as IStructure
from pymatgen.core.structure import Molecule as Molecule
from pymatgen.core.structure import PeriodicNeighbor as PeriodicNeighbor
from pymatgen.core.structure import SiteCollection as SiteCollection
from pymatgen.core.structure import Structure as Structure
from pymatgen.core.units import ArrayWithUnit as ArrayWithUnit
from pymatgen.core.units import FloatWithUnit as FloatWithUnit
from pymatgen.core.units import Unit as Unit
from pymatgen.core.composition import Composition
from pymatgen.core.lattice import Lattice
from pymatgen.core.operations import SymmOp
from pymatgen.core.periodic_table import DummySpecie, DummySpecies, Element, Species, get_el_sp
from pymatgen.core.sites import PeriodicSite, Site
from pymatgen.core.structure import IMolecule, IStructure, Molecule, PeriodicNeighbor, SiteCollection, Structure
from pymatgen.core.units import ArrayWithUnit, FloatWithUnit, Unit

__author__ = "Pymatgen Development Team"
__email__ = "pymatgen@googlegroups.com"
Expand Down
5 changes: 4 additions & 1 deletion pymatgen/ext/optimade.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
import logging
import sys
from collections import namedtuple
from typing import TYPE_CHECKING
from urllib.parse import urljoin, urlparse

import requests
from tqdm import tqdm
from typing_extensions import Self

from pymatgen.core import DummySpecies, Structure
from pymatgen.util.due import Doi, due
from pymatgen.util.provenance import StructureNL

if TYPE_CHECKING:
from typing_extensions import Self

# TODO: importing optimade-python-tool's data structures will make more sense
Provider = namedtuple("Provider", ["name", "base_url", "description", "homepage", "prefix"])

Expand Down
5 changes: 4 additions & 1 deletion pymatgen/io/abinit/netcdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@
import logging
import os.path
import warnings
from typing import TYPE_CHECKING

import numpy as np
from monty.collections import AttrDict
from monty.dev import requires
from monty.functools import lazy_property
from monty.string import marquee
from typing_extensions import Self

from pymatgen.core.structure import Structure
from pymatgen.core.units import ArrayWithUnit
from pymatgen.core.xcfunc import XcFunc

if TYPE_CHECKING:
from typing_extensions import Self

try:
import netCDF4
except ImportError:
Expand Down
16 changes: 9 additions & 7 deletions tests/io/test_packmol.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
from pymatgen.io.packmol import PackmolBoxGen
from pymatgen.util.testing import TEST_FILES_DIR, PymatgenTest

# ruff: noqa: PT011


TEST_DIR = f"{TEST_FILES_DIR}/packmol"

# error message is different in CI for unknown reasons (as of 2024-04-12)
# macOS: "Packmol failed with error code 173 and stderr: b'STOP 173\\n'"
# CI: "Packmol failed with return code 0 and stdout: Packmol was unable to
# put the molecules in the desired regions even without"
ERR_MSG_173 = "Packmol failed with "

if which("packmol") is None:
pytest.skip("packmol executable not present", allow_module_level=True)
Expand Down Expand Up @@ -110,7 +111,7 @@ def test_control_params(self):
input_string = file.read()
assert "maxit 0" in input_string
assert "nloop 0" in input_string
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=ERR_MSG_173):
input_set.run(self.tmp_path)

def test_timeout(self):
Expand Down Expand Up @@ -141,7 +142,7 @@ def test_no_return_and_box(self):
with open(f"{self.tmp_path}/packmol.inp") as file:
input_string = file.read()
assert "inside box 0 0 0 2 2 2" in input_string
with pytest.raises(ValueError):
with pytest.raises(ValueError, match=ERR_MSG_173):
pw.run(self.tmp_path)

def test_chdir_behavior(self):
Expand All @@ -159,7 +160,8 @@ def test_chdir_behavior(self):
box=[0, 0, 0, 2, 2, 2],
)
pw.write_input(self.tmp_path)
with pytest.raises(ValueError):

with pytest.raises(ValueError, match=ERR_MSG_173):
pw.run(self.tmp_path)
assert str(Path.cwd()) == start_dir

Expand Down
1 change: 0 additions & 1 deletion tests/transformations/test_standard_transformations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# ruff: noqa: N806
from __future__ import annotations

import functools
Expand Down

0 comments on commit f81535f

Please sign in to comment.