pallets / jinja

A very fast and expressive template engine.

Home Page:https://jinja.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Misleading error when attribute in getitem(obj, attr) throws AttributeError

agudallago opened this issue · comments

import os
from jinja2 import environment

class Foo:
  @property
  def bar(self):
    return os.system2 # throws AttributeError

foo = Foo()
environment.Environment().getitem(foo, "bar")._undefined_message

getitem() gives a misleading error when the property bar we are trying to access in object foo gives an AttributeError.

The expected message (which in turn is propagated to the render method) says:

'__main__.Foo object' has no attribute 'bar'

whereas it should say that bar exists, but has itself thrown an AttributeError.

This looks like a stupid thing but it's incredibly hard to debug if it happens inside render, since debugging the rendering process in the HTML line by line is super obscure.

Environment:

  • Python version: 3.11.5
  • Jinja version: 3.1.2

Happy to review a PR, but I don't see what we could do here.

Handle the AttributeError in a graceful way. Check if the function exists by looking into the dir() list for example.

You're asking "detect where an attribute error was raised" which is not trivial, and may not be possible or without other issues. "Just handle it gracefully" isn't really helpful. Again, I'm happy to review a PR, since you seem to know what you're looking for and how to do it.

except AttributeError:

There you're already handling the AttributeError. What I'm saying is when you handle, you can check if attr is part of obj or not. And then output a different message to the undefined.

Am I being too vague?

Yes, I need you to tell me exactly what that code should do to detect this situation.

What you're describing is a general issue with Python, it's not possible to tell the difference between and attribute that doesn't exist and a property that raises an AttributeError at runtime without expensive/imperfect introspection. After runtime, when the traceback is built and the frames printed out, you can look backwards and see what actually raised the AttributeError, and know that it's correct. But at runtime that's not viable, examining the stack is expensive and should not be done on every undefined, hasattr has the same problem as getattr, and dir can be overridden or inaccurate (for example, if __getattr__ is defined).

I understand doing proper introspection is expensive. But how does any extra expensive or imperfect introspection make this a worse user experience than getting a misleading error, if the program anyway is bound to raise an exception that in most programs would actually make your program ends or retries due to invalid arguments (either your html is broken, or. your context is).

I'm not sure how the dir can be inaccurate, you probably know a lot more about Python low level, but to me sounds like a cheap enough thing to check if the attribute is there and would make the error a lot more helpful for the user. And the library is supposed to help the user.