Skip to content

Commit

Permalink
Merge pull request #7 from johnomotani/doc-from-factory
Browse files Browse the repository at this point in the history
Provide 'doc' property of OptionsFactory
  • Loading branch information
johnomotani authored May 3, 2020
2 parents 036a848 + cf7cf41 commit 0003550
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
18 changes: 12 additions & 6 deletions optionsfactory/optionsfactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ def defaults(self):
"""
return deepcopy(self.__defaults)

@property
def doc(self):
"""Get the documentation for the options defined for this OptionsFactory
"""
return {key: value.doc for key, value in self.__defaults.items()}

def add(self, **kwargs):
"""Create a more specific version of the factory with extra options. For example,
may be useful for a subclass like
Expand Down Expand Up @@ -183,7 +190,9 @@ def __create_mutable(self, values=None, parent=None):
del values[key]

# Return new MutableOptions instance
return OptionsFactory.MutableOptions(values, self.__defaults, parent=parent)
return OptionsFactory.MutableOptions(
values, self.__defaults, self.doc, parent=parent
)

def __create_immutable(self, values=None):
# Create MutableOptions instance: use to check the values and evaluate defaults
Expand All @@ -198,7 +207,7 @@ class MutableOptions:
"""

def __init__(self, data, defaults, parent=None):
def __init__(self, data, defaults, doc, parent=None):
self.__defaults = {
key: value if not isinstance(value, OptionsFactory) else None
for key, value in defaults.items()
Expand Down Expand Up @@ -226,10 +235,7 @@ def __init__(self, data, defaults, parent=None):
for key, value in data.items():
self.__data[key] = _checked(value, meta=self.__defaults[key], name=key)

self.__doc = {
key: value.doc if value is not None else self.__data[key].doc
for key, value in self.__defaults.items()
}
self.__doc = doc

@property
def doc(self):
Expand Down
9 changes: 9 additions & 0 deletions optionsfactory/tests/test_mutableoptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ def test_defaults(self):
assert opts.g == 11
assert opts.h == 3

assert factory.doc["a"] is None
assert factory.doc["b"] is None
assert factory.doc["c"] is None
assert factory.doc["d"] is None
assert factory.doc["e"] is None
assert factory.doc["f"] == "option f"
assert factory.doc["g"] == "option g"
assert factory.doc["h"] == "option h"

def test_initialise(self):
factory = MutableOptionsFactory(
a=1,
Expand Down
9 changes: 9 additions & 0 deletions optionsfactory/tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ def test_defaults(self):
]
)

assert factory.doc["a"] is None
assert factory.doc["b"] is None
assert factory.doc["c"] is None
assert factory.doc["d"] is None
assert factory.doc["e"] is None
assert factory.doc["f"] == "option f"
assert factory.doc["g"] == "option g"
assert factory.doc["h"] == "option h"

def test_initialise(self):
factory = OptionsFactory(
a=1,
Expand Down

0 comments on commit 0003550

Please sign in to comment.