Skip to content

Commit 0a3677c

Browse files
author
SunDoge
committed
update typing
1 parent f810d58 commit 0a3677c

File tree

8 files changed

+37
-22
lines changed

8 files changed

+37
-22
lines changed

.editorconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ build/
55
dist/
66
*.egg-info/
77
.idea/
8-
*.pyc
8+
*.pyc
9+
.mypy_cache/

_test_args2.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import logging
2+
import pickle
13
from dataclasses import dataclass
24
from typing import Optional
5+
36
import typed_args._typed_args as tp
4-
import rich
5-
import pickle
6-
import logging
77

88
_logger = logging.getLogger(__name__)
99

@@ -29,7 +29,6 @@ class Args1(tp.TypedArgs):
2929
# rich.print(A.__dataclass_fields__)
3030

3131

32-
3332
args1 = Args1.from_args()
3433
print(args1.foo)
3534

tests/test_add_argument.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import typed_args as ta
2-
import pytest
31
from dataclasses import dataclass
42

3+
import pytest
4+
5+
import typed_args as ta
6+
57

68
def test_name_or_flags():
79
@dataclass
@@ -37,6 +39,3 @@ class Args(ta.TypedArgs):
3739

3840
args = Args.from_args(['--foo'])
3941
assert args == Args(foo=42)
40-
41-
42-

tests/test_list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from typed_args import TypedArgs, add_argument
21
from dataclasses import dataclass
32
from typing import List
3+
44
import typed_args as ta
5-
import pickle
5+
from typed_args import TypedArgs, add_argument
66

77

88
def test_list():

tests/test_pickle.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# from typed_args import TypedArgs, add_argument, dataclass
2-
import typed_args as ta
32
import pickle
4-
from typing import List, Optional
53
from dataclasses import dataclass
4+
from typing import List, Optional
5+
6+
import typed_args as ta
67

78

89
@dataclass()

typed_args/__init__.py

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

1010
from ._typed_args import TypedArgs, add_argument
1111

12-
__version__ = "0.5.1"
12+
__version__ = "0.5.2"
13+
14+
__all__ = [
15+
'TypedArgs', 'add_argument'
16+
]

typed_args/_typed_args.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import logging
1212
from argparse import Action, ArgumentParser, Namespace
1313
from collections.abc import Container
14-
from dataclasses import Field, dataclass, field
14+
from dataclasses import Field, dataclass
1515
from enum import Enum
1616
from typing import (Any, Callable, Dict, List, Optional, Sequence, Tuple, Type,
1717
TypeVar, Union)
@@ -41,10 +41,11 @@ def from_args(
4141

4242
cls._add_arguments(parser, prefix='')
4343

44-
args = parser.parse_args(args=args, namespace=namespace)
44+
parsed_args: Namespace = parser.parse_args(
45+
args=args, namespace=namespace)
4546

4647
typed_args = cls()
47-
typed_args._assign(args, prefix='')
48+
typed_args._assign(parsed_args, prefix='')
4849

4950
return typed_args
5051

@@ -60,10 +61,11 @@ def from_known_args(
6061

6162
cls._add_arguments(parser, prefix='')
6263

63-
args, unknown = parser.parse_known_args(args=args, namespace=namespace)
64+
parsed_args, unknown = parser.parse_known_args(
65+
args=args, namespace=namespace)
6466

6567
typed_args = cls()
66-
typed_args._assign(args, prefix='')
68+
typed_args._assign(parsed_args, prefix='')
6769

6870
return typed_args, unknown
6971

@@ -140,7 +142,7 @@ def add_argument(
140142
required: bool = None,
141143
help: str = None,
142144
metavar: str = None,
143-
) -> Field:
145+
) -> Any:
144146
kwargs = locals()
145147
args = kwargs.pop('flags')
146148

@@ -172,7 +174,11 @@ def default_factory(): return default
172174
default_factory = dataclasses.MISSING
173175

174176
# _logger.debug('metadata: %s', metadata)
175-
return field(default=field_default, default_factory=default_factory, metadata=metadata)
177+
return dataclasses.field(
178+
default=field_default,
179+
default_factory=default_factory,
180+
metadata=metadata
181+
)
176182

177183

178184
def add_parser():

0 commit comments

Comments
 (0)