From 8525e420bcfed4dfa34b3a1a6ce8f04471aa314a Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 14 Apr 2025 20:48:42 +0200 Subject: [PATCH] Avoid deprecated AnsibleFilterTypeError (#9992) Avoid deprecated AnsibleFilterTypeError. --- changelogs/fragments/9992-filtertypeerror.yml | 2 ++ plugins/filter/hashids.py | 8 ++++++-- plugins/filter/unicode_normalize.py | 9 +++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/9992-filtertypeerror.yml diff --git a/changelogs/fragments/9992-filtertypeerror.yml b/changelogs/fragments/9992-filtertypeerror.yml new file mode 100644 index 0000000000..ccb2ff8c0c --- /dev/null +++ b/changelogs/fragments/9992-filtertypeerror.yml @@ -0,0 +1,2 @@ +bugfixes: + - "hashids and unicode_normalize filter plugins - avoid deprecated ``AnsibleFilterTypeError`` on ansible-core 2.19 (https://github.com/ansible-collections/community.general/pull/9992)." diff --git a/plugins/filter/hashids.py b/plugins/filter/hashids.py index 6ec64d5f59..76e6aaa3a5 100644 --- a/plugins/filter/hashids.py +++ b/plugins/filter/hashids.py @@ -9,12 +9,16 @@ from __future__ import annotations from ansible.errors import ( AnsibleError, AnsibleFilterError, - AnsibleFilterTypeError, ) from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.collections import is_sequence +try: + from ansible.errors import AnsibleTypeError +except ImportError: + from ansible.errors import AnsibleFilterTypeError as AnsibleTypeError + try: from hashids import Hashids HAS_HASHIDS = True @@ -63,7 +67,7 @@ def hashids_encode(nums, salt=None, alphabet=None, min_length=None): try: hashid = hashids.encode(*nums) except TypeError as e: - raise AnsibleFilterTypeError( + raise AnsibleTypeError( "Data to encode must by a tuple or list of ints: %s" % to_native(e) ) diff --git a/plugins/filter/unicode_normalize.py b/plugins/filter/unicode_normalize.py index e897bb9cee..aed7979de8 100644 --- a/plugins/filter/unicode_normalize.py +++ b/plugins/filter/unicode_normalize.py @@ -48,9 +48,14 @@ _value: from unicodedata import normalize -from ansible.errors import AnsibleFilterError, AnsibleFilterTypeError +from ansible.errors import AnsibleFilterError from ansible.module_utils.six import text_type +try: + from ansible.errors import AnsibleTypeError +except ImportError: + from ansible.errors import AnsibleFilterTypeError as AnsibleTypeError + def unicode_normalize(data, form='NFC'): """Applies normalization to 'unicode' strings. @@ -65,7 +70,7 @@ def unicode_normalize(data, form='NFC'): """ if not isinstance(data, text_type): - raise AnsibleFilterTypeError("%s is not a valid input type" % type(data)) + raise AnsibleTypeError("%s is not a valid input type" % type(data)) if form not in ('NFC', 'NFD', 'NFKC', 'NFKD'): raise AnsibleFilterError("%s is not a valid form" % form)