diff --git a/pyrallis/parsers/decoding.py b/pyrallis/parsers/decoding.py index 24c41f9..0b829be 100644 --- a/pyrallis/parsers/decoding.py +++ b/pyrallis/parsers/decoding.py @@ -84,8 +84,7 @@ def decode_dataclass( try: instance = cls(**init_args) # type: ignore except TypeError as e: - # raise RuntimeError(f"Couldn't instantiate class {cls} using init args {init_args}.") - raise RuntimeError( + raise TypeError( f"Couldn't instantiate class {cls} using init args {init_args.keys()}: {e}" ) diff --git a/pyrallis/wrappers/field_wrapper.py b/pyrallis/wrappers/field_wrapper.py index 6a9063d..056d4e3 100644 --- a/pyrallis/wrappers/field_wrapper.py +++ b/pyrallis/wrappers/field_wrapper.py @@ -4,9 +4,9 @@ from logging import getLogger from typing import Any, Optional, List, Type, Dict, Set, Union, Tuple +from . import docstring from .wrapper import Wrapper from .. import utils -from . import docstring logger = getLogger(__name__) @@ -93,7 +93,8 @@ def get_arg_options(self) -> Dict[str, Any]: # 5. Return that dictionary. _arg_options: Dict[str, Any] = {} - _arg_options["required"] = self.required + _arg_options["required"] = False # Required arguments can also be set from yaml, + # so do not enforce with argparse _arg_options["dest"] = self.dest _arg_options["default"] = self.default diff --git a/tests/test_base.py b/tests/test_base.py index ea94002..d53df2e 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -25,7 +25,7 @@ def test_not_passing_required_argument_raises_error(simple_attribute): class SomeDataclass(TestSetup): some_attribute: some_type # type: ignore - with raises_missing_required_arg(): + with raises(TypeError): _ = SomeDataclass.setup("") @@ -79,7 +79,7 @@ class SomeClass(TestSetup): a: some_type # type: ignore """some docstring for attribute 'a'""" - with raises(SystemExit): + with raises(TypeError): _ = SomeClass.setup("") diff --git a/tests/test_choice.py b/tests/test_choice.py index 168beaf..eb35dbe 100644 --- a/tests/test_choice.py +++ b/tests/test_choice.py @@ -1,3 +1,4 @@ +from builtins import TypeError from dataclasses import dataclass, field from enum import Enum @@ -32,7 +33,7 @@ def test_passing_enum_to_choice_no_default_makes_required_arg(): class Something(TestSetup): favorite_color: Color = field() - with raises(SystemExit): + with raises(TypeError): s = Something.setup("") s = Something.setup("--favorite_color blue") diff --git a/tests/test_default_args.py b/tests/test_default_args.py index 8aa98ff..be7c517 100644 --- a/tests/test_default_args.py +++ b/tests/test_default_args.py @@ -3,7 +3,7 @@ from .testutils import * -def test_no_default_argument(simple_attribute, silent): +def test_no_default_argument(simple_attribute): some_type, passed_value, expected_value = simple_attribute @dataclass @@ -15,7 +15,7 @@ class SomeClass: cfg = parser.parse_args(shlex.split(f"--a {passed_value}")) assert cfg == SomeClass(a=expected_value) - with raises_missing_required_arg(): + with raises(TypeError): parser.parse_args("") diff --git a/tests/test_lists.py b/tests/test_lists.py index 6a377dc..7a08df7 100644 --- a/tests/test_lists.py +++ b/tests/test_lists.py @@ -40,10 +40,10 @@ def test_single_element_list(ContainerClass): def test_required_attributes_works(ContainerClass): - with raises_missing_required_arg(): + with raises(TypeError): ContainerClass.setup("--b [4]") - with raises_missing_required_arg(): + with raises(TypeError): ContainerClass.setup("--a [4]") container = ContainerClass.setup("--a [4] --b [5]")