Skip to content

Commit

Permalink
Use typing.TypeAlias (#367)
Browse files Browse the repository at this point in the history
* Use typing.TypeAlias

* Optionally install typing_extensions
  • Loading branch information
basnijholt authored Oct 11, 2022
1 parent adb08bd commit 50fae43
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
26 changes: 16 additions & 10 deletions adaptive/learner/learner1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
partial_function_from_dataframe,
)

try:
from typing import TypeAlias
except ImportError:
# Remove this when we drop support for Python 3.9
from typing_extensions import TypeAlias

try:
import pandas

Expand All @@ -33,21 +39,21 @@
# -- types --

# Commonly used types
Interval = Union[Tuple[float, float], Tuple[float, float, int]]
NeighborsType = Dict[float, List[Union[float, None]]]
Interval: TypeAlias = Union[Tuple[float, float], Tuple[float, float, int]]
NeighborsType: TypeAlias = Dict[float, List[Union[float, None]]]

# Types for loss_per_interval functions
NoneFloat = Union[Float, None]
NoneArray = Union[np.ndarray, None]
XsType0 = Tuple[Float, Float]
YsType0 = Union[Tuple[Float, Float], Tuple[np.ndarray, np.ndarray]]
XsType1 = Tuple[NoneFloat, NoneFloat, NoneFloat, NoneFloat]
YsType1 = Union[
NoneFloat: TypeAlias = Union[Float, None]
NoneArray: TypeAlias = Union[np.ndarray, None]
XsType0: TypeAlias = Tuple[Float, Float]
YsType0: TypeAlias = Union[Tuple[Float, Float], Tuple[np.ndarray, np.ndarray]]
XsType1: TypeAlias = Tuple[NoneFloat, NoneFloat, NoneFloat, NoneFloat]
YsType1: TypeAlias = Union[
Tuple[NoneFloat, NoneFloat, NoneFloat, NoneFloat],
Tuple[NoneArray, NoneArray, NoneArray, NoneArray],
]
XsTypeN = Tuple[NoneFloat, ...]
YsTypeN = Union[Tuple[NoneFloat, ...], Tuple[NoneArray, ...]]
XsTypeN: TypeAlias = Tuple[NoneFloat, ...]
YsTypeN: TypeAlias = Union[Tuple[NoneFloat, ...], Tuple[NoneArray, ...]]


__all__ = [
Expand Down
12 changes: 9 additions & 3 deletions adaptive/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import numpy as np

Float = Union[float, np.float_]
Int = Union[int, np.int_]
Real = Union[Float, Int]
try:
from typing import TypeAlias
except ImportError:
# Remove this when we drop support for Python 3.9
from typing_extensions import TypeAlias

Float: TypeAlias = Union[float, np.float_]
Int: TypeAlias = Union[int, np.int_]
Real: TypeAlias = Union[Float, Int]
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def get_version_and_cmdclass(package_name):
"cloudpickle",
"loky >= 2.9",
]
if sys.version_info < (3, 10):
install_requires.append("typing_extensions")

extras_require = {
"notebook": [
Expand Down Expand Up @@ -78,6 +80,7 @@ def get_version_and_cmdclass(package_name):
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
packages=find_packages("."),
install_requires=install_requires,
Expand Down

0 comments on commit 50fae43

Please sign in to comment.