Skip to content

Commit 23762b9

Browse files
authored
Merge pull request #167 from Daraan/config_files-as-sequence
Change config_files type to Iterable and add tests
2 parents 41b1714 + 860fa3e commit 23762b9

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ args.from_dict({
585585
Note: As with `load`, all required arguments must be present in the dictionary if not already set in the Tap object. All values in the provided dictionary will overwrite values currently in the Tap object.
586586

587587
### Loading from configuration files
588-
Configuration files can be loaded along with arguments with the optional flag `config_files: List[str]`. Arguments passed in from the command line overwrite arguments from the configuration files. Arguments in configuration files that appear later in the list overwrite the arguments in previous configuration files.
588+
Configuration files can be loaded along with arguments with the optional flag `config_files: Iterable[str]`. Arguments passed in from the command line overwrite arguments from the configuration files. Arguments in configuration files that appear later in the list overwrite the arguments in previous configuration files.
589589

590590
For example, if you have the config file `my_config.txt`
591591
```

src/tap/tap.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pprint import pformat
99
from shlex import quote, split
1010
from types import MethodType, UnionType
11-
from typing import Any, Callable, List, Optional, Sequence, Set, Tuple, TypeVar, Union, get_type_hints
11+
from typing import Any, Callable, Iterable, List, Optional, Sequence, Set, Tuple, TypeVar, Union, get_type_hints
1212
from typing_inspect import is_literal_type
1313

1414
from tap.utils import (
@@ -50,7 +50,7 @@ def __init__(
5050
*args,
5151
underscores_to_dashes: bool = False,
5252
explicit_bool: bool = False,
53-
config_files: Optional[list[PathLike]] = None,
53+
config_files: Optional[Iterable[PathLike]] = None,
5454
**kwargs,
5555
) -> None:
5656
"""Initializes the Tap instance.
@@ -60,7 +60,7 @@ def __init__(
6060
:param explicit_bool: Booleans can be specified on the command line as "--arg True" or "--arg False"
6161
rather than "--arg". Additionally, booleans can be specified by prefixes of True and False
6262
with any capitalization as well as 1 or 0.
63-
:param config_files: A list of paths to configuration files containing the command line arguments
63+
:param config_files: An iterable of paths to configuration files containing the command line arguments
6464
(e.g., '--arg1 a1 --arg2 a2'). Arguments passed in from the command line
6565
overwrite arguments from the configuration files. Arguments in configuration files
6666
that appear later in the list overwrite the arguments in previous configuration files.
@@ -686,13 +686,13 @@ def load(
686686

687687
return self
688688

689-
def _load_from_config_files(self, config_files: Optional[list[str]]) -> list[str]:
690-
"""Loads arguments from a list of configuration files containing command line arguments.
689+
def _load_from_config_files(self, config_files: Optional[Iterable[PathLike]]) -> list[str]:
690+
"""Loads arguments from an iterable of configuration files containing command line arguments.
691691
692-
:param config_files: A list of paths to configuration files containing the command line arguments
692+
:param config_files: An iterable of paths to configuration files containing the command line arguments
693693
(e.g., '--arg1 a1 --arg2 a2'). Arguments passed in from the command line
694694
overwrite arguments from the configuration files. Arguments in configuration files
695-
that appear later in the list overwrite the arguments in previous configuration files.
695+
that appear later in the iterable overwrite the arguments in previous configuration files.
696696
:return: A list of the contents of each config file in order of increasing precedence (highest last).
697697
"""
698698
args_from_config = []

tests/test_load_config_files.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import sys
3-
from tempfile import TemporaryDirectory
3+
from tempfile import NamedTemporaryFile, TemporaryDirectory
44
import unittest
55
from unittest import TestCase
66

@@ -103,6 +103,25 @@ class MultipleTap(Tap):
103103
self.assertEqual(args.a, 1)
104104
self.assertEqual(args.b, "two")
105105

106+
def test_config_as_iterator(self) -> None:
107+
class MultipleTap(Tap):
108+
a: int
109+
b: str = "b"
110+
111+
with TemporaryDirectory() as temp_dir:
112+
fname1, fname2 = os.path.join(temp_dir, "config1.txt"), os.path.join(temp_dir, "config2.txt")
113+
114+
with open(fname1, "w") as f1, open(fname2, "w") as f2:
115+
f1.write("--b two")
116+
f2.write("--a 1")
117+
118+
config_iter = (cf for cf in [fname1, fname2])
119+
120+
args = MultipleTap(config_files=config_iter).parse_args([])
121+
122+
self.assertEqual(args.a, 1)
123+
self.assertEqual(args.b, "two")
124+
106125
def test_multiple_configs_overwriting(self) -> None:
107126
class MultipleOverwritingTap(Tap):
108127
a: int

0 commit comments

Comments
 (0)