Skip to content

Commit

Permalink
Add types hints for ingredients and experiment (#543)
Browse files Browse the repository at this point in the history
* Added types hints for ingredients and experiment.

* Corrected the type. Iterable -> Sequence.

* Used Pathlike.

* Revert "Used Pathlike."

This reverts commit 20162ac.
  • Loading branch information
gabrieldemarmiesse authored and JarnoRFB committed Jul 31, 2019
1 parent 7054628 commit b9df680
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
9 changes: 6 additions & 3 deletions sacred/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os.path
import sys
from collections import OrderedDict
from typing import Sequence, Optional

from docopt import docopt, printable_usage

Expand All @@ -19,7 +20,7 @@
from sacred.ingredient import Ingredient
from sacred.initialize import create_run
from sacred.utils import print_filtered_stacktrace, ensure_wellformed_argv, \
SacredError, format_sacred_error
SacredError, format_sacred_error, PathType

__all__ = ('Experiment',)

Expand All @@ -35,8 +36,10 @@ class Experiment(Ingredient):
things in any experiment-file.
"""

def __init__(self, name=None, ingredients=(), interactive=False,
base_dir=None):
def __init__(self, name: Optional[str] = None,
ingredients: Sequence[Ingredient] = (),
interactive: bool = False,
base_dir: Optional[PathType] = None):
"""
Create a new experiment with the given name and optional ingredients.
Expand Down
9 changes: 7 additions & 2 deletions sacred/ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import inspect
import os.path
from sacred.utils import PathType
from typing import Sequence, Optional

from collections import OrderedDict

Expand Down Expand Up @@ -47,8 +49,11 @@ class Ingredient:
Ingredients can themselves use ingredients.
"""

def __init__(self, path, ingredients=(), interactive=False,
_caller_globals=None, base_dir=None):
def __init__(self, path: PathType,
ingredients: Sequence['Ingredient'] = (),
interactive: bool = False,
_caller_globals: Optional[dict] = None,
base_dir: Optional[PathType] = None):
self.path = path
self.config_hooks = []
self.configurations = []
Expand Down
7 changes: 6 additions & 1 deletion sacred/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import traceback as tb
from functools import partial
from packaging import version
from typing import Union
from pathlib import Path

import wrapt

Expand All @@ -27,7 +29,8 @@
"convert_to_nested_dict", "convert_camel_case_to_snake_case",
"print_filtered_stacktrace", "is_subdir",
"optional_kwargs_decorator", "get_inheritors",
"apply_backspaces_and_linefeeds", "rel_path", "IntervalTimer"]
"apply_backspaces_and_linefeeds", "rel_path", "IntervalTimer",
"PathType"]

NO_LOGGER = logging.getLogger('ignore')
NO_LOGGER.disabled = 1
Expand All @@ -36,6 +39,8 @@

PYTHON_IDENTIFIER = re.compile("^[a-zA-Z_][_a-zA-Z0-9]*$")

PathType = Union[str, bytes, Path]


class ObserverError(Exception):
"""Error that an observer raises but that should not make the run fail."""
Expand Down

0 comments on commit b9df680

Please sign in to comment.