Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type annotations #244

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
50143e5
Add type-annotations to Runner
basnijholt Oct 12, 2022
6cf01d8
Add type-hints to adaptive/_version.py
basnijholt Oct 12, 2022
1786f80
Add type-hints to adaptive/learner/__init__.py
basnijholt Oct 12, 2022
9490093
Add type-hints to adaptive/learner/balancing_learner.py
basnijholt Oct 12, 2022
6b3209f
Add type-hints to adaptive/learner/base_learner.py
basnijholt Oct 12, 2022
8363aa6
Add type-hints to adaptive/learner/data_saver.py
basnijholt Oct 12, 2022
2b0497f
Add type-hints to adaptive/learner/integrator_coeffs.py
basnijholt Oct 12, 2022
c2b6cf3
Add type-hints to adaptive/learner/integrator_learner.py
basnijholt Oct 12, 2022
3293f60
Add type-hints to adaptive/learner/learner2D.py
basnijholt Oct 12, 2022
2b5e0d1
Add type-hints to adaptive/learner/learnerND.py
basnijholt Oct 12, 2022
08ddfb2
Add type-hints to adaptive/learner/sequence_learner.py
basnijholt Oct 12, 2022
5d8110f
Add type-hints to adaptive/learner/skopt_learner.py
basnijholt Oct 12, 2022
7fd0588
Add type-hints to adaptive/learner/triangulation.py
basnijholt Oct 12, 2022
2e0efc1
Add type-hints to adaptive/notebook_integration.py
basnijholt Oct 12, 2022
96e186f
Add type-hints to adaptive/tests/algorithm_4.py
basnijholt Oct 12, 2022
ec3649a
Add type-hints to adaptive/tests/test_average_learner1d.py
basnijholt Oct 12, 2022
54265bb
Add type-hints to adaptive/tests/test_learner1d.py
basnijholt Oct 12, 2022
9e52ecb
Add type-hints to adaptive/tests/test_learners.py
basnijholt Oct 12, 2022
b4ba66e
Add type-hints to adaptive/types.py
basnijholt Oct 12, 2022
3928c3d
Add type-hints to adaptive/utils.py
basnijholt Oct 12, 2022
95cc29b
Add type-hints to setup.py
basnijholt Oct 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions adaptive/_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# This file is part of 'miniver': https://github.com/jbweston/miniver
#
from __future__ import annotations

import os
import subprocess
from collections import namedtuple
Expand All @@ -10,7 +12,7 @@
Version = namedtuple("Version", ("release", "dev", "labels"))

# No public API
__all__ = []
__all__: list[str] = []

package_root = os.path.dirname(os.path.realpath(__file__))
package_name = os.path.basename(package_root)
Expand All @@ -26,22 +28,21 @@
STATIC_VERSION_FILE = "_static_version.py"


def get_version(version_file=STATIC_VERSION_FILE):
def get_version(version_file: str = STATIC_VERSION_FILE) -> str:
version_info = get_static_version_info(version_file)
version = version_info["version"]
if version == "__use_git__":
if version_info["version"] == "__use_git__":
version = get_version_from_git()
if not version:
version = get_version_from_git_archive(version_info)
if not version:
version = Version("unknown", None, None)
return pep440_format(version)
else:
return version
return version_info["version"]


def get_static_version_info(version_file=STATIC_VERSION_FILE):
version_info = {}
def get_static_version_info(version_file: str = STATIC_VERSION_FILE) -> dict[str, str]:
version_info: dict[str, str] = {}
with open(os.path.join(package_root, version_file), "rb") as f:
exec(f.read(), {}, version_info)
return version_info
Expand All @@ -51,7 +52,7 @@ def version_is_from_git(version_file=STATIC_VERSION_FILE):
return get_static_version_info(version_file)["version"] == "__use_git__"


def pep440_format(version_info):
def pep440_format(version_info: Version) -> str:
release, dev, labels = version_info

version_parts = [release]
Expand All @@ -68,7 +69,7 @@ def pep440_format(version_info):
return "".join(version_parts)


def get_version_from_git():
def get_version_from_git() -> Version:
try:
p = subprocess.Popen(
["git", "rev-parse", "--show-toplevel"],
Expand All @@ -77,32 +78,32 @@ def get_version_from_git():
stderr=subprocess.PIPE,
)
except OSError:
return
return None
if p.wait() != 0:
return
return None
if not os.path.samefile(p.communicate()[0].decode().rstrip("\n"), distr_root):
# The top-level directory of the current Git repository is not the same
# as the root directory of the distribution: do not extract the
# version from Git.
return
return None

# git describe --first-parent does not take into account tags from branches
# that were merged-in. The '--long' flag gets us the 'dev' version and
# git hash, '--always' returns the git hash even if there are no tags.
for opts in [["--first-parent"], []]:
try:
p = subprocess.Popen(
["git", "describe", "--long", "--always", "--tags"] + opts,
["git", "describe", "--long", "--always", "--tags"] + opts, # type: ignore
cwd=distr_root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except OSError:
return
return None
if p.wait() == 0:
break
else:
return
return None

description = (
p.communicate()[0]
Expand Down Expand Up @@ -142,7 +143,7 @@ def get_version_from_git():
# Currently we can only tell the tag the current commit is
# pointing to, or its hash (with no version info)
# if it is not tagged.
def get_version_from_git_archive(version_info):
def get_version_from_git_archive(version_info) -> Version:
try:
refnames = version_info["refnames"]
git_hash = version_info["git_hash"]
Expand All @@ -165,7 +166,7 @@ def get_version_from_git_archive(version_info):
return Version("unknown", dev=None, labels=[f"g{git_hash}"])


__version__ = get_version()
__version__: str = get_version()


# The following section defines a module global 'cmdclass',
Expand Down
2 changes: 2 additions & 0 deletions adaptive/learner/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from contextlib import suppress

from adaptive.learner.average_learner import AverageLearner
Expand Down
Loading