Skip to content

Commit

Permalink
Fix faulty enum check during helptext generation
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed Mar 25, 2022
1 parent ca9af2b commit 2fc51d9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dcargs/_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def transform_generate_helptext(arg: ArgumentDefinition) -> ArgumentDefinition:
if arg.action is not None:
# Don't show defaults for boolean flags.
assert arg.action in ("store_true", "store_false")
elif arg.default is not None and hasattr(arg.default, "name"):
elif arg.default is not None and isinstance(arg.default, enum.Enum):
# Special case for enums.
help_parts.append(f"(default: {arg.default.name})")
elif not arg.required:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="dcargs",
version="0.0.16",
version="0.0.17",
description="Portable, reusable, strongly typed CLIs from dataclass definitions",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
22 changes: 22 additions & 0 deletions tests/test_docstrings.py → tests/test_helptext.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import contextlib
import dataclasses
import enum
import io
import pathlib
from typing import Generic, List, Optional, Tuple, TypeVar

import pytest
Expand Down Expand Up @@ -30,6 +32,26 @@ class Helptext:
assert "--z INT Documentation 3 (default: 3)\n" in helptext


def test_helptext_defaults():
class Color(enum.Enum):
RED = enum.auto()
GREEN = enum.auto()
BLUE = enum.auto()

@dataclasses.dataclass
class HelptextWithVariousDefaults:
x: pathlib.Path = pathlib.Path("/some/path/to/a/file")
y: Color = Color.RED

f = io.StringIO()
with pytest.raises(SystemExit):
with contextlib.redirect_stdout(f):
dcargs.parse(HelptextWithVariousDefaults, args=["--help"])
helptext = f.getvalue()
assert "--x PATH (default: /some/path/to/a/file)\n" in helptext
assert "--y {RED,GREEN,BLUE} (default: RED)\n" in helptext


def test_multiline_helptext():
@dataclasses.dataclass
class HelptextMultiline:
Expand Down

0 comments on commit 2fc51d9

Please sign in to comment.