Skip to content

Commit

Permalink
address review requests
Browse files Browse the repository at this point in the history
* tighten up dunder method exclusion heuristic and clarify raison d'etre in Undefined/ChainableUndefined.__getattr__
* use a more granular type ignore for incorrect base class declaration on ChainableUndefined.__getattr__
  • Loading branch information
nitzmahone committed Oct 1, 2024
1 parent d2f6333 commit 020dd78
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/jinja2/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,11 @@ def _fail_with_undefined_error(

@internalcode
def __getattr__(self, name: str) -> t.Any:
if name[:2] == "__":
# Raise AttributeError on requests for names that appear to be unimplemented
# dunder methods to keep Python's internal protocol probing behaviors working
# properly in cases where another exception type could cause unexpected or
# difficult-to-diagnose failures.
if name[:2] == "__" and name[-2:] == "__":
raise AttributeError(name)

return self._fail_with_undefined_error()
Expand Down Expand Up @@ -983,13 +987,18 @@ def __html__(self) -> str:
return str(self)

def __getattr__(self, name: str) -> "ChainableUndefined":
# unimplemented dunder methods returning Undefined break core Python protocols
if name[:2] == "__":
# Raise AttributeError on requests for names that appear to be unimplemented
# dunder methods to avoid confusing Python with truthy non-method objects that
# do not implement the protocol being probed for. e.g., copy.copy(Undefined())
# fails spectacularly if getattr(Undefined(), '__setstate__') returns an
# Undefined object instead of raising AttributeError to signal that it does not
# support that style of object initialization.
if name[:2] == "__" and name[-2:] == "__":
raise AttributeError(name)

return self

def __getitem__(self, _: str) -> "ChainableUndefined": # type: ignore
def __getitem__(self, _name: str) -> "ChainableUndefined": # type: ignore[override]
return self


Expand Down

0 comments on commit 020dd78

Please sign in to comment.