-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1054 from openfisca/typing/commons
[1/17] Improve commons module typing
- Loading branch information
Showing
11 changed files
with
262 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
.venv | ||
.project | ||
.spyderproject | ||
.pydevproject | ||
.vscode | ||
.settings/ | ||
.vscode/ | ||
build/ | ||
dist/ | ||
doc/ | ||
*.egg-info | ||
*.mo | ||
*.pyc | ||
*~ | ||
/cover | ||
/.coverage | ||
/tags | ||
.tags* | ||
.coverage | ||
.mypy_cache | ||
.noseids | ||
.project | ||
.pydevproject | ||
.pytest_cache | ||
.mypy_cache | ||
.settings | ||
.spyderproject | ||
.tags* | ||
.venv | ||
.vscode | ||
.vscode | ||
build | ||
cover | ||
dist | ||
doc | ||
performance.json | ||
tags |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Data types and protocols used by OpenFisca Core. | ||
The type definitions included in this sub-package are intented for | ||
contributors, to help them better understand and document contracts | ||
and expected behaviours. | ||
Official Public API: | ||
* ``ArrayLike`` | ||
* :attr:`.ArrayType` | ||
Note: | ||
How imports are being used today:: | ||
from openfisca_core.types import * # Bad | ||
from openfisca_core.types.data_types.arrays import ArrayLike # Bad | ||
The previous examples provoke cyclic dependency problems, that prevents us | ||
from modularizing the different components of the library, so as to make | ||
them easier to test and to maintain. | ||
How could them be used after the next major release:: | ||
from openfisca_core.types import ArrayLike | ||
ArrayLike # Good: import types as publicly exposed | ||
.. seealso:: `PEP8#Imports`_ and `OpenFisca's Styleguide`_. | ||
.. _PEP8#Imports: | ||
https://www.python.org/dev/peps/pep-0008/#imports | ||
.. _OpenFisca's Styleguide: | ||
https://github.com/openfisca/openfisca-core/blob/master/STYLEGUIDE.md | ||
""" | ||
|
||
# Official Public API | ||
|
||
from .data_types import ( # noqa: F401 | ||
ArrayLike, | ||
ArrayType, | ||
) | ||
|
||
__all__ = ["ArrayLike", "ArrayType"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .arrays import ArrayLike, ArrayType # noqa: F401 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Sequence, TypeVar, Union | ||
|
||
from nptyping import types, NDArray as ArrayType | ||
|
||
import numpy | ||
|
||
T = TypeVar("T", bool, bytes, float, int, object, str) | ||
|
||
types._ndarray_meta._Type = Union[type, numpy.dtype, TypeVar] | ||
|
||
ArrayLike = Union[ArrayType[T], Sequence[T]] | ||
""":obj:`typing.Generic`: Type of any castable to :class:`numpy.ndarray`. | ||
These include any :obj:`numpy.ndarray` and sequences (like | ||
:obj:`list`, :obj:`tuple`, and so on). | ||
Examples: | ||
>>> ArrayLike[float] | ||
typing.Union[numpy.ndarray, typing.Sequence[float]] | ||
>>> ArrayLike[str] | ||
typing.Union[numpy.ndarray, typing.Sequence[str]] | ||
Note: | ||
It is possible since numpy version 1.21 to specify the type of an | ||
array, thanks to `numpy.typing.NDArray`_:: | ||
from numpy.typing import NDArray | ||
NDArray[numpy.float64] | ||
`mypy`_ provides `duck type compatibility`_, so an :obj:`int` is | ||
considered to be valid whenever a :obj:`float` is expected. | ||
Todo: | ||
* Refactor once numpy version >= 1.21 is used. | ||
.. versionadded:: 35.5.0 | ||
.. versionchanged:: 35.6.0 | ||
Moved to :mod:`.types` | ||
.. _mypy: | ||
https://mypy.readthedocs.io/en/stable/ | ||
.. _duck type compatibility: | ||
https://mypy.readthedocs.io/en/stable/duck_type_compatibility.html | ||
.. _numpy.typing.NDArray: | ||
https://numpy.org/doc/stable/reference/typing.html#numpy.typing.NDArray | ||
""" |
Oops, something went wrong.