Skip to content

Commit e710811

Browse files
author
SunDoge
committed
release 0.5.0
1 parent 704867a commit e710811

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ python:
55
- "3.6"
66
- "3.7"
77
- "3.8"
8+
- "3.9"
89
script:
910
- pip install dataclasses
1011
- pytest

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.a3"
3+
version = "0.5.0"
44
description = "Parse command line arguments by defining dataclasses"
55
authors = ["SunDoge <384813529@qq.com>"]
66
readme = "README.md"

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.a3"
12+
__version__ = "0.5.0"

typed_args/_typed_args.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import dataclasses
1010
import inspect
1111
import logging
12-
from argparse import ArgumentParser, Namespace
12+
from argparse import Action, ArgumentParser, Namespace
13+
from collections.abc import Container
1314
from dataclasses import Field, dataclass, field
1415
from enum import Enum
15-
from typing import Dict, List, Optional, Sequence, Tuple, Type, TypeVar
16+
from typing import (Any, Callable, Dict, List, Optional, Sequence, Tuple, Type,
17+
TypeVar, Union)
1618

1719
_logger = logging.getLogger(__name__)
1820

@@ -129,18 +131,23 @@ def _assign(self, args: Namespace, prefix: str = ''):
129131

130132
def add_argument(
131133
*flags: str,
132-
action=None,
133-
nargs=None,
134-
const=None,
135-
default=None,
136-
type=None,
137-
choices=None,
138-
required=None,
139-
help=None,
140-
metavar=None,
134+
action: Union[str, Type[Action]] = None,
135+
nargs: Union[str, int] = None,
136+
const: Any = None,
137+
default: Any = None,
138+
type: Callable[[str], Any] = None,
139+
choices: Container = None, # to support `is in`
140+
required: bool = None,
141+
help: str = None,
142+
metavar: str = None,
141143
) -> Field:
142144
kwargs = locals()
143145
args = kwargs.pop('flags')
146+
147+
# The parser.add_argument only accept args with value
148+
# We use default value None to filter out the unused args
149+
# add_argument(*args, **kwargs) 有复杂的规则
150+
# 为了避免手动处理这些规则,采用过滤 None 的方式来过滤没有使用的参数
144151
kwargs = {k: v for k, v in kwargs.items() if v is not None}
145152
return _add_argument(*args, **kwargs)
146153

0 commit comments

Comments
 (0)