Skip to content

Commit

Permalink
Fixed a bug where required_args are set from a yaml file, changed tes…
Browse files Browse the repository at this point in the history
…ts accordingly
  • Loading branch information
eladrich committed Dec 30, 2021
1 parent 0df32d9 commit 91ad077
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
3 changes: 1 addition & 2 deletions pyrallis/parsers/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
)

Expand Down
5 changes: 3 additions & 2 deletions pyrallis/wrappers/field_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("")


Expand Down Expand Up @@ -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("")


Expand Down
3 changes: 2 additions & 1 deletion tests/test_choice.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from builtins import TypeError
from dataclasses import dataclass, field
from enum import Enum

Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_default_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("")


Expand Down
4 changes: 2 additions & 2 deletions tests/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]")
Expand Down

0 comments on commit 91ad077

Please sign in to comment.