Skip to content

Commit

Permalink
break out types.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jGaboardi committed Jul 13, 2024
1 parent e886440 commit 9dd5b97
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 52 deletions.
66 changes: 14 additions & 52 deletions fastpair/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,26 @@
# Copyright (c) 2002-2015, David Eppstein
# Licensed under the MIT Licence (http://opensource.org/licenses/MIT).

import sys

from collections import defaultdict
from collections.abc import Callable, Generator, Iterable
from itertools import combinations, cycle
from typing import Annotated, TypeAlias, TypedDict, TypeVar

################################################################
# See GH#73 -- remove once Python 3.11 is minimum
if sys.version_info.minor < 11:
from typing_extensions import Self
else:
from typing import Self
################################################################


import numpy
import scipy.spatial.distance as dist

__all__ = ["FastPair", "dist"]


# point typing ---------------------------------------------------------------------
_Numeric = TypeVar("_Numeric", float, int, complex, numpy.number)
NumericCoord: TypeAlias = Annotated[_Numeric, "Any numeric value."]

_PointCoords = TypeVar("_PointCoords", bound=tuple[NumericCoord])
PointCoords = Annotated[_PointCoords, "Must be a tuple of at least 2 elements."]

_PointsInput = TypeVar("_PointsInput", bound=Iterable[PointCoords])
PointsInput = Annotated[_PointsInput, "Input for ``.build()``."]


# distance typing ------------------------------------------------------------------
Dist: TypeAlias = Annotated[_Numeric, "Any numeric value."]
from .types import (
AllNeighs,
Callable,
ClosestPair,
Dist,
DistNeighDict, # noqa: F401
DistNeighTuple,
Iterable,
PointCoords,
PointsInput,
Self,
)

_DistNeighTuple = TypeVar("_DistNeighTuple", bound=tuple[Dist, PointCoords])
DistNeighTuple = Annotated[_DistNeighTuple, "Returned from ``.__getitem__()``."]

_AllNeighs = Generator[DistNeighTuple, None, None]
AllNeighs = Annotated[_AllNeighs, "Returned from ``.sdist()``."]


# pair typing ----------------------------------------------------------------------
class DistNeighDict(TypedDict):
"""The values of the ``FastPair.neighbors`` dictionary.
Also potentially returned from ``._update_point()``.
See GH#69.
"""

dist: Dist
neigh: PointCoords


_NeighCoords = tuple[PointCoords, PointCoords]
NeighCoords = Annotated[_NeighCoords, "2 instances of ``PointCoords``."]

_ClosestPair = TypeVar("_ClosestPair", bound=tuple[Dist, NeighCoords])
ClosestPair = Annotated[_ClosestPair, "Returned from ``.closest_pair()``."]
__all__ = ["FastPair", "dist"]


class AttrDict(dict):
Expand Down
71 changes: 71 additions & 0 deletions fastpair/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Copyright (c) 2016, Carson J. Q. Farmer <carson.farmer@gmail.com>
# Copyright (c) 2002-2015, David Eppstein
# Licensed under the MIT Licence (http://opensource.org/licenses/MIT).

import sys
from collections.abc import Callable, Generator, Iterable
from typing import Annotated, TypeAlias, TypedDict, TypeVar

################################################################
# See GH#73 -- remove once Python 3.11 is minimum
if sys.version_info.minor < 11:
from typing_extensions import Self
else:
from typing import Self
################################################################

import numpy

__all__ = [
"Callable",
"Iterable",
"Self",
"NumericCoord",
"PointCoords",
"PointsInput",
"Dist",
"DistNeighTuple",
"AllNeighs",
"DistNeighDict",
"NeighCoords",
"ClosestPair",
]


# point typing ---------------------------------------------------------------------
_Numeric = TypeVar("_Numeric", float, int, complex, numpy.number)
_NumericAnnotation = Annotated[_Numeric, "Any numeric value."]
NumericCoord: TypeAlias = _NumericAnnotation

_PointCoords = TypeVar("_PointCoords", bound=tuple[NumericCoord])
PointCoords = Annotated[_PointCoords, "A tuple of at least 2 numeric elements."]

_PointsInput = TypeVar("_PointsInput", bound=Iterable[PointCoords])
PointsInput = Annotated[_PointsInput, "Input for ``.build()``."]


# distance typing ------------------------------------------------------------------
Dist: TypeAlias = _NumericAnnotation

_DistNeighTuple = TypeVar("_DistNeighTuple", bound=tuple[Dist, PointCoords])
DistNeighTuple = Annotated[_DistNeighTuple, "Returned from ``.__getitem__()``."]

_AllNeighs = Generator[DistNeighTuple, None, None]
AllNeighs = Annotated[_AllNeighs, "Returned from ``.sdist()``."]


# pair typing ----------------------------------------------------------------------
class DistNeighDict(TypedDict):
"""The values of the ``FastPair.neighbors`` dictionary. Also potentially
returned from ``._update_point()``. See GH#69.
"""

dist: Dist
neigh: PointCoords


_NeighCoords = tuple[PointCoords, PointCoords]
NeighCoords = Annotated[_NeighCoords, "2 instances of ``PointCoords``."]

_ClosestPair = TypeVar("_ClosestPair", bound=tuple[Dist, NeighCoords])
ClosestPair = Annotated[_ClosestPair, "Returned from ``.closest_pair()``."]

0 comments on commit 9dd5b97

Please sign in to comment.