Skip to content

Commit 2a69ceb

Browse files
authored
Bugfix: fix optional positional arguments. (#345)
* Bugfix: fix optional and positional arguments. * Bugfix: fix optional and positional arguments. * typo * Use optional type
1 parent a70c4c7 commit 2a69ceb

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

simple_parsing/wrappers/field_wrapper.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ def get_arg_options(self) -> dict[str, Any]:
272272
_arg_options.pop("metavar", None)
273273

274274
elif utils.is_optional(self.type) or self.field.default is None:
275-
_arg_options["required"] = False
275+
if not self.field.metadata.get("positional"):
276+
_arg_options["required"] = False
276277

277278
if utils.is_optional(self.type):
278279
type_arguments = utils.get_args(self.type)

test/test_optional.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

6+
import simple_parsing
67
from simple_parsing import ArgumentParser
78

89
from .testutils import TestSetup
@@ -142,3 +143,14 @@ class Bob(TestSetup):
142143
# actual = SomeDataclass.setup(f"--some_attribute {passed_value}")
143144
# assert actual.some_attribute == expected_value
144145
# assert isinstance(actual.some_attribute, some_type)
146+
147+
148+
def test_optional_positional():
149+
"""Test a optional positional argument."""
150+
151+
@dataclass
152+
class Foo(TestSetup):
153+
a: Optional[int] = simple_parsing.field(positional=True, default=None)
154+
155+
assert Foo.setup("") == Foo(a=None)
156+
assert Foo.setup("1") == Foo(a=1)

0 commit comments

Comments
 (0)