Skip to content

Commit

Permalink
Merge pull request #818 from zerothi/pep-585-fixes
Browse files Browse the repository at this point in the history
fixing many simple PEP-585 deprecations
  • Loading branch information
zerothi authored Aug 8, 2024
2 parents 2334f69 + 2c5e221 commit a64d17f
Show file tree
Hide file tree
Showing 202 changed files with 328 additions and 571 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ we hit release version 1.0.0.
- A new `AtomicMatrixPlot` to plot sparse matrices, #668

### Fixed
- PEP-585 compliant
- buildable for numpy>2, #791
- `BrillouinZone.tocartesian()` now defaults to `k=self.k`
- reading XV/STRUCT files from fdf siles could cause problems, #778
Expand Down
1 change: 0 additions & 1 deletion benchmarks/bz_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import os
import sys
import time

import numpy as np

Expand Down
2 changes: 0 additions & 2 deletions benchmarks/graphene_within.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import sys

import numpy as np

import sisl

method = "cube"
Expand Down
1 change: 0 additions & 1 deletion benchmarks/sparse_matrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from __future__ import annotations

import cProfile
import io
import pstats
import sys

Expand Down
4 changes: 2 additions & 2 deletions src/sisl/_core/_ufuncs_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from functools import reduce
from numbers import Integral
from typing import Callable, List, Literal, Optional, Tuple, Union
from typing import Callable, Literal, Optional, Union

import numpy as np

Expand Down Expand Up @@ -166,7 +166,7 @@ def apply(
@register_sisl_dispatch(Geometry, module="sisl")
def sort(
geometry: Geometry, **kwargs
) -> Union[Geometry, Tuple[Geometry, List[List[int]]]]:
) -> Union[Geometry, tuple[Geometry, list[list[int]]]]:
r"""Sort atoms in a nested fashion according to various criteria
There are many ways to sort a `Geometry`.
Expand Down
5 changes: 3 additions & 2 deletions src/sisl/_core/_ufuncs_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from __future__ import annotations

from typing import Optional, Sequence, Union
from collections.abc import Sequence
from typing import Union

import numpy as np

import sisl._array as _a
from sisl._ufuncs import register_sisl_dispatch
from sisl.messages import SislError
from sisl.typing import CellAxes, CellAxis, GridLike, SileLike
from sisl.typing import CellAxis, GridLike, SileLike
from sisl.utils import import_attr
from sisl.utils.misc import direction

Expand Down
2 changes: 1 addition & 1 deletion src/sisl/_core/_ufuncs_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import math
from functools import reduce
from numbers import Integral
from typing import Optional, Union
from typing import Union

import numpy as np

Expand Down
2 changes: 0 additions & 2 deletions src/sisl/_core/_ufuncs_orbital.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from __future__ import annotations

import numpy as np

from sisl._ufuncs import register_sisl_dispatch

from .orbital import (
Expand Down
8 changes: 0 additions & 8 deletions src/sisl/_core/_ufuncs_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,5 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from __future__ import annotations

import numpy as np

from sisl._indices import indices
from sisl._ufuncs import register_sisl_dispatch
from sisl.typing import SparseMatrixExt

from .sparse import SparseCSR

# Nothing gets exposed here
__all__ = []
3 changes: 1 addition & 2 deletions src/sisl/_core/_ufuncs_sparse_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import numpy as np

from sisl import _array as _a
from sisl._core.geometry import Geometry
from sisl._ufuncs import register_sisl_dispatch
from sisl.typing import AtomsIndex

from .sparse import SparseCSR, _ncol_to_indptr, issparse
from .sparse import SparseCSR, _ncol_to_indptr
from .sparse_geometry import SparseAtom, SparseOrbital, _SparseGeometry

# Nothing gets exposed here
Expand Down
38 changes: 15 additions & 23 deletions src/sisl/_core/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@
import logging
import warnings
from collections import OrderedDict
from collections.abc import Iterator, Sequence
from functools import singledispatchmethod
from itertools import product
from math import acos
from numbers import Integral, Real
from pathlib import Path
from typing import Iterator, List, Optional, Sequence, Tuple, Union
from typing import Optional, Union

import numpy as np
import numpy.typing as npt
from numpy import (
argsort,
bool_,
ceil,
concatenate,
diff,
dot,
floor,
int32,
isin,
ndarray,
split,
sqrt,
square,
tile,
Expand All @@ -39,7 +37,7 @@
from sisl._category import Category, GenericCategory
from sisl._dispatch_class import _Dispatchs
from sisl._dispatcher import AbstractDispatch, ClassDispatcher, TypeDispatcher
from sisl._help import isndarray
from sisl._help import has_module, isndarray
from sisl._indices import (
indices_gt_le,
indices_in_sphere_with_dist,
Expand Down Expand Up @@ -310,7 +308,7 @@ def lasto(self) -> npt.NDArray[np.int32]:
return self.atoms.lasto

@property
def orbitals(self) -> List[Orbital]:
def orbitals(self) -> list[Orbital]:
"""List of orbitals per atom"""
return self.atoms.orbitals

Expand Down Expand Up @@ -505,7 +503,7 @@ def _(self, orbitals: bool) -> ndarray:

def as_primary(
self, na_primary: int, axes: Sequence[int] = (0, 1, 2), ret_super: bool = False
) -> Union[Geometry, Tuple[Geometry, Lattice]]:
) -> Union[Geometry, tuple[Geometry, Lattice]]:
"""Reduce the geometry to the primary unit-cell comprising `na_primary` atoms
This will basically try and find the tiling/repetitions required for the geometry to only have
Expand Down Expand Up @@ -952,7 +950,7 @@ def iter_block_rand(
iR: int = 20,
R: Optional[float] = None,
atoms: AtomsIndex = None,
) -> Iterator[Tuple[ndarray, ndarray]]:
) -> Iterator[tuple[ndarray, ndarray]]:
"""Perform the *random* block-iteration by randomly selecting the next center of block"""

# We implement yields as we can then do nested iterators
Expand Down Expand Up @@ -1031,7 +1029,7 @@ def iter_block_rand(

def iter_block_shape(
self, shape=None, iR: int = 20, atoms: AtomsIndex = None
) -> Iterator[Tuple[ndarray, ndarray]]:
) -> Iterator[tuple[ndarray, ndarray]]:
"""Perform the *grid* block-iteration by looping a grid"""

# We implement yields as we can then do nested iterators
Expand Down Expand Up @@ -1166,7 +1164,7 @@ def iter_block(
R: Optional[float] = None,
atoms: AtomsIndex = None,
method: str = "rand",
) -> Iterator[Tuple[ndarray, ndarray]]:
) -> Iterator[tuple[ndarray, ndarray]]:
"""Iterator for performance critical loops
NOTE: This requires that `R` has been set correctly as the maximum interaction range.
Expand Down Expand Up @@ -1244,7 +1242,7 @@ def overlap(
atol: float = 0.1,
offset: Sequence[float] = (0.0, 0.0, 0.0),
offset_other: Sequence[float] = (0.0, 0.0, 0.0),
) -> Tuple[ndarray, ndarray]:
) -> tuple[ndarray, ndarray]:
"""Calculate the overlapping indices between two geometries
Find equivalent atoms (in the primary unit-cell only) in two geometries.
Expand Down Expand Up @@ -2966,7 +2964,7 @@ def isc_tile(isc, n):

def a2transpose(
self, atoms1: AtomsIndex, atoms2: AtomsIndex = None
) -> Tuple[ndarray, ndarray]:
) -> tuple[ndarray, ndarray]:
"""Transposes connections from `atoms1` to `atoms2` such that supercell connections are transposed
When handling supercell indices it is useful to get the *transposed* connection. I.e. if you have
Expand Down Expand Up @@ -3032,7 +3030,7 @@ def a2transpose(

def o2transpose(
self, orb1: OrbitalsIndex, orb2: Optional[OrbitalsIndex] = None
) -> Tuple[ndarray, ndarray]:
) -> tuple[ndarray, ndarray]:
"""Transposes connections from `orb1` to `orb2` such that supercell connections are transposed
When handling supercell indices it is useful to get the *transposed* connection. I.e. if you have
Expand Down Expand Up @@ -3548,7 +3546,7 @@ def within_inf(
periodic: Optional[Union[Sequence[bool], CellAxes]] = None,
atol: float = 1e-5,
origin: Sequence[float] = (0.0, 0.0, 0.0),
) -> Tuple[ndarray, ndarray, ndarray]:
) -> tuple[ndarray, ndarray, ndarray]:
"""Find all atoms within a provided supercell
Note this function is rather different from `close` and `within`.
Expand Down Expand Up @@ -3683,7 +3681,7 @@ def within_inf(
# infinite supercell indices
return self.asc2uc(idx), xyz, isc

def _orbital_values(self, grid_shape: Tuple[int, int, int]):
def _orbital_values(self, grid_shape: tuple[int, int, int]):
r"""Calculates orbital values for a given grid.
Parameters
Expand Down Expand Up @@ -4562,15 +4560,12 @@ def dispatch(self, **kwargs):


to_dispatch.register("ase", GeometryToAseDispatch)
try:
if has_module("ase"):
from ase import Atoms as ase_Atoms

to_dispatch.register(ase_Atoms, GeometryToAseDispatch)
del ase_Atoms

except ImportError:
pass


class GeometryTopymatgenDispatch(GeometryToDispatch):
def dispatch(self, **kwargs):
Expand Down Expand Up @@ -4629,15 +4624,12 @@ def dispatch(self, *args, **kwargs):


to_dispatch.register("dataframe", GeometryToDataframeDispatch)
try:
if has_module("pandas"):
from pandas import DataFrame as pd_DataFrame

to_dispatch.register(pd_DataFrame, GeometryToDataframeDispatch)
del pd_DataFrame

except ImportError:
pass


# Clean up
del new_dispatch, to_dispatch
Expand Down
3 changes: 1 addition & 2 deletions src/sisl/_core/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -1709,7 +1709,7 @@ def sgrid(grid=None, argv=None, ret_grid=False):
import sys
from pathlib import Path

from sisl.io import BaseSile, get_sile
from sisl.io import BaseSile

# The file *MUST* be the first argument
# (except --help|-h)
Expand Down Expand Up @@ -1757,7 +1757,6 @@ def sgrid(grid=None, argv=None, ret_grid=False):
# First read the input "Sile"
stdout_grid = True
if grid is None:
from os.path import isfile

argv, input_file = cmd.collect_input(argv)

Expand Down
9 changes: 5 additions & 4 deletions src/sisl/_core/lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@

import logging
import math
from collections.abc import Sequence
from enum import IntEnum, auto
from numbers import Integral
from pathlib import Path
from typing import Optional, Sequence, Tuple, Union
from typing import Optional, Union

import numpy as np
from numpy import dot, ndarray
Expand Down Expand Up @@ -349,7 +350,7 @@ def conv(v):

def parameters(
self, rad: bool = False
) -> Tuple[float, float, float, float, float, float]:
) -> tuple[float, float, float, float, float, float]:
r"""Cell parameters of this cell in 3 lengths and 3 angles
Notes
Expand Down Expand Up @@ -609,7 +610,7 @@ def fit(self, xyz, axes: CellAxes = (0, 1, 2), atol: float = 0.05) -> Lattice:

def plane(
self, axis1: CellAxis, axis2: CellAxis, origin: bool = True
) -> Tuple[ndarray, ndarray]:
) -> tuple[ndarray, ndarray]:
"""Query point and plane-normal for the plane spanning `ax1` and `ax2`
Parameters
Expand Down Expand Up @@ -755,7 +756,7 @@ def cell2length(self, length, axes: CellAxes = (0, 1, 2)) -> ndarray:

return self.cell[axes] * (length / self.length[axes]).reshape(-1, 1)

def offset(self, isc=None) -> Tuple[float, float, float]:
def offset(self, isc=None) -> tuple[float, float, float]:
"""Returns the supercell offset of the supercell index"""
if isc is None:
return _a.arrayd([0, 0, 0])
Expand Down
8 changes: 3 additions & 5 deletions src/sisl/_core/orbital.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
from __future__ import annotations

from collections import namedtuple
from collections.abc import Iterable
from functools import partial
from math import factorial as fact
from math import pi
from math import sqrt as msqrt
from numbers import Integral, Real
from typing import Callable, Optional, Tuple
from typing import Callable, Optional

import numpy as np
import numpy.typing as npt
import scipy
from numpy import cos, sin, sqrt, square, take
from numpy import cos, sin, take
from scipy.special import eval_genlaguerre, factorial, lpmv

try:
Expand Down Expand Up @@ -341,7 +339,7 @@ def __setstate__(self, d):
def radial_minimize_range(
radial_func: Callable[[RadialFuncT], npt.NDArray],
contains: float,
dr: Tuple[float, float] = (0.01, 0.0001),
dr: tuple[float, float] = (0.01, 0.0001),
maxR: float = 100,
func: Optional[Callable[[RadialFuncT, npt.ArrayLike], npt.NDArray]] = None,
) -> float:
Expand Down
1 change: 0 additions & 1 deletion src/sisl/_core/sparse_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from __future__ import annotations

import functools as ftool
import warnings
from collections import namedtuple
from numbers import Integral
Expand Down
2 changes: 0 additions & 2 deletions src/sisl/_core/tests/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from __future__ import annotations

import math as m

import numpy as np
import pytest

Expand Down
2 changes: 0 additions & 2 deletions src/sisl/_core/tests/test_atoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from __future__ import annotations

import math as m

import numpy as np
import pytest

Expand Down
2 changes: 0 additions & 2 deletions src/sisl/_core/tests/test_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from __future__ import annotations

import itertools
import math as m
import os.path as osp

import numpy as np
import pytest
Expand Down
Loading

0 comments on commit a64d17f

Please sign in to comment.