Skip to content

Commit

Permalink
refactor!: expose HelpInfo as a namedtuple (#37)
Browse files Browse the repository at this point in the history
We refactor the HelpInfo type to be a namedtuple and be part of
the public API for better consumption of the information. We also
drop the assumption that the variable name would be formatted in
a RST document and remove the double backtick wrapping from the
generated help info data.
  • Loading branch information
P403n1x87 authored Oct 21, 2024
1 parent 672ca6e commit ba018d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
3 changes: 2 additions & 1 deletion envier/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from envier.env import Env
from envier.env import HelpInfo


En = Env
__all__ = ["En", "Env"]
__all__ = ["En", "Env", "HelpInfo"]
9 changes: 5 additions & 4 deletions envier/env.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import namedtuple
import os
import typing as t
import warnings
Expand All @@ -17,7 +18,7 @@ def __str__(self):
V = t.TypeVar("V")

MapType = t.Union[t.Callable[[str], V], t.Callable[[str, str], t.Tuple[K, V]]]
HelpInfo = t.Tuple[str, str, str, str]
HelpInfo = namedtuple("HelpInfo", ("name", "type", "default", "help"))


def _normalized(name: str) -> str:
Expand Down Expand Up @@ -432,16 +433,16 @@ def add_entries(full_prefix: str, config: t.Type[Env]) -> None:
help_type = v.help_type
else:
try:
help_type = "``%s``" % v.type.__name__ # type: ignore[attr-defined]
help_type = v.type.__name__ # type: ignore[attr-defined]
except AttributeError:
# typing.t.Union[<type>, NoneType]
help_type = v.type.__args__[0].__name__ # type: ignore[attr-defined]

private_prefix = "_" if v.private else ""

entries.append(
(
f"``{private_prefix}{full_prefix}{_normalized(v.name)}``",
HelpInfo(
f"{private_prefix}{full_prefix}{_normalized(v.name)}",
help_type, # type: ignore[attr-defined]
(
v.help_default
Expand Down
7 changes: 4 additions & 3 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from envier import En
from envier import Env
from envier import HelpInfo


def test_env_default():
Expand Down Expand Up @@ -377,8 +378,8 @@ class Config(Env):
assert config.private == 24
assert config.public == 25

assert Config.help_info() == [("``PUBLIC_FOO``", "``int``", "42", "")]
assert Config.help_info() == [HelpInfo("PUBLIC_FOO", "int", "42", "")]
assert set(Config.help_info(include_private=True)) == {
("``_PRIVATE_FOO``", "``int``", "42", ""),
("``PUBLIC_FOO``", "``int``", "42", ""),
("_PRIVATE_FOO", "int", "42", ""),
("PUBLIC_FOO", "int", "42", ""),
}
25 changes: 13 additions & 12 deletions tests/test_help.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from envier import Env
from envier import HelpInfo


class GlobalConfig(Env):
Expand Down Expand Up @@ -46,15 +47,15 @@ def test_help_info(monkeypatch):
monkeypatch.setenv("MYAPP_NO_DEFAULT", "1")

assert GlobalConfig.help_info() == [
("``MYAPP_DEBUG``", "Boolean", "False", "Whether to enable debug logging."),
(
"``MYAPP_NO_DEFAULT``",
HelpInfo("MYAPP_DEBUG", "Boolean", "False", "Whether to enable debug logging."),
HelpInfo(
"MYAPP_NO_DEFAULT",
"Boolean",
"",
"A variable with no default value, which makes it mandatory.",
),
(
"``MYAPP_URL``",
HelpInfo(
"MYAPP_URL",
"String",
"http://localhost:5000",
"The URL of the application.",
Expand All @@ -66,19 +67,19 @@ def test_help_info_recursive(monkeypatch):
monkeypatch.setenv("MYAPP_NO_DEFAULT", "1")

assert GlobalConfig.help_info(recursive=True) == [
("``MYAPP_DEBUG``", "Boolean", "False", "Whether to enable debug logging."),
(
"``MYAPP_NO_DEFAULT``",
HelpInfo("MYAPP_DEBUG", "Boolean", "False", "Whether to enable debug logging."),
HelpInfo(
"MYAPP_NO_DEFAULT",
"Boolean",
"",
"A variable with no default value, which makes it mandatory.",
),
(
"``MYAPP_URL``",
HelpInfo(
"MYAPP_URL",
"String",
"http://localhost:5000",
"The URL of the application.",
),
("``MYAPP_SERVICE_HOST``", "``str``", "localhost", "The host of the service."),
("``MYAPP_SERVICE_PORT``", "``int``", "3000", "The port of the service."),
HelpInfo("MYAPP_SERVICE_HOST", "str", "localhost", "The host of the service."),
HelpInfo("MYAPP_SERVICE_PORT", "int", "3000", "The port of the service."),
]

0 comments on commit ba018d7

Please sign in to comment.