Skip to content

Commit f810d58

Browse files
author
SunDoge
committed
fix default argument
1 parent e710811 commit f810d58

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.5.1]
11+
12+
- Fix default argument. `add_argument(default=[])` now init the correct `[]` for dataclass.
13+
1014
## [0.5.0]
1115

1216
- Function `add_argument` accepts `type` now and we don't check the type annotations in dataclass fields. This make `typed-args` less strict but more easy to use.
@@ -50,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5054
- Replace attributes by name
5155

5256
[unreleased]: https://github.com/SunDoge/typed-args
57+
[0.5.1]: https://github.com/SunDoge/typed-args/tree/v0.5.1
5358
[0.5.0]: https://github.com/SunDoge/typed-args/tree/v0.5.0
5459
[0.4.2]: https://github.com/SunDoge/typed-args/tree/v0.4.2
5560
[0.4.1]: https://github.com/SunDoge/typed-args/tree/v0.4.1

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "typed-args"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
description = "Parse command line arguments by defining dataclasses"
55
authors = ["SunDoge <384813529@qq.com>"]
66
readme = "README.md"

tests/test_list.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ def test_default_list():
2727
class Args(ta.TypedArgs):
2828
foo: int = ta.add_argument('--foo', type=int, default=42)
2929
bar: List[int] = ta.add_argument(nargs='*', default=[1, 2, 3])
30+
config: List[str] = ta.add_argument(
31+
'--config', default=[], type=str, action='append')
3032

3133
args = Args.from_args([])
3234

3335
assert args.bar == [1, 2, 3]
36+
assert args.config == []
37+
38+
args1 = Args()
39+
assert args1.foo == 42
40+
assert args1.bar == [1, 2, 3]
41+
assert args1.config == []

typed_args/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
from ._typed_args import TypedArgs, add_argument
1111

12-
__version__ = "0.5.0"
12+
__version__ = "0.5.1"

typed_args/_typed_args.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ def _add_argument(*args, **kwargs) -> Field:
165165
'mutable object cannot be dataclass default attribute, make default_factory'
166166
)
167167

168+
field_default = dataclasses.MISSING
168169
def default_factory(): return default
169-
170-
default = dataclasses.MISSING
171170
else:
171+
field_default = default
172172
default_factory = dataclasses.MISSING
173173

174174
# _logger.debug('metadata: %s', metadata)
175-
return field(default=default, default_factory=default_factory, metadata=metadata)
175+
return field(default=field_default, default_factory=default_factory, metadata=metadata)
176176

177177

178178
def add_parser():

0 commit comments

Comments
 (0)