Skip to content

Commit

Permalink
Make codecov happy
Browse files Browse the repository at this point in the history
  • Loading branch information
TedBrookings committed Nov 28, 2023
1 parent a1df504 commit 569af29
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignore:
- "test_*.py" # don't compute coverage of tests
4 changes: 2 additions & 2 deletions fgpyo/util/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def get_fields_dict(cls: Type[DataclassesOrAttrClass]) -> Dict[str, FieldType]:
Combine results in case someone chooses to mix them through inheritance.
"""
if not (is_dataclasses_class(cls) or is_attr_class(cls)):
raise ValueError("cls must a dataclasses or attr class")
raise TypeError("cls must a dataclasses or attr class")
return {
**(get_dataclasses_fields_dict(cls) if is_dataclasses_class(cls) else {}),
**(get_attr_fields_dict(cls) if is_attr_class(cls) else {}), # type: ignore
Expand All @@ -331,7 +331,7 @@ def get_fields_dict(cls: Type[DataclassesOrAttrClass]) -> Dict[str, FieldType]:

def get_fields(cls: Type[DataclassesOrAttrClass]) -> Tuple[FieldType, ...]:
if not (is_dataclasses_class(cls) or is_attr_class(cls)):
raise ValueError("cls must a dataclasses or attr class")
raise TypeError("cls must a dataclasses or attr class")
return (get_dataclasses_fields(cls) if is_dataclasses_class(cls) else ()) + (
get_attr_fields(cls) if is_attr_class(cls) else () # type: ignore
)
Expand Down
23 changes: 22 additions & 1 deletion fgpyo/util/tests/test_inspect.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from typing import Optional

import attr
import pytest

from fgpyo.util.inspect import attr_from
from fgpyo.util.inspect import attribute_has_default
from fgpyo.util.inspect import attribute_is_optional
from fgpyo.util.inspect import get_fields
from fgpyo.util.inspect import get_fields_dict


@attr.s(auto_attribs=True, frozen=True)
class Name:
required: str
custom_parser: str
converted: int = attr.field(converter=int)
optional_no_default: Optional[str]
optional_with_default_none: Optional[str] = None
optional_with_default_some: Optional[str] = "foo"
Expand All @@ -19,11 +23,12 @@ class Name:
def test_attr_from() -> None:
name = attr_from(
cls=Name,
kwargs={"required": "required", "custom_parser": "before"},
kwargs={"required": "required", "custom_parser": "before", "converted": "42"},
parsers={str: lambda value: "after" if value == "before" else value},
)
assert name.required == "required"
assert name.custom_parser == "after"
assert name.converted == 42
assert name.optional_no_default is None
assert name.optional_with_default_none is None
assert name.optional_with_default_some == "foo"
Expand All @@ -33,6 +38,7 @@ def test_attribute_is_optional() -> None:
fields_dict = attr.fields_dict(Name)
assert not attribute_is_optional(fields_dict["required"])
assert not attribute_is_optional(fields_dict["custom_parser"])
assert not attribute_is_optional(fields_dict["converted"])
assert attribute_is_optional(fields_dict["optional_no_default"])
assert attribute_is_optional(fields_dict["optional_with_default_none"])
assert attribute_is_optional(fields_dict["optional_with_default_some"])
Expand All @@ -42,6 +48,21 @@ def test_attribute_has_default() -> None:
fields_dict = attr.fields_dict(Name)
assert not attribute_has_default(fields_dict["required"])
assert not attribute_has_default(fields_dict["custom_parser"])
assert not attribute_has_default(fields_dict["converted"])
assert attribute_has_default(fields_dict["optional_no_default"])
assert attribute_has_default(fields_dict["optional_with_default_none"])
assert attribute_has_default(fields_dict["optional_with_default_some"])


def test_non_data_class_fails() -> None:
class NonDataClass:
x: int

with pytest.raises(TypeError):
get_fields_dict(NonDataClass) # type: ignore

with pytest.raises(TypeError):
get_fields(NonDataClass) # type: ignore

with pytest.raises(TypeError):
attr_from(cls=NonDataClass, kwargs={"x": "1"}, parsers={int: int})

0 comments on commit 569af29

Please sign in to comment.