Skip to content

Commit

Permalink
Change all group membership test utilities to be functions instead of…
Browse files Browse the repository at this point in the history
… variables
  • Loading branch information
kdeldycke committed Dec 4, 2024
1 parent 41516e5 commit 635115c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 10 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
> [!IMPORTANT]
> This version is not released yet and is under active development.
- Change all group membership test utilities to be functions instead of variables. You now have to call `is_<group_id>()` instead of `is_<group_id>`.

## [1.7.0 (2024-12-02)](https://github.com/kdeldycke/extra-platforms/compare/v1.6.0...v1.7.0)

- Display the hierarchy of non-overlapping groups as a mindmap.
Expand Down
37 changes: 29 additions & 8 deletions extra_platforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,41 @@ def current_os() -> Platform:


for _group in ALL_GROUPS:
var_id = f"is_{_group.id}"
var_value = current_os() in _group
assert var_id not in locals()
# TODO: use a cached method instead of a hard-coded boolean variable and attach a
# docstring to that method with the groups' own docstring.
locals()[var_id] = var_value
"""Generates a ``is_<group.id>`` local variable for each group.
func_id = f"is_{_group.id}"
assert func_id not in locals(), f"Function ID {func_id} already defined locally."

def generate_group_membership_func(_group: Group) -> callable:
"""Factory to produce dynamiccaly a group membership test function."""

def group_membership_test() -> bool:
"""Evaluates membership of the current platform to the group.
Returns ``True`` if the current platform is part of the group, ``False``
otherwise.
"""
return current_os() in _group

# Use lower-cased group name as a short description for the function docstring.
short_desc = _group.name[0].lower() + _group.name[1:]
group_membership_test.__doc__ = (
"Returns ``True`` if the current platform is part of the group composed "
f"of {short_desc}, ``False`` otherwise."
)

# TODO: cache the function.
return group_membership_test

locals()[func_id] = generate_group_membership_func(_group)
"""Generates a ``is_<group.id>()`` local function for each group.
These functions return a boolean value indicating the membership of the current
platform into that group.
Since platforms and groups have unique, non-overlapping IDs, we can create a
``is_<group.id>`` method for each group. The value of this boolean variable mark the
membership of the current platform to that group.
"""


__all__ = [
"AIX", # noqa: F405
"ALL_GROUPS", # noqa: F405
Expand Down
4 changes: 2 additions & 2 deletions extra_platforms/group_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@

UNIX_WITHOUT_MACOS = Group(
"unix_without_macos",
"Any Unix but macOS",
"Any Unix excluding macOS",
"⨂",
tuple(UNIX - MACOS),
)
Expand Down Expand Up @@ -173,7 +173,7 @@

BSD_WITHOUT_MACOS = Group(
"bsd_without_macos",
"Any BSD but macOS",
"Any BSD excluding macOS",
"🅱️",
tuple(BSD - MACOS),
)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import ast
import inspect
import types
from pathlib import Path

import extra_platforms
Expand Down Expand Up @@ -151,3 +152,18 @@ def test_current_os_func():
# Function.
current_platform = current_os()
assert current_platform in ALL_PLATFORMS.platforms


def test_group_membership_funcs():
for group in ALL_GROUPS:
func_id = f"is_{group.id}"
assert func_id in extra_platforms.__dict__

func = extra_platforms.__dict__[func_id]
assert isinstance(func, types.FunctionType)
assert getattr(extra_platforms, func_id) is func

assert isinstance(func(), bool)
assert func() == (current_os() in group)

assert group.name.lower() in func.__doc__.lower()

0 comments on commit 635115c

Please sign in to comment.