Comments (7)
Happy to review a PR, but I don't see what we could do here.
from jinja.
Handle the AttributeError in a graceful way. Check if the function exists by looking into the dir() list for example.
from jinja.
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.
from jinja.
jinja/src/jinja2/environment.py
Line 478 in d594969
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?
from jinja.
Yes, I need you to tell me exactly what that code should do to detect this situation.
from jinja.
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).
from jinja.
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.
from jinja.
Related Issues (20)
- Autoescape does not work well across blocks/inheritance
- Test failures with Python 3.13.0a1 HOT 3
- Property Template::is_up_to_date breaks type hint contract HOT 2
- NativeEnvironment casts integer strings to integer -- intended behavior? HOT 2
- Otera, a Better name than jinja.. HOT 1
- Change github link to ssh in "contributing" docs HOT 3
- Help improve Syntax Highlight for Monaco-Editor HOT 1
- The `int` filter throws `OverflowError` when the incoming string looks like scientific notation HOT 3
- Most basic Jinja function is inconsistent / crashes if the template has `{{ obj.property }}` elements, and `obj` is `None` or property does not exist HOT 2
- Introducing `NeverUndefined` HOT 3
- Leading newline after `trans` block with Jinja trim options results in translation lookup failure HOT 1
- Indent filter fails confusing if input is not string HOT 3
- test_striptags regressed with markupsafe 2.1.4 HOT 1
- Is it not possible to utilize a test adorned with the @pass_context decorator in the select function?
- add sort_keys argument to `tojson` filter HOT 2
- `tojson` always do autoescape HOT 2
- Native types in macros works for list but does not work for int HOT 2
- Template globals not visible in {% import %}'ed file when it's {% include %}'ed by another file HOT 2
- Flake8 error B036 reported by updated flake8-bugbear version HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jinja.