remove deprecated legacy resolve mode for context subclass

This commit is contained in:
David Lord 2021-11-09 12:50:28 -08:00
parent 88745a2b9e
commit 6faf093f9d
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
3 changed files with 3 additions and 55 deletions

View File

@ -17,6 +17,9 @@ Unreleased
- ``Markup`` and ``escape`` should be imported from MarkupSafe.
- Compiled templates from very old Jinja versions may need to be
recompiled.
- Legacy resolve mode for ``Context`` subclasses is no longer
supported. Override ``resolve_or_missing`` instead of
``resolve``.
Version 3.0.3

View File

@ -161,27 +161,6 @@ class Context:
:class:`Undefined` object for missing variables.
"""
_legacy_resolve_mode: t.ClassVar[bool] = False
def __init_subclass__(cls) -> None:
if "resolve_or_missing" in cls.__dict__:
# If the subclass overrides resolve_or_missing it opts in to
# modern mode no matter what.
cls._legacy_resolve_mode = False
elif "resolve" in cls.__dict__ or cls._legacy_resolve_mode:
# If the subclass overrides resolve, or if its base is
# already in legacy mode, warn about legacy behavior.
import warnings
warnings.warn(
"Overriding 'resolve' is deprecated and will not have"
" the expected behavior in Jinja 3.1. Override"
" 'resolve_or_missing' instead ",
DeprecationWarning,
stacklevel=2,
)
cls._legacy_resolve_mode = True
def __init__(
self,
environment: "Environment",
@ -239,15 +218,6 @@ class Context:
:param key: The variable name to look up.
"""
if self._legacy_resolve_mode:
if key in self.vars:
return self.vars[key]
if key in self.parent:
return self.parent[key]
return self.environment.undefined(name=key)
rv = self.resolve_or_missing(key)
if rv is missing:
@ -265,14 +235,6 @@ class Context:
:param key: The variable name to look up.
"""
if self._legacy_resolve_mode:
rv = self.resolve(key)
if isinstance(rv, Undefined):
return missing
return rv
if key in self.vars:
return self.vars[key]

View File

@ -591,23 +591,6 @@ class TestBug:
env = MyEnvironment(loader=loader)
assert env.get_template("test").render(foobar="test") == "test"
def test_legacy_custom_context(self, env):
from jinja2.runtime import Context, missing
with pytest.deprecated_call():
class MyContext(Context):
def resolve(self, name):
if name == "foo":
return 42
return super().resolve(name)
x = MyContext(env, parent={"bar": 23}, name="foo", blocks={})
assert x._legacy_resolve_mode
assert x.resolve_or_missing("foo") == 42
assert x.resolve_or_missing("bar") == 23
assert x.resolve_or_missing("baz") is missing
def test_recursive_loop_bug(self, env):
tmpl = env.from_string(
"{%- for value in values recursive %}1{% else %}0{% endfor -%}"