Skip to content

Commit

Permalink
Merge pull request AdaCore#698 from leocardao/mr/cardao/method-or-pro…
Browse files Browse the repository at this point in the history
…perty

Enable Anod class to use methods instead of properties
  • Loading branch information
leocardao authored Mar 26, 2024
2 parents f1a4a60 + 0a80bf0 commit abde23a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/e3/anod/sandbox/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
from e3.anod.error import AnodError
from e3.main import Main

from typing import TYPE_CHECKING

def anod_cmdline(subparsers, action_name: str, action_help: str) -> None: # type: ignore
if TYPE_CHECKING:
from argparse import _SubParsersAction


def anod_cmdline(
subparsers: _SubParsersAction, action_name: str, action_help: str
) -> None:
parser = subparsers.add_parser(action_name, help=action_help)
parser.set_defaults(action_name=action_name)
parser.add_argument("spec", metavar="SPEC", help="The specification file name")
Expand All @@ -29,7 +36,7 @@ def anod() -> None:
assert sys.modules["__main__"].__file__ is not None

sandbox_dir = os.path.abspath(
os.path.join(os.path.dirname(sys.modules["__main__"].__file__), os.pardir)
os.path.join(os.path.dirname(sys.modules["__main__"].__file__), os.pardir) # type: ignore
)

sandbox = e3.anod.sandbox.SandBox(root_dir=sandbox_dir)
Expand Down
20 changes: 20 additions & 0 deletions src/e3/anod/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,26 @@ class MyProduct(Anod):
ExternalSourceBuilder = e3.anod.package.ExternalSourceBuilder
ThirdPartySourceBuilder = e3.anod.package.ThirdPartySourceBuilder

def __new__(cls, *args: Any, **kwargs: Any) -> Any:
"""Replace `method` by property when decorator is missing."""
should_be_property = (
"enable_name_generator",
"readme_info",
"base_name",
"build_space_name",
"has_package",
"package",
"component",
"source_pkg_build",
)

for prop in should_be_property:
class_property = getattr(cls, prop)
if callable(class_property):
setattr(cls, prop, property(fget=class_property))

return super().__new__(cls)

def __init__(
self,
qualifier: str,
Expand Down
9 changes: 9 additions & 0 deletions tests/tests_e3/anod/spec_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,12 @@ class GeneratorDisabled(Anod):

assert spec_enable.args == {"q1": True}
assert spec_disable.args == {"q1": ""}


def test_missing_property():
class NoProperty(Anod):
def source_pkg_build(self) -> list:
return []

noproperty = NoProperty(qualifier="", kind="source")
assert noproperty.source_pkg_build == []

0 comments on commit abde23a

Please sign in to comment.