Skip to content

Commit

Permalink
trying to fix circular imports in typing constructs
Browse files Browse the repository at this point in the history
Sadly, the register function of singledispatch does
not handle the ForwardRefs very elegantly
as the objects references *needs* to be available
in the global space.
This is rather unfortunate as this is exactly what
we are trying to avoid using __future__ annotations.

I'll have to see later if this causes issues in the
documentation build.

Signed-off-by: Nick Papior <[email protected]>
  • Loading branch information
zerothi committed Jan 18, 2024
1 parent fa21085 commit 201298e
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 107 deletions.
12 changes: 12 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.autosummary",
"sphinx.ext.coverage",
"sphinx.ext.intersphinx",
"sphinx.ext.extlinks",
"sphinx.ext.mathjax",
"sphinx.ext.napoleon",
"sphinx.ext.todo",
"sphinx.ext.viewcode",
"sphinx_copybutton",
"sphinx_autodoc_typehints",
"sphinxcontrib.jquery", # a bug in 4.1.0 means search didn't work without explicit extension
"sphinx_inline_tabs",
Expand Down Expand Up @@ -240,6 +242,16 @@
# If this is false we do not have double method sections
# numpydoc_show_class_members = False

# Attributes section will be formatted as methods
numpydoc_attributes_as_param_list = False

# Plot directives for matplotlib
plot_include_source = True
plot_formats = [("png", 90)]
plot_pre_code = """\
import numpy as np
import sisl as si"""

# -----------------------------------------------------------------------------
# Intersphinx configuration
# -----------------------------------------------------------------------------
Expand Down
38 changes: 16 additions & 22 deletions src/sisl/_core/_ufuncs_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,26 @@

from functools import reduce
from numbers import Integral
from typing import TYPE_CHECKING, Optional, Union
from typing import Optional, Union

import numpy as np

import sisl._array as _a
from sisl._ufuncs import register_sisl_dispatch
from sisl.messages import deprecate_argument, warn
from sisl.typing import AtomsArgument, Coord, CoordOrScalar, LatticeOrGeometryLike
from sisl.utils import direction
from sisl.utils.mathematics import fnorm

from .geometry import Geometry
from .lattice import Lattice
from .quaternion import Quaternion

AtomsArgument = "AtomsArgument"
LatticeOrGeometryLike = "LatticeOrGeometryLike"
Coord = "Coord"
CoordOrScalar = "CoordOrScalar"
if TYPE_CHECKING:
from sisl.typing import AtomsArgument, Coord, CoordOrScalar, LatticeOrGeometryLike

# Nothing gets exposed here
__all__ = []


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def copy(geometry: Geometry) -> Geometry:
"""Create a new object with the same content (a copy)."""
g = geometry.__class__(
Expand All @@ -42,7 +36,7 @@ def copy(geometry: Geometry) -> Geometry:
return g


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def tile(geometry: Geometry, reps: int, axis: int) -> Geometry:
"""Tile the geometry to create a bigger one
Expand Down Expand Up @@ -109,7 +103,7 @@ def tile(geometry: Geometry, reps: int, axis: int) -> Geometry:
return geometry.__class__(xyz, atoms=geometry.atoms.tile(reps), lattice=lattice)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def untile(
geometry: Geometry,
reps: int,
Expand Down Expand Up @@ -187,7 +181,7 @@ def untile(
return new


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def repeat(geometry: Geometry, reps: int, axis: int) -> Geometry:
"""Create a repeated geometry
Expand Down Expand Up @@ -266,7 +260,7 @@ def repeat(geometry: Geometry, reps: int, axis: int) -> Geometry:
return geometry.__class__(xyz, atoms=geometry.atoms.repeat(reps), lattice=lattice)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def unrepeat(geometry: Geometry, reps: int, axis: int, *args, **kwargs) -> Geometry:
"""Unrepeats the geometry similarly as `untile`
Expand All @@ -284,7 +278,7 @@ def unrepeat(geometry: Geometry, reps: int, axis: int, *args, **kwargs) -> Geome
return geometry.sub(atoms).untile(reps, axis, *args, **kwargs)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def translate(
geometry: Geometry,
v: CoordOrScalar,
Expand Down Expand Up @@ -322,7 +316,7 @@ def translate(
Geometry.move = Geometry.translate


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def sub(geometry: Geometry, atoms: AtomsArgument) -> Geometry:
"""Create a new `Geometry` with a subset of this `Geometry`
Expand All @@ -348,7 +342,7 @@ def sub(geometry: Geometry, atoms: AtomsArgument) -> Geometry:
)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def remove(geometry: Geometry, atoms: AtomsArgument) -> Geometry:
"""Remove atoms from the geometry.
Expand All @@ -372,7 +366,7 @@ def remove(geometry: Geometry, atoms: AtomsArgument) -> Geometry:
return geometry.sub(atoms)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
@deprecate_argument(
"only",
"what",
Expand Down Expand Up @@ -490,7 +484,7 @@ def rotate(
return geometry.__class__(xyz, atoms=geometry.atoms.copy(), lattice=lattice)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def swapaxes(
geometry: Geometry,
axes_a: Union[int, str],
Expand Down Expand Up @@ -571,7 +565,7 @@ def swapaxes(
)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def center(
geometry: Geometry, atoms: Optional[AtomsArgument] = None, what: str = "xyz"
) -> np.ndarray:
Expand Down Expand Up @@ -631,7 +625,7 @@ def center(
)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def append(
geometry: Geometry,
other: LatticeOrGeometryLike,
Expand Down Expand Up @@ -719,7 +713,7 @@ def append(
return geometry.__class__(xyz, atoms=atoms, lattice=lattice, names=names)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def prepend(
geometry: Geometry,
other: LatticeOrGeometryLike,
Expand Down Expand Up @@ -807,7 +801,7 @@ def prepend(
return geometry.__class__(xyz, atoms=atoms, lattice=lattice, names=names)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Geometry, module="sisl")
def add(
geometry: Geometry,
other: LatticeOrGeometryLike,
Expand Down
19 changes: 8 additions & 11 deletions src/sisl/_core/_ufuncs_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,22 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
from __future__ import annotations

from typing import TYPE_CHECKING, Optional, Sequence, Union
from typing import Optional, Sequence, 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 GridLike

from .grid import Grid

GridLike = "GridLike"
if TYPE_CHECKING:
from sisl.typing import GridLike

# Nothing gets exposed here
__all__ = []


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Grid, module="sisl")
def copy(grid: Grid, dtype=None):
"""Copy the object, possibly changing the data-type"""
d = grid._sc_geometry_dict()
Expand All @@ -35,7 +32,7 @@ def copy(grid: Grid, dtype=None):
return out


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Grid, module="sisl")
def swapaxes(grid: Grid, axis_a: int, axis_b: int):
"""Swap two axes in the grid (also swaps axes in the lattice)
Expand All @@ -60,7 +57,7 @@ def swapaxes(grid: Grid, axis_a: int, axis_b: int):
return out


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Grid, module="sisl")
def sub(grid: Grid, idx: Union[int, Sequence[int]], axis: int):
"""Retains certain indices from a specified axis.
Expand Down Expand Up @@ -101,7 +98,7 @@ def sub(grid: Grid, idx: Union[int, Sequence[int]], axis: int):
return out


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Grid, module="sisl")
def remove(grid: Grid, idx: Union[int, Sequence[int]], axis: int):
"""Removes certain indices from a specified axis.
Expand All @@ -118,7 +115,7 @@ def remove(grid: Grid, idx: Union[int, Sequence[int]], axis: int):
return grid.sub(ret_idx, axis)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Grid, module="sisl")
def append(grid: Grid, other: GridLike, axis: int):
"""Appends other `Grid` to this grid along axis"""
shape = list(grid.shape)
Expand All @@ -135,7 +132,7 @@ def append(grid: Grid, other: GridLike, axis: int):
return grid.__class__(shape, **d)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Grid, module="sisl")
def tile(grid: Grid, reps: int, axis: int):
"""Tile grid to create a bigger one
Expand Down
27 changes: 12 additions & 15 deletions src/sisl/_core/_ufuncs_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,25 @@
import math
from functools import reduce
from numbers import Integral
from typing import TYPE_CHECKING, Optional, Union
from typing import Optional, Union

import numpy as np

import sisl._array as _a
from sisl._ufuncs import register_sisl_dispatch
from sisl.messages import deprecate_argument
from sisl.typing import Coord
from sisl.utils import direction
from sisl.utils.mathematics import fnorm

from .lattice import Lattice
from .quaternion import Quaternion

Coord = "Coord"
if TYPE_CHECKING:
from sisl.typing import Coord

# Nothing gets exposed here
__all__ = []


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
def copy(lattice: Lattice, cell=None, origin: Optional[Coord] = None) -> Lattice:
"""A deepcopy of the object
Expand Down Expand Up @@ -57,7 +54,7 @@ def copy(lattice: Lattice, cell=None, origin: Optional[Coord] = None) -> Lattice
return copy


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
def swapaxes(
lattice: Lattice,
axes_a: Union[int, str],
Expand Down Expand Up @@ -174,7 +171,7 @@ def swapaxes(
)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
@deprecate_argument(
"only",
"what",
Expand Down Expand Up @@ -228,7 +225,7 @@ def rotate(
return lattice.copy(cell)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
def add(lattice: Lattice, other) -> Lattice:
"""Add two supercell lattice vectors to each other
Expand All @@ -245,7 +242,7 @@ def add(lattice: Lattice, other) -> Lattice:
return lattice.__class__(cell, nsc=nsc, origin=origin)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
def tile(lattice: Lattice, reps: int, axis: int) -> Lattice:
"""Extend the unit-cell `reps` times along the `axis` lattice vector
Expand Down Expand Up @@ -273,7 +270,7 @@ def tile(lattice: Lattice, reps: int, axis: int) -> Lattice:
return lattice.__class__(cell, nsc=nsc, origin=origin)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
def repeat(lattice: Lattice, reps: int, axis: int) -> Lattice:
"""Extend the unit-cell `reps` times along the `axis` lattice vector
Expand All @@ -291,7 +288,7 @@ def repeat(lattice: Lattice, reps: int, axis: int) -> Lattice:
return lattice.tile(reps, axis)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
def untile(lattice: Lattice, reps: int, axis: int) -> Lattice:
"""Reverses a `Lattice.tile` and returns the segmented version
Expand All @@ -312,7 +309,7 @@ def untile(lattice: Lattice, reps: int, axis: int) -> Lattice:
Lattice.unrepeat = untile


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
def append(lattice: Lattice, other, axis: int) -> Lattice:
"""Appends other `Lattice` to this grid along axis"""
cell = np.copy(lattice.cell)
Expand All @@ -321,7 +318,7 @@ def append(lattice: Lattice, other, axis: int) -> Lattice:
return lattice.copy(cell)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
def prepend(lattice: Lattice, other, axis: int) -> Lattice:
"""Prepends other `Lattice` to this grid along axis
Expand All @@ -330,7 +327,7 @@ def prepend(lattice: Lattice, other, axis: int) -> Lattice:
return other.append(lattice, axis)


@register_sisl_dispatch(module="sisl")
@register_sisl_dispatch(Lattice, module="sisl")
def center(lattice: Lattice, axis: Optional[int] = None) -> np.ndarray:
"""Returns center of the `Lattice`, possibly with respect to an axis"""
if axis is None:
Expand Down
Loading

0 comments on commit 201298e

Please sign in to comment.