jinja2.__version__ is deprecated (#85221)

Prepare for its removal in Jinja 3.3.

See https://github.com/pallets/jinja/pull/2098

(cherry picked from commit 0e2f770a24)
pull/85255/head
Martin Krizek 6 months ago committed by Matt Davis
parent 5fd78b07fb
commit 0d01da2dc2

@ -0,0 +1,2 @@
minor_changes:
- "Use ``importlib.metadata.version()`` to detect Jinja version as jinja2.__version__ is deprecated and will be removed in Jinja 3.3."

@ -1,10 +1,12 @@
from __future__ import annotations from __future__ import annotations
from jinja2 import __version__ as _jinja2_version import importlib.metadata
jinja2_version = importlib.metadata.version('jinja2')
# DTFIX-FUTURE: sanity test to ensure this doesn't drift from requirements # DTFIX-FUTURE: sanity test to ensure this doesn't drift from requirements
_MINIMUM_JINJA_VERSION = (3, 1) _MINIMUM_JINJA_VERSION = (3, 1)
_CURRENT_JINJA_VERSION = tuple(map(int, _jinja2_version.split('.', maxsplit=2)[:2])) _CURRENT_JINJA_VERSION = tuple(map(int, jinja2_version.split('.', maxsplit=2)[:2]))
if _CURRENT_JINJA_VERSION < _MINIMUM_JINJA_VERSION: if _CURRENT_JINJA_VERSION < _MINIMUM_JINJA_VERSION:
raise RuntimeError(f'Jinja version {".".join(map(str, _MINIMUM_JINJA_VERSION))} or higher is required (current version {_jinja2_version}).') raise RuntimeError(f'Jinja version {".".join(map(str, _MINIMUM_JINJA_VERSION))} or higher is required (current version {jinja2_version}).')

@ -16,10 +16,9 @@ import typing as t
import yaml import yaml
from jinja2 import __version__ as j2_version
import ansible import ansible
from ansible import constants as C from ansible import constants as C
from ansible._internal import _templating
from ansible.module_utils.common.text.converters import to_native from ansible.module_utils.common.text.converters import to_native
from ansible.module_utils.common.yaml import HAS_LIBYAML, yaml_load from ansible.module_utils.common.yaml import HAS_LIBYAML, yaml_load
from ansible.release import __version__ from ansible.release import __version__
@ -313,7 +312,7 @@ def version(prog=None):
result.append(" ansible collection location = %s" % ':'.join(C.COLLECTIONS_PATHS)) result.append(" ansible collection location = %s" % ':'.join(C.COLLECTIONS_PATHS))
result.append(" executable location = %s" % sys.argv[0]) result.append(" executable location = %s" % sys.argv[0])
result.append(" python version = %s (%s)" % (''.join(sys.version.splitlines()), to_native(sys.executable))) result.append(" python version = %s (%s)" % (''.join(sys.version.splitlines()), to_native(sys.executable)))
result.append(" jinja version = %s" % j2_version) result.append(f" jinja version = {_templating.jinja2_version}")
result.append(f" pyyaml version = {yaml.__version__} ({libyaml_fragment})") result.append(f" pyyaml version = {yaml.__version__} ({libyaml_fragment})")
return "\n".join(result) return "\n".join(result)

Loading…
Cancel
Save