remove deprecated legacy resolve mode for context subclass
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 88745a2 · Browse files · 3 files·+3 added−55 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+3 added−55 removed
- tests:+0 added−17 removed
- other:+3 added−38 removed
Provenance
- Commit
6faf093- Tree
85476ab- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
88745a2
3 files changed+3 added−55 removed
modified:src/jinja2/runtime.py-38 removed
159 unmodified lines160 method that doesn't fail with a `KeyError` but returns an161 :class:`Undefined` object for missing variables.162 """163 164- _legacy_resolve_mode: t.ClassVar[bool] = False165-166- def __init_subclass__(cls) -> None:167- if "resolve_or_missing" in cls.__dict__:168- # If the subclass overrides resolve_or_missing it opts in to169- # modern mode no matter what.170- cls._legacy_resolve_mode = False171- elif "resolve" in cls.__dict__ or cls._legacy_resolve_mode:172- # If the subclass overrides resolve, or if its base is173- # already in legacy mode, warn about legacy behavior.174- import warnings175-176- warnings.warn(177- "Overriding 'resolve' is deprecated and will not have"178- " the expected behavior in Jinja 3.1. Override"179- " 'resolve_or_missing' instead ",180- DeprecationWarning,181- stacklevel=2,182- )183- cls._legacy_resolve_mode = True184-164 def __init__(165 self,166 environment: "Environment",167 parent: t.Dict[str, t.Any],49 unmodified lines217 functions use that method, not this one.218 219 :param key: The variable name to look up.220 """242- if self._legacy_resolve_mode:243- if key in self.vars:244- return self.vars[key]245-246- if key in self.parent:247- return self.parent[key]248-249- return self.environment.undefined(name=key)250-221 rv = self.resolve_or_missing(key)222 223 if rv is missing:224 return self.environment.undefined(name=key)9 unmodified lines234 method. Don't call this method directly.235 236 :param key: The variable name to look up.237 """268- if self._legacy_resolve_mode:269- rv = self.resolve(key)270-271- if isinstance(rv, Undefined):272- return missing273-274- return rv275-238 if key in self.vars:239 return self.vars[key]240 241 if key in self.parent:813 unmodified linesmodified:tests/test_regression.py-17 removed
589 unmodified lines590 loader = DictLoader({"base": "{{ foobar }}", "test": '{% extends "base" %}'})591 env = MyEnvironment(loader=loader)592 assert env.get_template("test").render(foobar="test") == "test"593 594- def test_legacy_custom_context(self, env):595- from jinja2.runtime import Context, missing596-597- with pytest.deprecated_call():598-599- class MyContext(Context):600- def resolve(self, name):601- if name == "foo":602- return 42603- return super().resolve(name)604-605- x = MyContext(env, parent={"bar": 23}, name="foo", blocks={})606- assert x._legacy_resolve_mode607- assert x.resolve_or_missing("foo") == 42608- assert x.resolve_or_missing("bar") == 23609- assert x.resolve_or_missing("baz") is missing610-594 def test_recursive_loop_bug(self, env):595 tmpl = env.from_string(596 "{%- for value in values recursive %}1{% else %}0{% endfor -%}"597 )147 unmodified linesmodified:CHANGES.rst+3 added
15 unmodified lines16 by ``pass_environment``.17 - ``Markup`` and ``escape`` should be imported from MarkupSafe.18 - Compiled templates from very old Jinja versions may need to be19 recompiled.20+ - Legacy resolve mode for ``Context`` subclasses is no longer21+ supported. Override ``resolve_or_missing`` instead of22+ ``resolve``.23 24 25 Version 3.0.326 -------------911 unmodified lines