Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude children from description #11

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# envolved Changelog
## 1.2.1
### Fixed
* The children of envvars that are excluded from the description are now also excluded.
## 1.2.0
### Added
* new argument `strip_items` for `CollectionParser`.
Expand Down
4 changes: 2 additions & 2 deletions docs/envvar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ EnvVars
pos_args: collections.base.Sequence[envvar.EnvVar | InferEnvVar] = ..., \
description: str | collections.abc.Sequence[str] | None = None,\
validators: collections.abc.Iterable[collections.abc.Callable[[T], T]] = (), \
on_partial: T | missing | as_default | discard = missing) -> envvar.SchemaEnvVar[T]:
on_partial: T | missing | as_default | discard = missing) -> envvar.SchemaEnvVar[T]
:noindex:

Creates an EnvVar that reads from multiple environment variables.
Expand Down Expand Up @@ -217,7 +217,7 @@ EnvVars

The EnvVar's :attr:`default` must not be :data:`missing` if this option is used.

* If set to :data:`missing`, an :exc:`~exceptions.MissingEnvError` will be raised, even if the EnvVar's
* If set to :data:`missing`, a :exc:`~exceptions.MissingEnvError` will be raised, even if the EnvVar's
:attr:`~EnvVar.default` is set.
* If set to a value, that value will be returned.

Expand Down
2 changes: 1 addition & 1 deletion envolved/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.2.0"
__version__ = "1.2.1"
30 changes: 19 additions & 11 deletions envolved/describe/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from __future__ import annotations

from itertools import chain
from typing import Any, Iterable, List, Mapping, Set, TypeVar, Union

from envolved.describe.flat import FlatEnvVarsDescription
from envolved.describe.nested import NestedEnvVarsDescription, RootNestedDescription
from envolved.envvar import EnvVar, InferEnvVar, top_level_env_vars
from envolved.envvar import EnvVar, InferEnvVar, all_env_vars


def describe_env_vars(**kwargs: Any) -> List[str]:
Expand All @@ -19,12 +20,20 @@ def __init__(self, env_vars: Iterable[EnvVar] | None = None) -> None:
children: Set[EnvVar] = set()

if env_vars is None:
env_vars = top_level_env_vars
env_vars = all_env_vars
to_exclude = roots_to_exclude_from_description | set(
chain.from_iterable(r._get_descendants() for r in roots_to_exclude_from_description)
)
else:
to_exclude = set()

for env_var in env_vars:
self.env_var_roots.add(env_var)
children.update(env_var._get_descendants())
# remove any children we found along the way
self.env_var_roots -= children
# remove any children we were asked to exclude
self.env_var_roots -= to_exclude

def flat(self) -> FlatEnvVarsDescription:
return FlatEnvVarsDescription.from_envvars(self.env_var_roots)
Expand All @@ -38,21 +47,20 @@ def nested(self) -> NestedEnvVarsDescription:
bound=Union[EnvVar, InferEnvVar, Iterable[Union[EnvVar, InferEnvVar]], Mapping[Any, Union[EnvVar, InferEnvVar]]],
)

roots_to_exclude_from_description: Set[EnvVar] = set()

def exclude_from_description(to_exclude: T) -> T:
global top_level_env_vars # noqa: PLW0603

def exclude_from_description(to_exclude: T) -> T:
if isinstance(to_exclude, EnvVar):
evs = frozenset((to_exclude,))
elif isinstance(to_exclude, InferEnvVar):
evs = frozenset()
roots_to_exclude_from_description.add(to_exclude)
elif isinstance(to_exclude, Mapping):
evs = frozenset(to_exclude.values())
exclude_from_description(to_exclude.values())
elif isinstance(to_exclude, Iterable):
evs = frozenset(to_exclude)
for v in to_exclude:
exclude_from_description(v)
elif isinstance(to_exclude, InferEnvVar):
pass
else:
raise TypeError(f"cannot exclude unrecognized type {type(to_exclude)!r}")

top_level_env_vars -= evs

return to_exclude
4 changes: 2 additions & 2 deletions envolved/envvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,13 @@ def env_var( # type: ignore[misc]
return register_env_var(ev)


top_level_env_vars: MutableSet[EnvVar] = WeakSet()
all_env_vars: MutableSet[EnvVar] = WeakSet()

EV = TypeVar("EV", bound=EnvVar)


def register_env_var(ev: EV) -> EV:
top_level_env_vars.add(ev)
all_env_vars.add(ev)
return ev


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "envolved"
version = "1.2.0"
version = "1.2.1"
description = ""
authors = ["ben avrahami <[email protected]>"]
license = "MIT"
Expand Down
12 changes: 11 additions & 1 deletion tests/unittests/test_describe.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ def test_describe():
}
exclude_from_description(point_args)

p = env_var("p_", type=SimpleNamespace, args=point_args) # noqa: F841
_p = exclude_from_description(env_var("_p_", type=SimpleNamespace, args=point_args))

p = env_var("p_", type=SimpleNamespace, args=point_args)

_w_p = exclude_from_description(p.with_prefix("_w_"))

j_p = _p.with_prefix("j")
j_p.description = "j point"

q = env_var( # noqa: F841
"q_",
Expand Down Expand Up @@ -51,6 +58,9 @@ def test_describe():
assert describe_env_vars() == [
"A: full description of A",
"B",
"j point:",
" J_P_X: x coordinate",
" J_P_Y: y coordinate",
" P_X: x coordinate",
" P_Y: y coordinate",
"point Q next line:",
Expand Down