Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 0f2e978

Browse files
committed
base: Add field for config field descriptions
Signed-off-by: John Andersen <[email protected]>
1 parent 00b693a commit 0f2e978

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

dffml/base.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import contextlib
99
import dataclasses
1010
from argparse import ArgumentParser
11-
from typing import Dict, Any, Tuple, NamedTuple, Type
11+
from typing import Dict, Any, Tuple, NamedTuple, Type, Optional
1212

1313
try:
1414
from typing import get_origin, get_args
@@ -131,8 +131,8 @@ def mkarg(field):
131131
elif get_origin(field.type) is list:
132132
arg["type"] = get_args(field.type)[0]
133133
arg["nargs"] = "+"
134-
if "help" in field.metadata:
135-
arg["help"] = field.metadata["help"]
134+
if "description" in field.metadata:
135+
arg["help"] = field.metadata["description"]
136136
return arg
137137

138138

@@ -189,11 +189,23 @@ def _fromdict(cls, **kwargs):
189189
return cls(**kwargs)
190190

191191

192+
def field(description: str, *args, metadata: Optional[dict] = None, **kwargs):
193+
"""
194+
Creates an instance of :py:func:`dataclasses.field`. The first argument,
195+
``description`` is the description of the field, and will be set as the
196+
``"description"`` key in the metadata ``dict``.
197+
"""
198+
if not metadata:
199+
metadata = {}
200+
metadata["description"] = description
201+
return dataclasses.field(*args, metadata=metadata, **kwargs)
202+
203+
192204
def config(cls):
193205
"""
194206
Decorator to create a dataclass
195207
"""
196-
datacls = dataclasses.dataclass(eq=True, frozen=True)(cls)
208+
datacls = dataclasses.dataclass(eq=True, init=True, frozen=True)(cls)
197209
datacls._fromdict = classmethod(_fromdict)
198210
datacls._replace = lambda self, *args, **kwargs: dataclasses.replace(
199211
self, *args, **kwargs

tests/test_base.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import unittest
2-
from dataclasses import field
32
from typing import List
43

5-
from dffml.base import BaseDataFlowFacilitatorObject, config, list_action
4+
from dffml.base import (
5+
BaseDataFlowFacilitatorObject,
6+
config,
7+
field,
8+
list_action,
9+
)
610
from dffml.feature.feature import DefFeature, Feature, Features
711
from dffml.source.source import BaseSource
812
from dffml.source.csv import CSVSource
@@ -16,7 +20,7 @@
1620
class FakeTestingConfig:
1721
files: List[str]
1822
features: Features
19-
name: str = field(metadata={"help": "Name of FakeTesting"})
23+
name: str = field("Name of FakeTesting")
2024
label: str = "unlabeled"
2125
readonly: bool = False
2226
source: BaseSource = JSONSource

0 commit comments

Comments
 (0)