From b56b38b3ba0ef403ac0e30d412c06ed1080e7fb3 Mon Sep 17 00:00:00 2001 From: Martin Krizek Date: Wed, 20 Jul 2022 18:21:20 +0200 Subject: [PATCH] Use classproperty to access field attributes of a class (#78180) * Use classproperty to access field attributes of a class, consolidate into one class --- .../fragments/fieldattributes-classproperty.yml | 2 ++ lib/ansible/playbook/base.py | 12 ++---------- test/units/playbook/test_base.py | 2 +- 3 files changed, 5 insertions(+), 11 deletions(-) create mode 100644 changelogs/fragments/fieldattributes-classproperty.yml diff --git a/changelogs/fragments/fieldattributes-classproperty.yml b/changelogs/fragments/fieldattributes-classproperty.yml new file mode 100644 index 00000000000..684cb27f8ef --- /dev/null +++ b/changelogs/fragments/fieldattributes-classproperty.yml @@ -0,0 +1,2 @@ +minor_changes: + - Utilize @classmethod and @property together to form classproperty (Python 3.9) to access field attributes of a class diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py index 8837dd4f47f..ae7957b0bcf 100644 --- a/lib/ansible/playbook/base.py +++ b/lib/ansible/playbook/base.py @@ -69,9 +69,9 @@ def _validate_action_group_metadata(action, found_group_metadata, fq_group_name) display.warning(" ".join(metadata_warnings)) -# FIXME use @property and @classmethod together which is possible since Python 3.9 -class _FABMeta(type): +class FieldAttributeBase: + @classmethod @property def fattributes(cls): # FIXME is this worth caching? @@ -86,14 +86,6 @@ class _FABMeta(type): fattributes[attr.alias] = attr return fattributes - -class FieldAttributeBase(metaclass=_FABMeta): - - # FIXME use @property and @classmethod together which is possible since Python 3.9 - @property - def fattributes(self): - return self.__class__.fattributes - def __init__(self): # initialize the data loader and variable manager, which will be provided diff --git a/test/units/playbook/test_base.py b/test/units/playbook/test_base.py index f05c0cc1435..d5810e73af9 100644 --- a/test/units/playbook/test_base.py +++ b/test/units/playbook/test_base.py @@ -488,7 +488,7 @@ class TestBaseSubClass(TestBase): def test_attr_class_post_validate_class_not_instance(self): not_a_esc = ExampleSubClass ds = {'test_attr_class_post_validate': not_a_esc} - self.assertRaisesRegex(AnsibleParserError, 'is not a valid.*got a.*Meta.*instead', + self.assertRaisesRegex(AnsibleParserError, "is not a valid.*got a instead", self._base_validate, ds) def test_attr_class_post_validate_wrong_class(self):