diff options
author | Abhijeet Kasurde <akasurde@redhat.com> | 2024-09-23 22:41:08 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-23 22:41:08 +0200 |
commit | b5263c2c10118cdc0b0fe8d29bdac5040aa0c194 (patch) | |
tree | 6d924282f5fa96acd9c09e522454a4c96f455aaa | |
parent | ansible-test - Use Python version in pylint contexts (#83984) (diff) | |
download | ansible-b5263c2c10118cdc0b0fe8d29bdac5040aa0c194.tar.xz ansible-b5263c2c10118cdc0b0fe8d29bdac5040aa0c194.zip |
isidentifier: Remove Python 2 specific code (#83688)
Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
-rw-r--r-- | changelogs/fragments/isidentifier.yml | 3 | ||||
-rw-r--r-- | lib/ansible/utils/vars.py | 41 | ||||
-rw-r--r-- | test/units/utils/test_isidentifier.py | 10 |
3 files changed, 20 insertions, 34 deletions
diff --git a/changelogs/fragments/isidentifier.yml b/changelogs/fragments/isidentifier.yml new file mode 100644 index 0000000000..3b5b09241e --- /dev/null +++ b/changelogs/fragments/isidentifier.yml @@ -0,0 +1,3 @@ +--- +minor_changes: + - isidentifier - remove unwanted Python 2 specific code. diff --git a/lib/ansible/utils/vars.py b/lib/ansible/utils/vars.py index 9e0510a766..50ddf42e7f 100644 --- a/lib/ansible/utils/vars.py +++ b/lib/ansible/utils/vars.py @@ -32,8 +32,6 @@ from ansible.module_utils.common.text.converters import to_native, to_text from ansible.parsing.splitter import parse_kv -ADDITIONAL_PY2_KEYWORDS = frozenset(("True", "False", "None")) - _MAXSIZE = 2 ** 32 cur_id = 0 node_mac = ("%012x" % uuid.getnode())[:12] @@ -237,7 +235,22 @@ def load_options_vars(version): return load_options_vars.options_vars -def _isidentifier_PY3(ident): +def isidentifier(ident): + """Determine if string is valid identifier. + + The purpose of this function is to be used to validate any variables created in + a play to be valid Python identifiers and to not conflict with Python keywords + to prevent unexpected behavior. Since Python 2 and Python 3 differ in what + a valid identifier is, this function unifies the validation so playbooks are + portable between the two. The following changes were made: + + * disallow non-ascii characters (Python 3 allows for them as opposed to Python 2) + + :arg ident: A text string of identifier to check. Note: It is callers + responsibility to convert ident to text if it is not already. + + Originally posted at https://stackoverflow.com/a/29586366 + """ if not isinstance(ident, string_types): return False @@ -251,25 +264,3 @@ def _isidentifier_PY3(ident): return False return True - - -isidentifier = _isidentifier_PY3 - - -isidentifier.__doc__ = """Determine if string is valid identifier. - -The purpose of this function is to be used to validate any variables created in -a play to be valid Python identifiers and to not conflict with Python keywords -to prevent unexpected behavior. Since Python 2 and Python 3 differ in what -a valid identifier is, this function unifies the validation so playbooks are -portable between the two. The following changes were made: - - * disallow non-ascii characters (Python 3 allows for them as opposed to Python 2) - * True, False and None are reserved keywords (these are reserved keywords - on Python 3 as opposed to Python 2) - -:arg ident: A text string of identifier to check. Note: It is callers - responsibility to convert ident to text if it is not already. - -Originally posted at http://stackoverflow.com/a/29586366 -""" diff --git a/test/units/utils/test_isidentifier.py b/test/units/utils/test_isidentifier.py index e4b2a40b27..1a88f5e3c4 100644 --- a/test/units/utils/test_isidentifier.py +++ b/test/units/utils/test_isidentifier.py @@ -24,21 +24,13 @@ def test_valid_identifier(identifier): @pytest.mark.parametrize( "identifier", [ "pass", "foo ", " foo", "1234", "1234abc", "", " ", "foo bar", "no-dashed-names-for-you", + "True", "False", "None" ] ) def test_invalid_identifier(identifier): assert not isidentifier(identifier) -def test_keywords_not_in_PY2(): - """In Python 2 ("True", "False", "None") are not keywords. The isidentifier - method ensures that those are treated as keywords on both Python 2 and 3. - """ - assert not isidentifier("True") - assert not isidentifier("False") - assert not isidentifier("None") - - def test_non_ascii(): """In Python 3 non-ascii characters are allowed as opposed to Python 2. The isidentifier method ensures that those are treated as keywords on both |