Compare commits

..

No commits in common. "main" and "3.1.4" have entirely different histories.
main ... 3.1.4

81 changed files with 1724 additions and 3024 deletions

View File

@ -1,17 +1,7 @@
#!/bin/bash
set -e
# Install uv if not already installed
if ! command -v uv &> /dev/null; then
echo "Installing uv..."
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.cargo/bin:$PATH"
fi
# Create venv using uv and install dependencies
echo "Creating virtual environment and installing dependencies..."
uv sync
# Install pre-commit hooks
echo "Installing pre-commit hooks..."
python3 -m venv --upgrade-deps .venv
. .venv/bin/activate
pip install -r requirements/dev.txt
pip install -e .
pre-commit install --install-hooks

24
.github/dependabot.yml vendored Normal file
View File

@ -0,0 +1,24 @@
version: 2
updates:
- package-ecosystem: github-actions
directory: /
schedule:
interval: monthly
ignore:
# slsa depends on upload/download v3
- dependency-name: actions/upload-artifact
versions: '>= 4'
- dependency-name: actions/download-artifact
versions: '>= 4'
groups:
github-actions:
patterns:
- '*'
- package-ecosystem: pip
directory: /requirements/
schedule:
interval: monthly
groups:
python-requirements:
patterns:
- '*'

View File

@ -10,7 +10,6 @@ on:
permissions:
issues: write
pull-requests: write
discussions: write
concurrency:
group: lock
jobs:

View File

@ -1,25 +0,0 @@
name: pre-commit
on:
pull_request:
push:
branches: [main, stable]
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: astral-sh/setup-uv@f0ec1fc3b38f5e7cd731bb6ce540c5af426746bb # v6.1.0
with:
enable-cache: true
prune-cache: false
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
id: setup-python
with:
python-version-file: pyproject.toml
- uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
with:
path: ~/.cache/pre-commit
key: pre-commit|${{ hashFiles('pyproject.toml', '.pre-commit-config.yaml') }}
- run: uv run --locked --group pre-commit pre-commit run --show-diff-on-failure --color=always --all-files
- uses: pre-commit-ci/lite-action@5d6cc0eb514c891a40562a58a8e71576c5c7fb43 # v1.1.0
if: ${{ !cancelled() }}

View File

@ -1,39 +1,61 @@
name: Publish
on:
push:
tags: ['*']
tags:
- '*'
jobs:
build:
runs-on: ubuntu-latest
outputs:
hash: ${{ steps.hash.outputs.hash }}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: astral-sh/setup-uv@f0ec1fc3b38f5e7cd731bb6ce540c5af426746bb # v6.1.0
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
enable-cache: true
prune-cache: false
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version-file: pyproject.toml
python-version: '3.x'
cache: pip
cache-dependency-path: requirements*/*.txt
- run: pip install -r requirements/build.txt
# Use the commit date instead of the current date during the build.
- run: echo "SOURCE_DATE_EPOCH=$(git log -1 --pretty=%ct)" >> $GITHUB_ENV
- run: uv build
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
- run: python -m build
# Generate hashes used for provenance.
- name: generate hash
id: hash
run: cd dist && echo "hash=$(sha256sum * | base64 -w0)" >> $GITHUB_OUTPUT
- uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3.1.3
with:
path: ./dist
create-release:
provenance:
needs: [build]
permissions:
actions: read
id-token: write
contents: write
# Can't pin with hash due to how this workflow works.
uses: slsa-framework/slsa-github-generator/.github/workflows/generator_generic_slsa3.yml@v1.10.0
with:
base64-subjects: ${{ needs.build.outputs.hash }}
create-release:
# Upload the sdist, wheels, and provenance to a GitHub release. They remain
# available as build artifacts for a while as well.
needs: [provenance]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
- uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
- name: create release
run: >
gh release create --draft --repo ${{ github.repository }}
${{ github.ref_name }} artifact/*
${{ github.ref_name }}
*.intoto.jsonl/* artifact/*
env:
GH_TOKEN: ${{ github.token }}
publish-pypi:
needs: [build]
needs: [provenance]
# Wait for approval before attempting to upload to PyPI. This allows reviewing the
# files in the draft release.
environment:
name: publish
url: https://pypi.org/project/Jinja2/${{ github.ref_name }}
@ -41,7 +63,11 @@ jobs:
permissions:
id-token: write
steps:
- uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
- uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4
- uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
- uses: pypa/gh-action-pypi-publish@81e9d935c883d0b210363ab89cf05f3894778450 # v1.8.14
with:
repository-url: https://test.pypi.org/legacy/
packages-dir: artifact/
- uses: pypa/gh-action-pypi-publish@81e9d935c883d0b210363ab89cf05f3894778450 # v1.8.14
with:
packages-dir: artifact/

View File

@ -1,10 +1,18 @@
name: Tests
on:
pull_request:
paths-ignore: ['docs/**', 'README.md']
push:
branches: [main, stable]
paths-ignore: ['docs/**', 'README.md']
branches:
- main
- '*.x'
paths-ignore:
- 'docs/**'
- '*.md'
- '*.rst'
pull_request:
paths-ignore:
- 'docs/**'
- '*.md'
- '*.rst'
jobs:
tests:
name: ${{ matrix.name || matrix.python }}
@ -13,37 +21,38 @@ jobs:
fail-fast: false
matrix:
include:
- {python: '3.13'}
- {name: Windows, python: '3.13', os: windows-latest}
- {name: Mac, python: '3.13', os: macos-latest}
- {python: '3.12'}
- {name: Windows, python: '3.12', os: windows-latest}
- {name: Mac, python: '3.12', os: macos-latest}
- {python: '3.11'}
- {python: '3.10'}
- {name: PyPy, python: 'pypy-3.11', tox: pypy3.11}
- {python: '3.9'}
- {python: '3.8'}
- {python: '3.7'}
- {name: PyPy, python: 'pypy-3.10', tox: pypy310}
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: astral-sh/setup-uv@f0ec1fc3b38f5e7cd731bb6ce540c5af426746bb # v6.1.0
with:
enable-cache: true
prune-cache: false
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
python-version: ${{ matrix.python }}
- run: uv run --locked tox run -e ${{ matrix.tox || format('py{0}', matrix.python) }}
allow-prereleases: true
cache: pip
cache-dependency-path: requirements*/*.txt
- run: pip install tox
- run: tox run -e ${{ matrix.tox || format('py{0}', matrix.python) }}
typing:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
- uses: astral-sh/setup-uv@f0ec1fc3b38f5e7cd731bb6ce540c5af426746bb # v6.1.0
- uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
with:
enable-cache: true
prune-cache: false
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version-file: pyproject.toml
python-version: '3.x'
cache: pip
cache-dependency-path: requirements*/*.txt
- name: cache mypy
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
with:
path: ./.mypy_cache
key: mypy|${{ hashFiles('pyproject.toml') }}
- run: uv run --locked tox run -e typing
- run: pip install tox
- run: tox run -e typing

2
.gitignore vendored
View File

@ -1,5 +1,7 @@
.idea/
.vscode/
.venv*/
venv*/
__pycache__/
dist/
.coverage*

View File

@ -1,15 +1,13 @@
ci:
autoupdate_schedule: monthly
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 76e47323a83cd9795e4ff9a1de1c0d2eef610f17 # frozen: v0.11.11
rev: v0.4.1
hooks:
- id: ruff
- id: ruff-format
- repo: https://github.com/astral-sh/uv-pre-commit
rev: 648bdbfd6bb1a82f132ecc2c666e0d1b2e4b0d94 # frozen: 0.7.8
hooks:
- id: uv-lock
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
rev: v4.6.0
hooks:
- id: check-merge-conflict
- id: debug-statements

View File

@ -1,10 +1,13 @@
version: 2
build:
os: ubuntu-24.04
os: ubuntu-22.04
tools:
python: '3.13'
commands:
- asdf plugin add uv
- asdf install uv latest
- asdf global uv latest
- uv run --group docs sphinx-build -W -b dirhtml docs $READTHEDOCS_OUTPUT/html
python: '3.12'
python:
install:
- requirements: requirements/docs.txt
- method: pip
path: .
sphinx:
builder: dirhtml
fail_on_warning: true

View File

@ -1,79 +1,5 @@
.. currentmodule:: jinja2
Version 3.2.0
-------------
Unreleased
- Drop support for Python 3.7, 3.8, and 3.9.
- Update minimum MarkupSafe version to >= 3.0.
- Update minimum Babel version to >= 2.17.
- Deprecate the ``__version__`` attribute. Use feature detection or
``importlib.metadata.version("jinja2")`` instead.
- Use modern packaging metadata with ``pyproject.toml`` instead of ``setup.cfg``.
:pr:`1793`
- Use ``flit_core`` instead of ``setuptools`` as build backend.
Version 3.1.6
-------------
Released 2025-03-05
- The ``|attr`` filter does not bypass the environment's attribute lookup,
allowing the sandbox to apply its checks. :ghsa:`cpwx-vrp4-4pq7`
Version 3.1.5
-------------
Released 2024-12-21
- The sandboxed environment handles indirect calls to ``str.format``, such as
by passing a stored reference to a filter that calls its argument.
:ghsa:`q2x7-8rv6-6q7h`
- Escape template name before formatting it into error messages, to avoid
issues with names that contain f-string syntax.
:issue:`1792`, :ghsa:`gmj6-6f8f-6699`
- Sandbox does not allow ``clear`` and ``pop`` on known mutable sequence
types. :issue:`2032`
- Calling sync ``render`` for an async template uses ``asyncio.run``.
:pr:`1952`
- Avoid unclosed ``auto_aiter`` warnings. :pr:`1960`
- Return an ``aclose``-able ``AsyncGenerator`` from
``Template.generate_async``. :pr:`1960`
- Avoid leaving ``root_render_func()`` unclosed in
``Template.generate_async``. :pr:`1960`
- Avoid leaving async generators unclosed in blocks, includes and extends.
:pr:`1960`
- The runtime uses the correct ``concat`` function for the current environment
when calling block references. :issue:`1701`
- Make ``|unique`` async-aware, allowing it to be used after another
async-aware filter. :issue:`1781`
- ``|int`` filter handles ``OverflowError`` from scientific notation.
:issue:`1921`
- Make compiling deterministic for tuple unpacking in a ``{% set ... %}``
call. :issue:`2021`
- Fix dunder protocol (`copy`/`pickle`/etc) interaction with ``Undefined``
objects. :issue:`2025`
- Fix `copy`/`pickle` support for the internal ``missing`` object.
:issue:`2027`
- ``Environment.overlay(enable_async)`` is applied correctly. :pr:`2061`
- The error message from ``FileSystemLoader`` includes the paths that were
searched. :issue:`1661`
- ``PackageLoader`` shows a clearer error message when the package does not
contain the templates directory. :issue:`1705`
- Improve annotations for methods returning copies. :pr:`1880`
- ``urlize`` does not add ``mailto:`` to values like `@a@b`. :pr:`1870`
- Tests decorated with `@pass_context`` can be used with the ``|select``
filter. :issue:`1624`
- Using ``set`` for multiple assignment (``a, b = 1, 2``) does not fail when the
target is a namespace attribute. :issue:`1413`
- Using ``set`` in all branches of ``{% if %}{% elif %}{% else %}`` blocks
does not cause the variable to be considered initially undefined.
:issue:`1253`
Version 3.1.4
-------------
@ -206,8 +132,9 @@ Released 2021-05-18
extensions shows more relevant context. :issue:`1429`
- Fixed calling deprecated ``jinja2.Markup`` without an argument.
Use ``markupsafe.Markup`` instead. :issue:`1438`
- Calling sync ``render`` for an async template uses ``asyncio.new_event_loop``
This fixes a deprecation that Python 3.10 introduces. :issue:`1443`
- Calling sync ``render`` for an async template uses ``asyncio.run``
on Python >= 3.7. This fixes a deprecation that Python 3.10
introduces. :issue:`1443`
Version 3.0.0
@ -1059,7 +986,7 @@ Released 2008-07-17, codename Jinjavitus
evaluates to ``false``.
- Improved error reporting for undefined values by providing a
position.
- ``filesizeformat`` filter uses decimal prefixes now by default and
- ``filesizeformat`` filter uses decimal prefixes now per default and
can be set to binary mode with the second parameter.
- Fixed bug in finalizer

222
CONTRIBUTING.rst Normal file
View File

@ -0,0 +1,222 @@
How to contribute to Jinja
==========================
Thank you for considering contributing to Jinja!
Support questions
-----------------
Please don't use the issue tracker for this. The issue tracker is a
tool to address bugs and feature requests in Jinja itself. Use one of
the following resources for questions about using Jinja or issues with
your own code:
- The ``#get-help`` channel on our Discord chat:
https://discord.gg/pallets
- The mailing list flask@python.org for long term discussion or larger
issues.
- Ask on `Stack Overflow`_. Search with Google first using:
``site:stackoverflow.com jinja {search term, exception message, etc.}``
.. _Stack Overflow: https://stackoverflow.com/questions/tagged/jinja?tab=Frequent
Reporting issues
----------------
Include the following information in your post:
- Describe what you expected to happen.
- If possible, include a `minimal reproducible example`_ to help us
identify the issue. This also helps check that the issue is not with
your own code.
- Describe what actually happened. Include the full traceback if there
was an exception.
- List your Python and Jinja versions. If possible, check if this
issue is already fixed in the latest releases or the latest code in
the repository.
.. _minimal reproducible example: https://stackoverflow.com/help/minimal-reproducible-example
Submitting patches
------------------
If there is not an open issue for what you want to submit, prefer
opening one for discussion before working on a PR. You can work on any
issue that doesn't have an open PR linked to it or a maintainer assigned
to it. These show up in the sidebar. No need to ask if you can work on
an issue that interests you.
Include the following in your patch:
- Use `Black`_ to format your code. This and other tools will run
automatically if you install `pre-commit`_ using the instructions
below.
- Include tests if your patch adds or changes code. Make sure the test
fails without your patch.
- Update any relevant docs pages and docstrings. Docs pages and
docstrings should be wrapped at 72 characters.
- Add an entry in ``CHANGES.rst``. Use the same style as other
entries. Also include ``.. versionchanged::`` inline changelogs in
relevant docstrings.
.. _Black: https://black.readthedocs.io
.. _pre-commit: https://pre-commit.com
First time setup
~~~~~~~~~~~~~~~~
- Download and install the `latest version of git`_.
- Configure git with your `username`_ and `email`_.
.. code-block:: text
$ git config --global user.name 'your name'
$ git config --global user.email 'your email'
- Make sure you have a `GitHub account`_.
- Fork Jinja to your GitHub account by clicking the `Fork`_ button.
- `Clone`_ the main repository locally.
.. code-block:: text
$ git clone https://github.com/pallets/jinja
$ cd jinja
- Add your fork as a remote to push your work to. Replace
``{username}`` with your username. This names the remote "fork", the
default Pallets remote is "origin".
.. code-block:: text
$ git remote add fork https://github.com/{username}/jinja
- Create a virtualenv.
.. code-block:: text
$ python3 -m venv env
$ . env/bin/activate
On Windows, activating is different.
.. code-block:: text
> env\Scripts\activate
- Upgrade pip and setuptools.
.. code-block:: text
$ python -m pip install --upgrade pip setuptools
- Install the development dependencies, then install Jinja in editable
mode.
.. code-block:: text
$ pip install -r requirements/dev.txt && pip install -e .
- Install the pre-commit hooks.
.. code-block:: text
$ pre-commit install
.. _latest version of git: https://git-scm.com/downloads
.. _username: https://docs.github.com/en/github/using-git/setting-your-username-in-git
.. _email: https://docs.github.com/en/github/setting-up-and-managing-your-github-user-account/setting-your-commit-email-address
.. _GitHub account: https://github.com/join
.. _Fork: https://github.com/pallets/jinja/fork
.. _Clone: https://docs.github.com/en/github/getting-started-with-github/fork-a-repo#step-2-create-a-local-clone-of-your-fork
Start coding
~~~~~~~~~~~~
- Create a branch to identify the issue you would like to work on. If
you're submitting a bug or documentation fix, branch off of the
latest ".x" branch.
.. code-block:: text
$ git fetch origin
$ git checkout -b your-branch-name origin/3.0.x
If you're submitting a feature addition or change, branch off of the
"main" branch.
.. code-block:: text
$ git fetch origin
$ git checkout -b your-branch-name origin/main
- Using your favorite editor, make your changes,
`committing as you go`_.
- Include tests that cover any code changes you make. Make sure the
test fails without your patch. Run the tests as described below.
- Push your commits to your fork on GitHub and
`create a pull request`_. Link to the issue being addressed with
``fixes #123`` in the pull request.
.. code-block:: text
$ git push --set-upstream fork your-branch-name
.. _committing as you go: https://dont-be-afraid-to-commit.readthedocs.io/en/latest/git/commandlinegit.html#commit-your-changes
.. _create a pull request: https://docs.github.com/en/github/collaborating-with-issues-and-pull-requests/creating-a-pull-request
Running the tests
~~~~~~~~~~~~~~~~~
Run the basic test suite with pytest.
.. code-block:: text
$ pytest
This runs the tests for the current environment, which is usually
sufficient. CI will run the full suite when you submit your pull
request. You can run the full test suite with tox if you don't want to
wait.
.. code-block:: text
$ tox
Running test coverage
~~~~~~~~~~~~~~~~~~~~~
Generating a report of lines that do not have test coverage can indicate
where to start contributing. Run ``pytest`` using ``coverage`` and
generate a report.
.. code-block:: text
$ pip install coverage
$ coverage run -m pytest
$ coverage html
Open ``htmlcov/index.html`` in your browser to explore the report.
Read more about `coverage <https://coverage.readthedocs.io>`__.
Building the docs
~~~~~~~~~~~~~~~~~
Build the docs in the ``docs`` directory using Sphinx.
.. code-block:: text
$ cd docs
$ make html
Open ``_build/html/index.html`` in your browser to view the docs.
Read more about `Sphinx <https://www.sphinx-doc.org/en/stable/>`__.

View File

@ -1,5 +1,3 @@
<div align="center"><img src="https://raw.githubusercontent.com/pallets/jinja/refs/heads/stable/docs/_static/jinja-name.svg" alt="" height="150"></div>
# Jinja
Jinja is a fast, expressive, extensible templating engine. Special
@ -29,17 +27,18 @@ restricting functionality too much.
## In A Nutshell
```jinja
{% extends "base.html" %}
{% block title %}Members{% endblock %}
{% block content %}
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
{% endblock %}
```
.. code-block:: jinja
{% extends "base.html" %}
{% block title %}Members{% endblock %}
{% block content %}
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
{% endblock %}
## Donate
@ -49,11 +48,3 @@ allow the maintainers to devote more time to the projects, [please
donate today][].
[please donate today]: https://palletsprojects.com/donate
## Contributing
See our [detailed contributing documentation][contrib] for many ways to
contribute, including reporting issues, requesting features, asking or answering
questions, and making PRs.
[contrib]: https://palletsprojects.com/contributing/

132
artwork/jinjalogo.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" viewBox="0 0 500 500" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
<rect id="Icon" x="0" y="0" width="500" height="500" style="fill:none;"/>
<clipPath id="_clip1">
<rect x="0" y="0" width="500" height="500"/>
</clipPath>
<g clip-path="url(#_clip1)">
<path d="M491.941,72.796l-1.81,-0c-88.724,29.526 -204.909,29.526 -237.199,28.989l-29.877,-0.536c-88.119,-3.222 -133.085,-37.043 -211.849,-72.206c-5.432,-2.416 -11.77,1.61 -11.166,7.247c0.604,19.327 5.734,100.39 66.392,121.596c2.112,0.805 4.526,1.61 6.639,2.147c3.018,0.537 4.225,2.953 4.828,4.563l5.131,15.837c0.905,3.758 3.621,6.979 6.639,6.979l5.13,0c4.527,0 8.148,3.221 8.45,7.248l-0,15.3c-0,1.61 -1.509,2.953 -3.32,2.953l-38.929,-0c-3.622,-0 -6.64,2.684 -6.64,5.905l0,23.89c0,3.221 3.018,5.905 6.64,5.905l38.929,-0c1.811,-0 3.32,1.342 3.32,2.953l-0,6.442c-0,1.61 -1.509,2.952 -3.32,2.952l-38.929,0c-3.622,0.269 -6.338,2.685 -6.338,5.906l0,23.889c0,2.685 2.414,5.637 5.13,5.637l40.439,0c1.811,0 3.32,1.342 3.32,2.953l-0,157.027c-0,8.053 7.544,14.764 16.597,14.764l27.462,-0c9.054,-0 16.598,-6.711 16.598,-14.764l0,-157.027c0,-1.611 1.509,-2.953 3.32,-2.953l169.6,-0.268c1.811,-0 3.32,1.342 3.32,2.952l-0,157.833c-0,8.053 7.544,14.764 16.598,14.764l27.462,-0c9.053,-0 16.598,-6.711 16.598,-14.764l-0,-158.101c-0,-1.611 1.508,-2.953 3.319,-2.953c0,0 42.249,-0.268 42.853,-0.537c1.811,-1.073 3.018,-2.952 3.018,-5.1l-0,-23.621c-0,-3.221 -3.018,-5.905 -6.941,-5.905l-41.948,-0l0,-0.269l-0.301,0l-0,-8.857c-0,-1.611 1.508,-2.953 3.319,-2.953l38.93,-0c3.621,-0 6.639,-2.684 6.639,-5.905l-0,-23.89c-0,-3.221 -3.018,-5.905 -6.639,-5.905l-38.93,-0c-1.811,-0 -3.319,-1.343 -3.319,-2.953l-0,-15.3c-0,-3.758 3.621,-7.248 8.449,-7.248l5.131,0c3.621,0 5.733,-3.489 6.639,-6.979l5.13,-15.837c0.604,-2.147 2.716,-4.026 5.13,-4.831c38.93,-8.59 54.924,-34.09 68.203,-74.085l-0,-0.268c1.508,-10.2 -5.432,-12.079 -7.847,-12.616Zm-150.89,114.08l0,23.352c0,3.221 -2.112,5.637 -4.828,5.637l-54.321,0c-2.716,0 -4.828,-2.416 -4.828,-5.637l-0,-23.352c-0,-2.953 2.112,-5.637 4.828,-5.637l54.321,-0c2.414,0.268 4.828,2.684 4.828,5.637Zm-111.96,-0l-0,23.352c-0,2.953 -2.112,5.637 -4.828,5.637l-54.321,0c-2.716,0 -4.828,-2.416 -4.828,-5.637l-0,-23.352c-0,-2.953 2.112,-5.637 4.828,-5.637l54.321,-0c2.414,0.268 4.828,2.684 4.828,5.637Z" style="fill:#7e0c1b;fill-rule:nonzero;"/>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 2.7 KiB

BIN
docs/_static/jinja-logo-sidebar.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

BIN
docs/_static/jinja-logo.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" viewBox="0 0 500 500" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
<rect id="Logo" x="0" y="0" width="500" height="500" style="fill:none;"/>
<path id="Box" d="M500,50l0,400c0,27.596 -22.404,50 -50,50l-400,0c-27.596,0 -50,-22.404 -50,-50l0,-400c0,-27.596 22.404,-50 50,-50l400,0c27.596,0 50,22.404 50,50Z" style="fill:url(#_Linear1);"/>
<path id="Shadow" d="M500,246.897l0,203.103c0,27.596 -22.404,50 -50,50l-164.98,0l-119.852,-119.852c1.802,1.562 4.252,2.533 6.921,2.533l16.477,0c5.432,0 9.959,-4.026 9.959,-8.858l-0,-94.216c-0,-0.966 0.905,-1.772 1.992,-1.772l101.76,-0.161c1.086,0 1.992,0.806 1.992,1.772l-0,94.7c-0,4.831 4.526,8.858 9.958,8.858l16.478,-0c5.432,-0 9.958,-4.027 9.958,-8.858l0,-94.861c0,-0.967 0.906,-1.772 1.992,-1.772c0,0 25.35,-0.161 25.712,-0.322c1.086,-0.644 1.81,-1.771 1.81,-3.06l0,-14.173c0,-1.932 -1.81,-3.543 -4.164,-3.543l-25.169,0l0,-0.161l-0.181,0l0,-5.315c0,-0.966 0.906,-1.771 1.992,-1.771l23.358,-0c2.173,-0 3.983,-1.611 3.983,-3.543l0,-14.334c0,-1.933 -1.81,-3.543 -3.983,-3.543l-23.358,-0c-1.086,-0 -1.992,-0.806 -1.992,-1.772l0,-9.18c0,-2.255 2.173,-4.349 5.07,-4.349l3.078,0c2.173,0 3.441,-2.093 3.984,-4.187l3.078,-9.502c0.362,-1.289 1.63,-2.416 3.078,-2.899c23.358,-5.154 32.955,-20.454 40.922,-44.451l-0,-0.161c0.422,-2.854 -0.258,-4.622 -1.252,-5.729l101.379,101.379Zm-375.729,-61.204c4.362,3.788 9.511,6.914 15.588,9.039c1.267,0.483 2.716,0.966 3.983,1.288c1.811,0.322 2.535,1.772 2.898,2.738l3.078,9.502c0.543,2.255 2.173,4.187 3.983,4.187l3.078,0c2.716,0 4.889,1.933 5.07,4.349l0,6.575l-37.678,-37.678Zm9.606,62.5c0.716,0.602 1.677,0.975 2.723,0.975l23.358,-0c1.086,-0 1.991,0.805 1.991,1.771l0,3.866c0,0.966 -0.905,1.771 -1.991,1.771l-17.698,0l-8.383,-8.383Zm0.033,28.751c0.543,0.537 1.236,0.891 1.965,0.891l24.264,0c1.086,0 1.991,0.806 1.991,1.772l0,25.557l-28.22,-28.22Zm170.721,-64.819l-0,14.012c-0,1.933 -1.268,3.382 -2.897,3.382l-32.593,0c-1.629,0 -2.897,-1.449 -2.897,-3.382l0,-14.012c0,-1.771 1.268,-3.382 2.897,-3.382l32.593,0c1.448,0.161 2.897,1.611 2.897,3.382Zm-67.176,0l-0,14.012c-0,1.772 -1.268,3.382 -2.897,3.382l-32.593,0c-1.629,0 -2.897,-1.449 -2.897,-3.382l0,-14.012c0,-1.771 1.268,-3.382 2.897,-3.382l32.593,0c1.448,0.161 2.897,1.611 2.897,3.382Z" style="fill:#630b28;"/>
<path id="Icon" d="M395.165,143.677l-1.087,0c-53.234,17.716 -122.945,17.716 -142.319,17.394l-17.926,-0.322c-52.872,-1.933 -79.851,-22.225 -127.109,-43.323c-3.26,-1.45 -7.062,0.966 -6.7,4.348c0.362,11.596 3.44,60.234 39.835,72.958c1.267,0.483 2.716,0.966 3.983,1.288c1.811,0.322 2.535,1.772 2.898,2.738l3.078,9.502c0.543,2.255 2.173,4.187 3.983,4.187l3.078,0c2.716,0 4.889,1.933 5.07,4.349l0,9.18c0,0.966 -0.905,1.772 -1.991,1.772l-23.358,-0c-2.173,-0 -3.984,1.61 -3.984,3.543l0,14.334c0,1.932 1.811,3.543 3.984,3.543l23.358,-0c1.086,-0 1.991,0.805 1.991,1.771l0,3.866c0,0.966 -0.905,1.771 -1.991,1.771l-23.358,0c-2.173,0.161 -3.803,1.611 -3.803,3.543l0,14.334c0,1.611 1.449,3.382 3.078,3.382l24.264,0c1.086,0 1.991,0.806 1.991,1.772l0,94.216c0,4.832 4.527,8.858 9.959,8.858l16.477,0c5.432,0 9.959,-4.026 9.959,-8.858l-0,-94.216c-0,-0.966 0.905,-1.772 1.992,-1.772l101.76,-0.161c1.086,0 1.992,0.806 1.992,1.772l-0,94.7c-0,4.831 4.526,8.858 9.958,8.858l16.478,-0c5.432,-0 9.958,-4.027 9.958,-8.858l0,-94.861c0,-0.967 0.906,-1.772 1.992,-1.772c0,0 25.35,-0.161 25.712,-0.322c1.086,-0.644 1.81,-1.771 1.81,-3.06l0,-14.173c0,-1.932 -1.81,-3.543 -4.164,-3.543l-25.169,0l0,-0.161l-0.181,0l0,-5.315c0,-0.966 0.906,-1.771 1.992,-1.771l23.358,-0c2.173,-0 3.983,-1.611 3.983,-3.543l0,-14.334c0,-1.933 -1.81,-3.543 -3.983,-3.543l-23.358,-0c-1.086,-0 -1.992,-0.806 -1.992,-1.772l0,-9.18c0,-2.255 2.173,-4.349 5.07,-4.349l3.078,0c2.173,0 3.441,-2.093 3.984,-4.187l3.078,-9.502c0.362,-1.289 1.63,-2.416 3.078,-2.899c23.358,-5.154 32.955,-20.454 40.922,-44.451l-0,-0.161c0.905,-6.12 -3.26,-7.247 -4.708,-7.57Zm-90.534,68.448l-0,14.012c-0,1.933 -1.268,3.382 -2.897,3.382l-32.593,0c-1.629,0 -2.897,-1.449 -2.897,-3.382l0,-14.012c0,-1.771 1.268,-3.382 2.897,-3.382l32.593,0c1.448,0.161 2.897,1.611 2.897,3.382Zm-67.176,0l-0,14.012c-0,1.772 -1.268,3.382 -2.897,3.382l-32.593,0c-1.629,0 -2.897,-1.449 -2.897,-3.382l0,-14.012c0,-1.771 1.268,-3.382 2.897,-3.382l32.593,0c1.448,0.161 2.897,1.611 2.897,3.382Z" style="fill:#fff;fill-rule:nonzero;"/>
<defs>
<linearGradient id="_Linear1" x1="0" y1="0" x2="1" y2="0" gradientUnits="userSpaceOnUse" gradientTransform="matrix(3.06162e-14,500,-500,3.06162e-14,267.59,0)"><stop offset="0" style="stop-color:#f6cadc;stop-opacity:1"/><stop offset="1" style="stop-color:#7f0d18;stop-opacity:1"/></linearGradient>
</defs>
</svg>

Before

Width:  |  Height:  |  Size: 4.9 KiB

View File

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="100%" height="100%" viewBox="0 0 664 300" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
<g>
<path id="Name" d="M416,89.625l0,68.7c0,14.3 -2.95,24.375 -8.85,30.225c-5.9,5.85 -14.45,8.775 -25.65,8.775c-7.2,-0 -13.225,-0.95 -18.075,-2.85c-4.85,-1.9 -9.325,-4.9 -13.425,-9l11.7,-12.75c1.7,1.7 4.275,3.125 7.725,4.275c3.45,1.15 7.4,1.725 11.85,1.725c4.45,-0 7.875,-1.075 10.275,-3.225c2.4,-2.15 3.6,-6.175 3.6,-12.075l0,-73.8l20.85,-0Z" style="fill-rule:nonzero;"/>
<path d="M446.45,78.075c4.7,-0 7.75,0.725 9.15,2.175c1.4,1.45 2.1,4.5 2.1,9.15c0,4.65 -0.725,7.7 -2.175,9.15c-1.45,1.45 -4.5,2.175 -9.15,2.175c-4.65,-0 -7.7,-0.75 -9.15,-2.25c-1.45,-1.5 -2.175,-4.55 -2.175,-9.15c0,-4.6 0.725,-7.625 2.175,-9.075c1.45,-1.45 4.525,-2.175 9.225,-2.175Zm9.6,118.35l-19.5,-0l0,-82.5l19.5,-0l0,82.5Z" style="fill-rule:nonzero;"/>
<path d="M544.25,196.425l-19.5,-0l0,-54.75c0,-4.9 -1,-8.25 -3,-10.05c-2,-1.8 -4.8,-2.7 -8.4,-2.7l-17.55,7.35l0,60.15l-19.5,-0l0,-85.65l14.55,-0l4.5,10.2l24,-11.1c6.7,-0 12.525,2.475 17.475,7.425c4.95,4.95 7.425,12.175 7.425,21.675l0,57.45Z" style="fill-rule:nonzero;"/>
<path d="M583.25,113.925l0,85.65c0,6.1 -1.925,11.35 -5.775,15.75c-3.85,4.4 -9.525,6.6 -17.025,6.6l-11.1,-0l0,-16.5l7.5,-0c4.6,-0 6.9,-2.35 6.9,-7.05l0,-84.45l19.5,-0Zm-9.6,-35.85c4.7,-0 7.75,0.725 9.15,2.175c1.4,1.45 2.1,4.5 2.1,9.15c0,4.65 -0.725,7.7 -2.175,9.15c-1.45,1.45 -4.5,2.175 -9.15,2.175c-4.65,-0 -7.7,-0.75 -9.15,-2.25c-1.45,-1.5 -2.175,-4.55 -2.175,-9.15c0,-4.6 0.725,-7.625 2.175,-9.075c1.45,-1.45 4.525,-2.175 9.225,-2.175Z" style="fill-rule:nonzero;"/>
<path d="M663.95,196.425l-13.05,-0l-6,-10.35l-20.85,11.25c-11.6,-0 -19.4,-4.2 -23.4,-12.6c-2,-4.1 -3.375,-8.525 -4.125,-13.275c-0.75,-4.75 -1.125,-9.7 -1.125,-14.85c0,-5.15 0.05,-8.95 0.15,-11.4c0.1,-2.45 0.35,-5.3 0.75,-8.55c0.4,-3.25 0.975,-5.975 1.725,-8.175c0.75,-2.2 1.825,-4.475 3.225,-6.825c1.4,-2.35 3.1,-4.225 5.1,-5.625c4.5,-3.1 10.35,-4.65 17.55,-4.65l20.55,-0l19.5,-1.2l0,86.25Zm-19.5,-27.3l0,-40.2l-14.85,-0c-5.5,-0 -9.325,2.1 -11.475,6.3c-2.15,4.2 -3.225,10.475 -3.225,18.825c0,8.35 1.025,14.225 3.075,17.625c2.05,3.4 5.925,5.1 11.625,5.1l14.85,-7.65Z" style="fill-rule:nonzero;"/>
<g id="Logo">
<path id="Box" d="M300,30l-0,240c-0,16.557 -13.443,30 -30,30l-240,-0c-16.557,-0 -30,-13.443 -30,-30l0,-240c0,-16.557 13.443,-30 30,-30l240,0c16.557,0 30,13.443 30,30Z" style="fill:url(#_Linear1);"/>
<path id="Shadow" d="M300,148.138l0,121.862c0,16.557 -13.443,30 -30,30l-98.988,-0l-71.911,-71.911c1.081,0.937 2.551,1.52 4.152,1.52l9.887,-0c3.259,-0 5.975,-2.416 5.975,-5.315l-0,-56.53c-0,-0.58 0.543,-1.063 1.195,-1.063l61.056,-0.096c0.652,-0 1.195,0.483 1.195,1.063l0,56.819c0,2.899 2.716,5.315 5.975,5.315l9.887,0c3.259,0 5.975,-2.416 5.975,-5.315l-0,-56.916c-0,-0.58 0.543,-1.063 1.195,-1.063c0,-0 15.21,-0.097 15.427,-0.193c0.652,-0.387 1.086,-1.063 1.086,-1.836l0,-8.504c0,-1.16 -1.086,-2.126 -2.498,-2.126l-15.101,0l-0,-0.097l-0.109,0l-0,-3.188c-0,-0.58 0.543,-1.063 1.195,-1.063l14.015,-0c1.303,-0 2.39,-0.967 2.39,-2.126l-0,-8.601c-0,-1.159 -1.087,-2.125 -2.39,-2.125l-14.015,-0c-0.652,-0 -1.195,-0.484 -1.195,-1.063l-0,-5.508c-0,-1.353 1.304,-2.61 3.042,-2.61l1.847,0c1.304,0 2.064,-1.256 2.39,-2.512l1.847,-5.701c0.217,-0.773 0.978,-1.45 1.847,-1.74c14.014,-3.092 19.772,-12.272 24.553,-26.67l-0,-0.097c0.253,-1.712 -0.155,-2.773 -0.751,-3.437l60.827,60.827Zm-225.437,-36.722c2.617,2.273 5.706,4.148 9.352,5.423c0.761,0.29 1.63,0.58 2.39,0.773c1.087,0.193 1.521,1.063 1.739,1.643l1.847,5.701c0.326,1.353 1.303,2.512 2.39,2.512l1.847,0c1.629,0 2.933,1.16 3.042,2.61l-0,3.945l-22.607,-22.607Zm5.763,37.5c0.43,0.361 1.007,0.585 1.634,0.585l14.015,-0c0.651,-0 1.195,0.483 1.195,1.063l-0,2.319c-0,0.58 -0.544,1.063 -1.195,1.063l-10.619,-0l-5.03,-5.03Zm0.02,17.251c0.326,0.321 0.742,0.534 1.179,0.534l14.558,0c0.652,0 1.195,0.483 1.195,1.063l0,15.335l-16.932,-16.932Zm102.432,-38.892l0,8.407c0,1.16 -0.76,2.029 -1.738,2.029l-19.555,0c-0.978,0 -1.738,-0.869 -1.738,-2.029l-0,-8.407c-0,-1.063 0.76,-2.029 1.738,-2.029l19.555,-0c0.869,0.097 1.738,0.966 1.738,2.029Zm-40.305,0l-0,8.407c-0,1.063 -0.761,2.029 -1.738,2.029l-19.556,0c-0.978,0 -1.738,-0.869 -1.738,-2.029l-0,-8.407c-0,-1.063 0.76,-2.029 1.738,-2.029l19.556,-0c0.869,0.097 1.738,0.966 1.738,2.029Z" style="fill:#630b28;"/>
<path id="Icon" d="M237.099,86.206l-0.652,0c-31.94,10.63 -73.767,10.63 -85.392,10.437l-10.755,-0.194c-31.723,-1.159 -47.911,-13.335 -76.266,-25.994c-1.955,-0.869 -4.237,0.58 -4.02,2.609c0.218,6.958 2.065,36.141 23.901,43.775c0.761,0.29 1.63,0.58 2.39,0.773c1.087,0.193 1.521,1.063 1.739,1.643l1.847,5.701c0.326,1.353 1.303,2.512 2.39,2.512l1.847,0c1.629,0 2.933,1.16 3.042,2.61l-0,5.508c-0,0.579 -0.544,1.063 -1.195,1.063l-14.015,-0c-1.304,-0 -2.39,0.966 -2.39,2.125l-0,8.601c-0,1.159 1.086,2.126 2.39,2.126l14.015,-0c0.651,-0 1.195,0.483 1.195,1.063l-0,2.319c-0,0.58 -0.544,1.063 -1.195,1.063l-14.015,-0c-1.304,0.096 -2.282,0.966 -2.282,2.126l0,8.6c0,0.966 0.87,2.029 1.847,2.029l14.558,0c0.652,0 1.195,0.483 1.195,1.063l0,56.53c0,2.899 2.716,5.315 5.975,5.315l9.887,-0c3.259,-0 5.975,-2.416 5.975,-5.315l-0,-56.53c-0,-0.58 0.543,-1.063 1.195,-1.063l61.056,-0.096c0.652,-0 1.195,0.483 1.195,1.063l0,56.819c0,2.899 2.716,5.315 5.975,5.315l9.887,0c3.259,0 5.975,-2.416 5.975,-5.315l-0,-56.916c-0,-0.58 0.543,-1.063 1.195,-1.063c0,-0 15.21,-0.097 15.427,-0.193c0.652,-0.387 1.086,-1.063 1.086,-1.836l0,-8.504c0,-1.16 -1.086,-2.126 -2.498,-2.126l-15.101,0l-0,-0.097l-0.109,0l-0,-3.188c-0,-0.58 0.543,-1.063 1.195,-1.063l14.015,-0c1.303,-0 2.39,-0.967 2.39,-2.126l-0,-8.601c-0,-1.159 -1.087,-2.125 -2.39,-2.125l-14.015,-0c-0.652,-0 -1.195,-0.484 -1.195,-1.063l-0,-5.508c-0,-1.353 1.304,-2.61 3.042,-2.61l1.847,0c1.304,0 2.064,-1.256 2.39,-2.512l1.847,-5.701c0.217,-0.773 0.978,-1.45 1.847,-1.74c14.014,-3.092 19.772,-12.272 24.553,-26.67l-0,-0.097c0.543,-3.672 -1.956,-4.348 -2.825,-4.542Zm-54.321,41.069l0,8.407c0,1.16 -0.76,2.029 -1.738,2.029l-19.555,0c-0.978,0 -1.738,-0.869 -1.738,-2.029l-0,-8.407c-0,-1.063 0.76,-2.029 1.738,-2.029l19.555,-0c0.869,0.097 1.738,0.966 1.738,2.029Zm-40.305,0l-0,8.407c-0,1.063 -0.761,2.029 -1.738,2.029l-19.556,0c-0.978,0 -1.738,-0.869 -1.738,-2.029l-0,-8.407c-0,-1.063 0.76,-2.029 1.738,-2.029l19.556,-0c0.869,0.097 1.738,0.966 1.738,2.029Z" style="fill:#fff;fill-rule:nonzero;"/>
</g>
</g>
<defs>
<linearGradient id="_Linear1" x1="0" y1="0" x2="1" y2="0" gradientUnits="userSpaceOnUse" gradientTransform="matrix(1.83697e-14,300,-300,1.83697e-14,160.554,0)"><stop offset="0" style="stop-color:#f6cadc;stop-opacity:1"/><stop offset="1" style="stop-color:#7f0d18;stop-opacity:1"/></linearGradient>
</defs>
</svg>

Before

Width:  |  Height:  |  Size: 6.9 KiB

View File

@ -515,6 +515,9 @@ environment to compile different code behind the scenes in order to
handle async and sync code in an asyncio event loop. This has the
following implications:
- Template rendering requires an event loop to be available to the
current thread. :func:`asyncio.get_running_loop` must return an
event loop.
- The compiled code uses ``await`` for functions and attributes, and
uses ``async for`` loops. In order to support using both async and
sync functions in this context, a small wrapper is placed around
@ -666,8 +669,8 @@ Now it can be used in templates:
.. sourcecode:: jinja
{{ article.pub_date|datetime_format }}
{{ article.pub_date|datetime_format("%B %Y") }}
{{ article.pub_date|datetimeformat }}
{{ article.pub_date|datetimeformat("%B %Y") }}
Some decorators are available to tell Jinja to pass extra information to
the filter. The object is passed as the first argument, making the value
@ -697,10 +700,10 @@ enabled before escaping the input and marking the output safe.
br = Markup(br)
result = "\n\n".join(
f"<p>{br.join(p.splitlines())}</p>"
f"<p>{br.join(p.splitlines())}<\p>"
for p in re.split(r"(?:\r\n|\r(?!\n)|\n){2,}", value)
)
return Markup(result) if eval_ctx.autoescape else result
return Markup(result) if autoescape else result
.. _writing-tests:

View File

@ -24,7 +24,7 @@ autodoc_preserve_defaults = True
extlinks = {
"issue": ("https://github.com/pallets/jinja/issues/%s", "#%s"),
"pr": ("https://github.com/pallets/jinja/pull/%s", "#%s"),
"ghsa": ("https://github.com/pallets/jinja/security/advisories/GHSA-%s", "GHSA-%s"),
"ghsa": ("https://github.com/advisories/GHSA-%s", "GHSA-%s"),
}
intersphinx_mapping = {
"python": ("https://docs.python.org/3/", None),
@ -49,7 +49,7 @@ html_sidebars = {
}
singlehtml_sidebars = {"index": ["project.html", "localtoc.html", "ethicalads.html"]}
html_static_path = ["_static"]
html_favicon = "_static/jinja-icon.svg"
html_logo = "_static/jinja-logo.svg"
html_favicon = "_static/jinja-logo-sidebar.png"
html_logo = "_static/jinja-logo-sidebar.png"
html_title = f"Jinja Documentation ({version})"
html_show_sourcelink = False

View File

@ -39,10 +39,6 @@ After enabling, an application has to provide functions for ``gettext``,
globally or when rendering. A ``_()`` function is added as an alias to
the ``gettext`` function.
A convenient way to provide these functions is to call one of the below
methods depending on the translation system in use. If you do not require
actual translation, use ``Environment.install_null_translations`` to
install no-op functions.
Environment Methods
~~~~~~~~~~~~~~~~~~~

View File

@ -70,8 +70,6 @@ these document types.
While automatic escaping means that you are less likely have an XSS
problem, it also requires significant extra processing during compiling
and rendering, which can reduce performance. Jinja uses `MarkupSafe`_ for
and rendering, which can reduce performance. Jinja uses MarkupSafe for
escaping, which provides optimized C code for speed, but it still
introduces overhead to track escaping across methods and formatting.
.. _MarkupSafe: https://markupsafe.palletsprojects.com/

View File

@ -3,9 +3,9 @@
Jinja
=====
.. image:: _static/jinja-name.svg
.. image:: _static/jinja-logo.png
:align: center
:height: 200px
:target: https://palletsprojects.com/p/jinja/
Jinja is a fast, expressive, extensible templating engine. Special
placeholders in the template allow writing code similar to Python

View File

@ -30,7 +30,7 @@ Installation
------------
We recommend using the latest version of Python. Jinja supports Python
3.10 and newer. We also recommend using a `virtual environment`_ in order
3.7 and newer. We also recommend using a `virtual environment`_ in order
to isolate your project dependencies from other projects and the system.
.. _virtual environment: https://packaging.python.org/tutorials/installing-packages/#creating-virtual-environments

View File

@ -55,17 +55,6 @@ Foo
>>> print(result.value)
15
Sandboxed Native Environment
----------------------------
You can combine :class:`.SandboxedEnvironment` and :class:`NativeEnvironment` to
get both behaviors.
.. code-block:: python
class SandboxedNativeEnvironment(SandboxedEnvironment, NativeEnvironment):
pass
API
---

View File

@ -202,11 +202,10 @@ option can also be set to strip tabs and spaces from the beginning of a
line to the start of a block. (Nothing will be stripped if there are
other characters before the start of the block.)
With both ``trim_blocks`` and ``lstrip_blocks`` disabled (the default), block
tags on their own lines will be removed, but a blank line will remain and the
spaces in the content will be preserved. For example, this template:
.. code-block:: jinja
With both `trim_blocks` and `lstrip_blocks` enabled, you can put block tags
on their own lines, and the entire block line will be removed when
rendered, preserving the whitespace of the contents. For example,
without the `trim_blocks` and `lstrip_blocks` options, this template::
<div>
{% if True %}
@ -214,10 +213,7 @@ spaces in the content will be preserved. For example, this template:
{% endif %}
</div>
With both ``trim_blocks`` and ``lstrip_blocks`` disabled, the template is
rendered with blank lines inside the div:
.. code-block:: text
gets rendered with blank lines inside the div::
<div>
@ -225,10 +221,8 @@ rendered with blank lines inside the div:
</div>
With both ``trim_blocks`` and ``lstrip_blocks`` enabled, the template block
lines are completely removed:
.. code-block:: text
But with both `trim_blocks` and `lstrip_blocks` enabled, the template block
lines are removed and other whitespace is preserved::
<div>
yay
@ -528,8 +522,8 @@ However, the name after the `endblock` word must match the block name.
Block Nesting and Scope
~~~~~~~~~~~~~~~~~~~~~~~
Blocks can be nested for more complex layouts. By default, a block may not
access variables from outside the block (outer scopes)::
Blocks can be nested for more complex layouts. However, per default blocks
may not access variables from outer scopes::
{% for item in seq %}
<li>{% block loop_item %}{{ item }}{% endblock %}</li>
@ -1086,34 +1080,34 @@ Assignments use the `set` tag and can have multiple targets::
Block Assignments
~~~~~~~~~~~~~~~~~
It's possible to use `set` as a block to assign the content of the block to a
variable. This can be used to create multi-line strings, since Jinja doesn't
support Python's triple quotes (``"""``, ``'''``).
.. versionadded:: 2.8
Instead of using an equals sign and a value, you only write the variable name,
and everything until ``{% endset %}`` is captured.
Starting with Jinja 2.8, it's possible to also use block assignments to
capture the contents of a block into a variable name. This can be useful
in some situations as an alternative for macros. In that case, instead of
using an equals sign and a value, you just write the variable name and then
everything until ``{% endset %}`` is captured.
.. code-block:: jinja
Example::
{% set navigation %}
<li><a href="/">Index</a>
<li><a href="/downloads">Downloads</a>
{% endset %}
Filters applied to the variable name will be applied to the block's content.
The `navigation` variable then contains the navigation HTML source.
.. code-block:: jinja
.. versionchanged:: 2.10
Starting with Jinja 2.10, the block assignment supports filters.
Example::
{% set reply | wordwrap %}
You wrote:
{{ message }}
{% endset %}
.. versionadded:: 2.8
.. versionchanged:: 2.10
Block assignment supports filters.
.. _extends:
@ -1412,31 +1406,27 @@ Comparisons
Logic
~~~~~
For ``if`` statements, ``for`` filtering, and ``if`` expressions, it can be
useful to combine multiple expressions.
For ``if`` statements, ``for`` filtering, and ``if`` expressions, it can be useful to
combine multiple expressions:
``and``
For ``x and y``, if ``x`` is false, then the value is ``x``, else ``y``. In
a boolean context, this will be treated as ``True`` if both operands are
truthy.
Return true if the left and the right operand are true.
``or``
For ``x or y``, if ``x`` is true, then the value is ``x``, else ``y``. In a
boolean context, this will be treated as ``True`` if at least one operand is
truthy.
Return true if the left or the right operand are true.
``not``
For ``not x``, if ``x`` is false, then the value is ``True``, else
``False``.
Prefer negating ``is`` and ``in`` using their infix notation:
``foo is not bar`` instead of ``not foo is bar``; ``foo not in bar`` instead
of ``not foo in bar``. All other expressions require prefix notation:
``not (foo and bar).``
negate a statement (see below).
``(expr)``
Parentheses group an expression. This is used to change evaluation order, or
to make a long expression easier to read or less ambiguous.
Parentheses group an expression.
.. admonition:: Note
The ``is`` and ``in`` operators support negation using an infix notation,
too: ``foo is not bar`` and ``foo not in bar`` instead of ``not foo is bar``
and ``not foo in bar``. All other expressions require a prefix notation:
``not (foo and bar).``
Other Operators
@ -1678,9 +1668,6 @@ The following functions are available in the global scope by default:
.. versionadded:: 2.10
.. versionchanged:: 3.2
Namespace attributes can be assigned to in multiple assignment.
Extensions
----------
@ -1791,7 +1778,7 @@ It's possible to translate strings in expressions with these functions:
- ``_(message)``: Alias for ``gettext``.
- ``gettext(message)``: Translate a message.
- ``ngettext(singular, plural, n)``: Translate a singular or plural
- ``ngettext(singluar, plural, n)``: Translate a singular or plural
message based on a count variable.
- ``pgettext(context, message)``: Like ``gettext()``, but picks the
translation based on the context string.

View File

@ -21,7 +21,7 @@ for a neat trick.
Usually child templates extend from one template that adds a basic HTML
skeleton. However it's possible to put the `extends` tag into an `if` tag to
only extend from the layout template if the `standalone` variable evaluates
to false, which it does by default if it's not defined. Additionally a very
to false which it does per default if it's not defined. Additionally a very
basic skeleton is added to the file so that if it's indeed rendered with
`standalone` set to `True` a very basic HTML skeleton is added::

View File

@ -6,9 +6,9 @@ env = Environment(
{
"child.html": """\
{% extends default_layout or 'default.html' %}
{% import 'helpers.html' as helpers %}
{% include helpers = 'helpers.html' %}
{% macro get_the_answer() %}42{% endmacro %}
{% set title = 'Hello World' %}
{% title = 'Hello World' %}
{% block body %}
{{ get_the_answer() }}
{{ helpers.conspirate() }}

View File

@ -1,65 +1,36 @@
[project]
name = "Jinja2"
version = "3.2.0.dev"
description = "A very fast and expressive template engine."
readme = "README.md"
license = "BSD-3-Clause"
license-files = ["LICENSE.txt"]
license = {file = "LICENSE.txt"}
maintainers = [{name = "Pallets", email = "contact@palletsprojects.com"}]
classifiers = [
"Development Status :: 5 - Production/Stable",
"Environment :: Web Environment",
"Intended Audience :: Developers",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
"Topic :: Text Processing :: Markup :: HTML",
"Typing :: Typed",
]
requires-python = ">=3.10"
dependencies = ["MarkupSafe>=3.0"]
requires-python = ">=3.7"
dependencies = ["MarkupSafe>=2.0"]
dynamic = ["version"]
[project.urls]
Donate = "https://palletsprojects.com/donate"
Documentation = "https://jinja.palletsprojects.com/"
Changes = "https://jinja.palletsprojects.com/page/changes/"
Changes = "https://jinja.palletsprojects.com/changes/"
Source = "https://github.com/pallets/jinja/"
Chat = "https://discord.gg/pallets"
[project.optional-dependencies]
i18n = ["Babel>=2.17"]
i18n = ["Babel>=2.7"]
[dependency-groups]
dev = [
"ruff",
"tox",
"tox-uv",
]
docs = [
"pallets-sphinx-themes",
"sphinx",
"sphinxcontrib-log-cabinet",
]
docs-auto = [
"sphinx-autobuild",
]
gha-update = [
"gha-update ; python_full_version >= '3.12'",
]
pre-commit = [
"pre-commit",
"pre-commit-uv",
]
tests = [
"pytest",
"pytest-timeout",
"trio"
]
typing = [
"mypy",
"pyright",
"pytest",
]
[project.entry-points."babel.extractors"]
jinja2 = "jinja2.ext:babel_extract[i18n]"
[build-system]
requires = ["flit_core<4"]
@ -71,18 +42,15 @@ name = "jinja2"
[tool.flit.sdist]
include = [
"docs/",
"examples/",
"requirements/",
"tests/",
"CHANGES.rst",
"uv.lock"
"CHANGES.md",
"tox.ini",
]
exclude = [
"docs/_build/",
]
[tool.uv]
default-groups = ["dev", "pre-commit", "tests", "typing"]
[tool.pytest.ini_options]
testpaths = ["tests"]
filterwarnings = [
@ -96,24 +64,17 @@ source = ["jinja2", "tests"]
[tool.coverage.paths]
source = ["src", "*/site-packages"]
[tool.coverage.report]
exclude_also = [
"if t.TYPE_CHECKING",
"raise NotImplementedError",
": \\.{3}",
]
[tool.mypy]
python_version = "3.10"
files = ["src"]
python_version = "3.8"
files = ["src/jinja2"]
show_error_codes = true
pretty = true
strict = true
[tool.pyright]
pythonVersion = "3.10"
include = ["src"]
typeCheckingMode = "standard"
pythonVersion = "3.8"
include = ["src/jinja2"]
typeCheckingMode = "basic"
[tool.ruff]
src = ["src"]
@ -130,82 +91,8 @@ select = [
"UP", # pyupgrade
"W", # pycodestyle warning
]
ignore = [
"UP038", # keep isinstance tuple
]
ignore-init-module-imports = true
[tool.ruff.lint.isort]
force-single-line = true
order-by-type = false
[tool.gha-update]
tag-only = [
"slsa-framework/slsa-github-generator",
]
[tool.tox]
env_list = [
"py3.13", "py3.12", "py3.11", "py3.10",
"pypy3.11",
"style",
"typing",
"docs",
]
[tool.tox.env_run_base]
description = "pytest on latest dependency versions"
runner = "uv-venv-lock-runner"
package = "wheel"
wheel_build_env = ".pkg"
constrain_package_deps = true
use_frozen_constraints = true
dependency_groups = ["tests"]
commands = [[
"pytest", "-v", "--tb=short", "--basetemp={env_tmp_dir}",
{replace = "posargs", default = [], extend = true},
]]
[tool.tox.env.style]
description = "run all pre-commit hooks on all files"
dependency_groups = ["pre-commit"]
skip_install = true
commands = [["pre-commit", "run", "--all-files"]]
[tool.tox.env.typing]
description = "run static type checkers"
dependency_groups = ["typing"]
commands = [
["mypy"],
]
[tool.tox.env.docs]
description = "build docs"
dependency_groups = ["docs"]
commands = [["sphinx-build", "-E", "-W", "-b", "dirhtml", "docs", "docs/_build/dirhtml"]]
[tool.tox.env.docs-auto]
description = "continuously rebuild docs and start a local server"
dependency_groups = ["docs", "docs-auto"]
commands = [["sphinx-autobuild", "-W", "-b", "dirhtml", "--watch", "src", "docs", "docs/_build/dirhtml"]]
[tool.tox.env.update-actions]
description = "update GitHub Actions pins"
labels = ["update"]
dependency_groups = ["gha-update"]
skip_install = true
commands = [["gha-update"]]
[tool.tox.env.update-pre_commit]
description = "update pre-commit pins"
labels = ["update"]
dependency_groups = ["pre-commit"]
skip_install = true
commands = [["pre-commit", "autoupdate", "--freeze", "-j4"]]
[tool.tox.env.update-requirements]
description = "update uv lock"
labels = ["update"]
dependency_groups = []
no_default_groups = true
skip_install = true
commands = [["uv", "lock", {replace = "posargs", default = ["-U"], extend = true}]]

1
requirements/build.in Normal file
View File

@ -0,0 +1 @@
build

15
requirements/build.txt Normal file
View File

@ -0,0 +1,15 @@
# SHA1:80754af91bfb6d1073585b046fe0a474ce868509
#
# This file is autogenerated by pip-compile-multi
# To update, run:
#
# pip-compile-multi
#
build==0.10.0
# via -r requirements/build.in
packaging==23.1
# via build
pyproject-hooks==1.0.0
# via build
tomli==2.0.1
# via build

6
requirements/dev.in Normal file
View File

@ -0,0 +1,6 @@
-r docs.in
-r tests.in
-r typing.in
pip-compile-multi
pre-commit
tox

64
requirements/dev.txt Normal file
View File

@ -0,0 +1,64 @@
# SHA1:54b5b77ec8c7a0064ffa93b2fd16cb0130ba177c
#
# This file is autogenerated by pip-compile-multi
# To update, run:
#
# pip-compile-multi
#
-r docs.txt
-r tests.txt
-r typing.txt
build==0.10.0
# via pip-tools
cachetools==5.3.1
# via tox
cfgv==3.3.1
# via pre-commit
chardet==5.1.0
# via tox
click==8.1.3
# via
# pip-compile-multi
# pip-tools
colorama==0.4.6
# via tox
distlib==0.3.6
# via virtualenv
filelock==3.12.2
# via
# tox
# virtualenv
identify==2.5.24
# via pre-commit
nodeenv==1.8.0
# via pre-commit
pip-compile-multi==2.6.3
# via -r requirements/dev.in
pip-tools==6.13.0
# via pip-compile-multi
platformdirs==3.8.0
# via
# tox
# virtualenv
pre-commit==3.3.3
# via -r requirements/dev.in
pyproject-api==1.5.2
# via tox
pyproject-hooks==1.0.0
# via build
pyyaml==6.0.1
# via pre-commit
toposort==1.10
# via pip-compile-multi
tox==4.6.3
# via -r requirements/dev.in
virtualenv==20.23.1
# via
# pre-commit
# tox
wheel==0.40.0
# via pip-tools
# The following packages are considered to be unsafe in a requirements file:
# pip
# setuptools

4
requirements/docs.in Normal file
View File

@ -0,0 +1,4 @@
Pallets-Sphinx-Themes
Sphinx
sphinx-issues
sphinxcontrib-log-cabinet

61
requirements/docs.txt Normal file
View File

@ -0,0 +1,61 @@
# SHA1:45c590f97fe95b8bdc755eef796e91adf5fbe4ea
#
# This file is autogenerated by pip-compile-multi
# To update, run:
#
# pip-compile-multi
#
alabaster==0.7.13
# via sphinx
babel==2.12.1
# via sphinx
certifi==2023.5.7
# via requests
charset-normalizer==3.1.0
# via requests
docutils==0.20.1
# via sphinx
idna==3.4
# via requests
imagesize==1.4.1
# via sphinx
jinja2==3.1.2
# via sphinx
markupsafe==2.1.3
# via jinja2
packaging==23.1
# via
# pallets-sphinx-themes
# sphinx
pallets-sphinx-themes==2.1.1
# via -r requirements/docs.in
pygments==2.15.1
# via sphinx
requests==2.31.0
# via sphinx
snowballstemmer==2.2.0
# via sphinx
sphinx==7.0.1
# via
# -r requirements/docs.in
# pallets-sphinx-themes
# sphinx-issues
# sphinxcontrib-log-cabinet
sphinx-issues==3.0.1
# via -r requirements/docs.in
sphinxcontrib-applehelp==1.0.4
# via sphinx
sphinxcontrib-devhelp==1.0.2
# via sphinx
sphinxcontrib-htmlhelp==2.0.1
# via sphinx
sphinxcontrib-jsmath==1.0.1
# via sphinx
sphinxcontrib-log-cabinet==1.0.1
# via -r requirements/docs.in
sphinxcontrib-qthelp==1.0.3
# via sphinx
sphinxcontrib-serializinghtml==1.1.5
# via sphinx
urllib3==2.0.3
# via requests

1
requirements/tests.in Normal file
View File

@ -0,0 +1 @@
pytest

19
requirements/tests.txt Normal file
View File

@ -0,0 +1,19 @@
# SHA1:0eaa389e1fdb3a1917c0f987514bd561be5718ee
#
# This file is autogenerated by pip-compile-multi
# To update, run:
#
# pip-compile-multi
#
exceptiongroup==1.1.1
# via pytest
iniconfig==2.0.0
# via pytest
packaging==23.1
# via pytest
pluggy==1.2.0
# via pytest
pytest==7.4.0
# via -r requirements/tests.in
tomli==2.0.1
# via pytest

1
requirements/typing.in Normal file
View File

@ -0,0 +1 @@
mypy

15
requirements/typing.txt Normal file
View File

@ -0,0 +1,15 @@
# SHA1:7983aaa01d64547827c20395d77e248c41b2572f
#
# This file is autogenerated by pip-compile-multi
# To update, run:
#
# pip-compile-multi
#
mypy==1.4.1
# via -r requirements/typing.in
mypy-extensions==1.0.0
# via mypy
tomli==2.0.1
# via mypy
typing-extensions==4.6.3
# via mypy

View File

@ -30,8 +30,8 @@ def collapse_ranges(data):
Source: https://stackoverflow.com/a/4629241/400617
"""
for _, g in itertools.groupby(enumerate(data), lambda x: ord(x[1]) - x[0]):
lb = list(g)
yield lb[0][1], lb[-1][1]
b = list(g)
yield b[0][1], b[-1][1]
def build_pattern(ranges):
@ -54,16 +54,17 @@ def build_pattern(ranges):
def main():
"""Build the regex pattern and write it to ``jinja2/_identifier.py``."""
"""Build the regex pattern and write it to
``jinja2/_identifier.py``.
"""
pattern = build_pattern(collapse_ranges(get_characters()))
filename = os.path.abspath(
os.path.join(os.path.dirname(__file__), "..", "src", "jinja2", "_identifier.py")
)
with open(filename, "w", encoding="utf8") as f:
f.write("# generated by scripts/generate_identifier_pattern.py")
f.write(f"# Python {sys.version_info[0]}.{sys.version_info[1]}\n")
f.write("import re\n\n")
f.write("# generated by scripts/generate_identifier_pattern.py\n")
f.write("pattern = re.compile(\n")
f.write(f' r"[\\w{pattern}]+" # noqa: B950\n')
f.write(")\n")

View File

@ -3,10 +3,6 @@ non-XML syntax that supports inline expressions and an optional
sandboxed environment.
"""
from __future__ import annotations
import typing as t
from .bccache import BytecodeCache as BytecodeCache
from .bccache import FileSystemBytecodeCache as FileSystemBytecodeCache
from .bccache import MemcachedBytecodeCache as MemcachedBytecodeCache
@ -39,19 +35,4 @@ from .utils import pass_environment as pass_environment
from .utils import pass_eval_context as pass_eval_context
from .utils import select_autoescape as select_autoescape
def __getattr__(name: str) -> t.Any:
if name == "__version__":
import importlib.metadata
import warnings
warnings.warn(
"The `__version__` attribute is deprecated and will be removed in"
" Jinja 3.3. Use feature detection or"
' `importlib.metadata.version("jinja2")` instead.',
DeprecationWarning,
stacklevel=2,
)
return importlib.metadata.version("jinja2")
raise AttributeError(name)
__version__ = "3.1.4"

View File

@ -1,6 +1,6 @@
# generated by scripts/generate_identifier_pattern.py for Python 3.10
import re
# generated by scripts/generate_identifier_pattern.py
pattern = re.compile(
r"[\w·̀-ͯ·҃-֑҇-ׇֽֿׁׂׅׄؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ܑۭܰ-݊ަ-ް߫-߽߳ࠖ-࠙ࠛ-ࠣࠥ-ࠧࠩ-࡙࠭-࡛࣓-ࣣ࣡-ःऺ-़ा-ॏ॑-ॗॢॣঁ-ঃ়া-ৄেৈো-্ৗৢৣ৾ਁ-ਃ਼ਾ-ੂੇੈੋ-੍ੑੰੱੵઁ-ઃ઼ા-ૅે-ૉો-્ૢૣૺ-૿ଁ-ଃ଼ା-ୄେୈୋ-୍୕-ୗୢୣஂா-ூெ-ைொ-்ௗఀ-ఄా-ౄె-ైొ-్ౕౖౢౣಁ-ಃ಼ಾ-ೄೆ-ೈೊ-್ೕೖೢೣഀ-ഃ഻഼ാ-ൄെ-ൈൊ-്ൗൢൣඁ-ඃ්ා-ුූෘ-ෟෲෳัิ-ฺ็-๎ັິ-ຼ່-ໍ༹༘༙༵༷༾༿ཱ-྄྆྇ྍ-ྗྙ-ྼ࿆ါ-ှၖ-ၙၞ-ၠၢ-ၤၧ-ၭၱ-ၴႂ-ႍႏႚ-ႝ፝-፟ᜒ-᜔ᜲ-᜴ᝒᝓᝲᝳ឴-៓៝᠋-᠍ᢅᢆᢩᤠ-ᤫᤰ-᤻ᨗ-ᨛᩕ-ᩞ᩠-᩿᩼᪰-᪽ᪿᫀᬀ-ᬄ᬴-᭄᭫-᭳ᮀ-ᮂᮡ-ᮭ᯦-᯳ᰤ-᰷᳐-᳔᳒-᳨᳭᳴᳷-᳹᷀-᷹᷻-᷿‿⁀⁔⃐-⃥⃜⃡-⃰℘℮⳯-⵿⳱ⷠ-〪ⷿ-゙゚〯꙯ꙴ-꙽ꚞꚟ꛰꛱ꠂ꠆ꠋꠣ-ꠧꢀꢁꢴ-ꣅ꣠-꣱ꣿꤦ-꤭ꥇ-꥓ꦀ-ꦃ꦳-꧀ꧥꨩ-ꨶꩃꩌꩍꩻ-ꩽꪰꪲ-ꪴꪷꪸꪾ꪿꫁ꫫ-ꫯꫵ꫶ꯣ-ꯪ꯬꯭ﬞ︀-️︠-︯︳︴﹍-﹏_𐇽𐋠𐍶-𐍺𐨁-𐨃𐨅𐨆𐨌-𐨏𐨸-𐨿𐨺𐫦𐫥𐴤-𐽆𐴧𐺫𐺬-𐽐𑀀-𑀂𑀸-𑁆𑁿-𑂂𑂰-𑂺𑄀-𑄂𑄧-𑄴𑅅𑅆𑅳𑆀-𑆂𑆳-𑇀𑇉-𑇌𑇎𑇏𑈬-𑈷𑈾𑋟-𑋪𑌀-𑌃𑌻𑌼𑌾-𑍄𑍇𑍈𑍋-𑍍𑍗𑍢𑍣𑍦-𑍬𑍰-𑍴𑐵-𑑆𑑞𑒰-𑓃𑖯-𑖵𑖸-𑗀𑗜𑗝𑘰-𑙀𑚫-𑚷𑜝-𑜫𑠬-𑠺𑤰-𑤵𑤷𑤸𑤻-𑤾𑥀𑥂𑥃𑧑-𑧗𑧚-𑧠𑧤𑨁-𑨊𑨳-𑨹𑨻-𑨾𑩇𑩑-𑩛𑪊-𑪙𑰯-𑰶𑰸-𑰿𑲒-𑲧𑲩-𑲶𑴱-𑴶𑴺𑴼𑴽𑴿-𑵅𑵇𑶊-𑶎𑶐𑶑𑶓-𑶗𑻳-𑻶𖫰-𖫴𖬰-𖬶𖽏𖽑-𖾇𖾏-𖾒𖿤𖿰𖿱𛲝𛲞𝅥-𝅩𝅭-𝅲𝅻-𝆂𝆅-𝆋𝆪-𝆭𝉂-𝉄𝨀-𝨶𝨻-𝩬𝩵𝪄𝪛-𝪟𝪡-𝪯𞀀-𞀆𞀈-𞀘𞀛-𞀡𞀣𞀤𞀦-𞀪𞄰-𞄶𞋬-𞣐𞋯-𞣖𞥄-𞥊󠄀-󠇯]+" # noqa: B950
r"[\w·̀-ͯ·҃-֑҇-ׇֽֿׁׂׅׄؐ-ًؚ-ٰٟۖ-ۜ۟-۪ۤۧۨ-ܑۭܰ-݊ަ-ް߫-߽߳ࠖ-࠙ࠛ-ࠣࠥ-ࠧࠩ-࡙࠭-࡛࣓-ࣣ࣡-ःऺ-़ा-ॏ॑-ॗॢॣঁ-ঃ়া-ৄেৈো-্ৗৢৣ৾ਁ-ਃ਼ਾ-ੂੇੈੋ-੍ੑੰੱੵઁ-ઃ઼ા-ૅે-ૉો-્ૢૣૺ-૿ଁ-ଃ଼ା-ୄେୈୋ-୍ୗୢୣஂா-ூெ-ைொ-்ௗఀ-ఄా-ౄె-ైొ-్ౕౖౢౣಁ-ಃ಼ಾ-ೄೆ-ೈೊ-್ೕೖೢೣഀ-ഃ഻഼ാ-ൄെ-ൈൊ-്ൗൢൣඃ්ා-ුූෘ-ෟෲෳัิ-ฺ็-๎ັິ-ູົຼ່-ໍ༹༘༙༵༷༾༿ཱ-྄྆྇ྍ-ྗྙ-ྼ࿆ါ-ှၖ-ၙၞ-ၠၢ-ၤၧ-ၭၱ-ၴႂ-ႍႏႚ-ႝ፝-፟ᜒ-᜔ᜲ-᜴ᝒᝓᝲᝳ឴-៓៝᠋-᠍ᢅᢆᢩᤠ-ᤫᤰ-᤻ᨗ-ᨛᩕ-ᩞ᩠-᩿᩼᪰-᪽ᬀ-ᬄ᬴-᭄᭫-᭳ᮀ-ᮂᮡ-ᮭ᯦-᯳ᰤ-᰷᳐-᳔᳒-᳨᳭ᳲ-᳴᳷-᳹᷀-᷹᷻-᷿‿⁀⁔⃐-⃥⃜⃡-⃰℘℮⳯-⵿⳱ⷠ-〪ⷿ-゙゚〯꙯ꙴ-꙽ꚞꚟ꛰꛱ꠂ꠆ꠋꠣ-ꠧꢀꢁꢴ-ꣅ꣠-꣱ꣿꤦ-꤭ꥇ-꥓ꦀ-ꦃ꦳-꧀ꧥꨩ-ꨶꩃꩌꩍꩻ-ꩽꪰꪲ-ꪴꪷꪸꪾ꪿꫁ꫫ-ꫯꫵ꫶ꯣ-ꯪ꯬꯭ﬞ︀-️︠-︯︳︴﹍-﹏_𐇽𐋠𐍶-𐍺𐨁-𐨃𐨅𐨆𐨌-𐨏𐨸-𐨿𐨺𐫦𐫥𐴤-𐽆𐴧-𐽐𑀀-𑀂𑀸-𑁆𑁿-𑂂𑂰-𑂺𑄀-𑄂𑄧-𑄴𑅅𑅆𑅳𑆀-𑆂𑆳-𑇀𑇉-𑇌𑈬-𑈷𑈾𑋟-𑋪𑌀-𑌃𑌻𑌼𑌾-𑍄𑍇𑍈𑍋-𑍍𑍗𑍢𑍣𑍦-𑍬𑍰-𑍴𑐵-𑑆𑑞𑒰-𑓃𑖯-𑖵𑖸-𑗀𑗜𑗝𑘰-𑙀𑚫-𑚷𑜝-𑜫𑠬-𑠺𑨁-𑨊𑨳-𑨹𑨻-𑨾𑩇𑩑-𑩛𑪊-𑪙𑰯-𑰶𑰸-𑰿𑲒-𑲧𑲩-𑲶𑴱-𑴶𑴺𑴼𑴽𑴿-𑵅𑵇𑶊-𑶎𑶐𑶑𑶓-𑶗𑻳-𑻶𖫰-𖫴𖬰-𖬶𖽑-𖽾𖾏-𖾒𛲝𛲞𝅥-𝅩𝅭-𝅲𝅻-𝆂𝆅-𝆋𝆪-𝆭𝉂-𝉄𝨀-𝨶𝨻-𝩬𝩵𝪄𝪛-𝪟𝪡-𝪯𞀀-𞀆𞀈-𞀘𞀛-𞀡𞀣𞀤𞀦-𞣐𞀪-𞣖𞥄-𞥊󠄀-󠇯]+" # noqa: B950
)

View File

@ -6,9 +6,6 @@ from functools import wraps
from .utils import _PassArg
from .utils import pass_eval_context
if t.TYPE_CHECKING:
import typing_extensions as te
V = t.TypeVar("V")
@ -67,33 +64,21 @@ async def auto_await(value: t.Union[t.Awaitable["V"], "V"]) -> "V":
if inspect.isawaitable(value):
return await t.cast("t.Awaitable[V]", value)
return value
return t.cast("V", value)
class _IteratorToAsyncIterator(t.Generic[V]):
def __init__(self, iterator: "t.Iterator[V]"):
self._iterator = iterator
def __aiter__(self) -> "te.Self":
return self
async def __anext__(self) -> V:
try:
return next(self._iterator)
except StopIteration as e:
raise StopAsyncIteration(e.value) from e
def auto_aiter(
iterable: "t.AsyncIterable[V] | t.Iterable[V]",
async def auto_aiter(
iterable: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
) -> "t.AsyncIterator[V]":
if hasattr(iterable, "__aiter__"):
return iterable.__aiter__()
async for item in t.cast("t.AsyncIterable[V]", iterable):
yield item
else:
return _IteratorToAsyncIterator(iter(iterable))
for item in iterable:
yield item
async def auto_to_list(
value: "t.AsyncIterable[V] | t.Iterable[V]",
) -> list["V"]:
value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
) -> t.List["V"]:
return [x async for x in auto_aiter(value)]

View File

@ -27,7 +27,9 @@ if t.TYPE_CHECKING:
class _MemcachedClient(te.Protocol):
def get(self, key: str) -> bytes: ...
def set(self, key: str, value: bytes, timeout: int | None = None) -> None: ...
def set(
self, key: str, value: bytes, timeout: t.Optional[int] = None
) -> None: ...
bc_version = 5
@ -58,7 +60,7 @@ class Bucket:
def reset(self) -> None:
"""Resets the bucket (unloads the bytecode)."""
self.code: CodeType | None = None
self.code: t.Optional[CodeType] = None
def load_bytecode(self, f: t.BinaryIO) -> None:
"""Loads bytecode from a file or file like object."""
@ -147,7 +149,9 @@ class BytecodeCache:
by a particular environment.
"""
def get_cache_key(self, name: str, filename: str | None = None) -> str:
def get_cache_key(
self, name: str, filename: t.Optional[t.Union[str]] = None
) -> str:
"""Returns the unique hash key for this template name."""
hash = sha1(name.encode("utf-8"))
@ -164,7 +168,7 @@ class BytecodeCache:
self,
environment: "Environment",
name: str,
filename: str | None,
filename: t.Optional[str],
source: str,
) -> Bucket:
"""Return a cache bucket for the given template. All arguments are
@ -200,7 +204,7 @@ class FileSystemBytecodeCache(BytecodeCache):
"""
def __init__(
self, directory: str | None = None, pattern: str = "__jinja2_%s.cache"
self, directory: t.Optional[str] = None, pattern: str = "__jinja2_%s.cache"
) -> None:
if directory is None:
directory = self._get_default_cache_dir()
@ -373,7 +377,7 @@ class MemcachedBytecodeCache(BytecodeCache):
self,
client: "_MemcachedClient",
prefix: str = "jinja2/bytecode/",
timeout: int | None = None,
timeout: t.Optional[int] = None,
ignore_memcache_errors: bool = True,
):
self.client = client

View File

@ -55,7 +55,7 @@ def optimizeconst(f: F) -> F:
return f(self, node, frame, **kwargs)
return update_wrapper(new_func, f) # type: ignore[return-value]
return update_wrapper(t.cast(F, new_func), f)
def _make_binop(op: str) -> t.Callable[["CodeGenerator", nodes.BinExpr, "Frame"], None]:
@ -101,12 +101,12 @@ def _make_unop(
def generate(
node: nodes.Template,
environment: "Environment",
name: str | None,
filename: str | None,
stream: t.TextIO | None = None,
name: t.Optional[str],
filename: t.Optional[str],
stream: t.Optional[t.TextIO] = None,
defer_init: bool = False,
optimized: bool = True,
) -> str | None:
) -> t.Optional[str]:
"""Generate the python source for a node tree."""
if not isinstance(node, nodes.Template):
raise TypeError("Can't compile non template nodes")
@ -139,7 +139,9 @@ def has_safe_repr(value: t.Any) -> bool:
return False
def find_undeclared(nodes: t.Iterable[nodes.Node], names: t.Iterable[str]) -> set[str]:
def find_undeclared(
nodes: t.Iterable[nodes.Node], names: t.Iterable[str]
) -> t.Set[str]:
"""Check if the names passed are accessed undeclared. The return value
is a set of all the undeclared names from the sequence of names found.
"""
@ -153,7 +155,7 @@ def find_undeclared(nodes: t.Iterable[nodes.Node], names: t.Iterable[str]) -> se
class MacroRef:
def __init__(self, node: nodes.Macro | nodes.CallBlock) -> None:
def __init__(self, node: t.Union[nodes.Macro, nodes.CallBlock]) -> None:
self.node = node
self.accesses_caller = False
self.accesses_kwargs = False
@ -167,7 +169,7 @@ class Frame:
self,
eval_ctx: EvalContext,
parent: t.Optional["Frame"] = None,
level: int | None = None,
level: t.Optional[int] = None,
) -> None:
self.eval_ctx = eval_ctx
@ -185,10 +187,10 @@ class Frame:
# this for example affects {% filter %} or {% macro %}. If a frame
# is buffered this variable points to the name of the list used as
# buffer.
self.buffer: str | None = None
self.buffer: t.Optional[str] = None
# the name of the block we're in, otherwise None.
self.block: str | None = None
self.block: t.Optional[str] = None
else:
self.symbols = Symbols(parent.symbols, level=level)
@ -214,7 +216,7 @@ class Frame:
# or compile time.
self.soft_frame = False
def copy(self) -> "te.Self":
def copy(self) -> "Frame":
"""Create a copy of the current one."""
rv = object.__new__(self.__class__)
rv.__dict__.update(self.__dict__)
@ -227,7 +229,7 @@ class Frame:
return Frame(self.eval_ctx, level=self.symbols.level + 1)
return Frame(self.eval_ctx, self)
def soft(self) -> "te.Self":
def soft(self) -> "Frame":
"""Return a soft frame. A soft frame may not be modified as
standalone thing as it shares the resources with the frame it
was created of, but it's not a rootlevel frame any longer.
@ -251,8 +253,8 @@ class DependencyFinderVisitor(NodeVisitor):
"""A visitor that collects filter and test calls."""
def __init__(self) -> None:
self.filters: set[str] = set()
self.tests: set[str] = set()
self.filters: t.Set[str] = set()
self.tests: t.Set[str] = set()
def visit_Filter(self, node: nodes.Filter) -> None:
self.generic_visit(node)
@ -274,7 +276,7 @@ class UndeclaredNameVisitor(NodeVisitor):
def __init__(self, names: t.Iterable[str]) -> None:
self.names = set(names)
self.undeclared: set[str] = set()
self.undeclared: t.Set[str] = set()
def visit_Name(self, node: nodes.Name) -> None:
if node.ctx == "load" and node.name in self.names:
@ -299,9 +301,9 @@ class CodeGenerator(NodeVisitor):
def __init__(
self,
environment: "Environment",
name: str | None,
filename: str | None,
stream: t.TextIO | None = None,
name: t.Optional[str],
filename: t.Optional[str],
stream: t.Optional[t.TextIO] = None,
defer_init: bool = False,
optimized: bool = True,
) -> None:
@ -313,17 +315,17 @@ class CodeGenerator(NodeVisitor):
self.stream = stream
self.created_block_context = False
self.defer_init = defer_init
self.optimizer: Optimizer | None = None
self.optimizer: t.Optional[Optimizer] = None
if optimized:
self.optimizer = Optimizer(environment)
# aliases for imports
self.import_aliases: dict[str, str] = {}
self.import_aliases: t.Dict[str, str] = {}
# a registry for all blocks. Because blocks are moved out
# into the global python scope they are registered here
self.blocks: dict[str, nodes.Block] = {}
self.blocks: t.Dict[str, nodes.Block] = {}
# the number of extends statements so far
self.extends_so_far = 0
@ -337,12 +339,12 @@ class CodeGenerator(NodeVisitor):
self.code_lineno = 1
# registry of all filters and tests (global, not block local)
self.tests: dict[str, str] = {}
self.filters: dict[str, str] = {}
self.tests: t.Dict[str, str] = {}
self.filters: t.Dict[str, str] = {}
# the debug information
self.debug_info: list[tuple[int, int]] = []
self._write_debug_info: int | None = None
self.debug_info: t.List[t.Tuple[int, int]] = []
self._write_debug_info: t.Optional[int] = None
# the number of new lines before the next write()
self._new_lines = 0
@ -361,10 +363,10 @@ class CodeGenerator(NodeVisitor):
self._indentation = 0
# Tracks toplevel assignments
self._assign_stack: list[set[str]] = []
self._assign_stack: t.List[t.Set[str]] = []
# Tracks parameter definition blocks
self._param_def_block: list[set[str]] = []
self._param_def_block: t.List[t.Set[str]] = []
# Tracks the current context.
self._context_reference_stack = ["context"]
@ -417,7 +419,7 @@ class CodeGenerator(NodeVisitor):
"""Outdent by step."""
self._indentation -= step
def start_write(self, frame: Frame, node: nodes.Node | None = None) -> None:
def start_write(self, frame: Frame, node: t.Optional[nodes.Node] = None) -> None:
"""Yield or write into the frame buffer."""
if frame.buffer is None:
self.writeline("yield ", node)
@ -430,7 +432,7 @@ class CodeGenerator(NodeVisitor):
self.write(")")
def simple_write(
self, s: str, frame: Frame, node: nodes.Node | None = None
self, s: str, frame: Frame, node: t.Optional[nodes.Node] = None
) -> None:
"""Simple shortcut for start_write + write + end_write."""
self.start_write(frame, node)
@ -462,12 +464,14 @@ class CodeGenerator(NodeVisitor):
self._new_lines = 0
self.stream.write(x)
def writeline(self, x: str, node: nodes.Node | None = None, extra: int = 0) -> None:
def writeline(
self, x: str, node: t.Optional[nodes.Node] = None, extra: int = 0
) -> None:
"""Combination of newline and write."""
self.newline(node, extra)
self.write(x)
def newline(self, node: nodes.Node | None = None, extra: int = 0) -> None:
def newline(self, node: t.Optional[nodes.Node] = None, extra: int = 0) -> None:
"""Add one or more newlines before the next write."""
self._new_lines = max(self._new_lines, 1 + extra)
if node is not None and node.lineno != self._last_line:
@ -476,9 +480,9 @@ class CodeGenerator(NodeVisitor):
def signature(
self,
node: nodes.Call | nodes.Filter | nodes.Test,
node: t.Union[nodes.Call, nodes.Filter, nodes.Test],
frame: Frame,
extra_kwargs: t.Mapping[str, t.Any] | None = None,
extra_kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
) -> None:
"""Writes a function call to the stream for the current node.
A leading comma is added automatically. The extra keyword
@ -608,8 +612,8 @@ class CodeGenerator(NodeVisitor):
return f"{self.choose_async()}def {name}"
def macro_body(
self, node: nodes.Macro | nodes.CallBlock, frame: Frame
) -> tuple[Frame, MacroRef]:
self, node: t.Union[nodes.Macro, nodes.CallBlock], frame: Frame
) -> t.Tuple[Frame, MacroRef]:
"""Dump the function def of a macro or call block."""
frame = frame.inner()
frame.symbols.analyze_node(node)
@ -807,7 +811,7 @@ class CodeGenerator(NodeVisitor):
self.writeline("_block_vars.update({")
else:
self.writeline("context.vars.update({")
for idx, name in enumerate(sorted(vars)):
for idx, name in enumerate(vars):
if idx:
self.write(", ")
ref = frame.symbols.ref(name)
@ -817,12 +821,14 @@ class CodeGenerator(NodeVisitor):
if len(public_names) == 1:
self.writeline(f"context.exported_vars.add({public_names[0]!r})")
else:
names_str = ", ".join(map(repr, sorted(public_names)))
names_str = ", ".join(map(repr, public_names))
self.writeline(f"context.exported_vars.update(({names_str}))")
# -- Statement Visitors
def visit_Template(self, node: nodes.Template, frame: Frame | None = None) -> None:
def visit_Template(
self, node: nodes.Template, frame: t.Optional[Frame] = None
) -> None:
assert frame is None, "no root frame allowed"
eval_ctx = EvalContext(self.environment, self.name)
@ -896,15 +902,12 @@ class CodeGenerator(NodeVisitor):
if not self.environment.is_async:
self.writeline("yield from parent_template.root_render_func(context)")
else:
self.writeline("agen = parent_template.root_render_func(context)")
self.writeline("try:")
self.indent()
self.writeline("async for event in agen:")
self.writeline(
"async for event in parent_template.root_render_func(context):"
)
self.indent()
self.writeline("yield event")
self.outdent()
self.outdent()
self.writeline("finally: await agen.aclose()")
self.outdent(1 + (not self.has_known_extends))
# at this point we now have the blocks collected and can visit them too.
@ -974,20 +977,14 @@ class CodeGenerator(NodeVisitor):
f"yield from context.blocks[{node.name!r}][0]({context})", node
)
else:
self.writeline(f"gen = context.blocks[{node.name!r}][0]({context})")
self.writeline("try:")
self.indent()
self.writeline(
f"{self.choose_async()}for event in gen:",
f"{self.choose_async()}for event in"
f" context.blocks[{node.name!r}][0]({context}):",
node,
)
self.indent()
self.simple_write("event", frame)
self.outdent()
self.outdent()
self.writeline(
f"finally: {self.choose_async('await gen.aclose()', 'gen.close()')}"
)
self.outdent(level)
@ -1060,39 +1057,32 @@ class CodeGenerator(NodeVisitor):
self.writeline("else:")
self.indent()
def loop_body() -> None:
self.indent()
self.simple_write("event", frame)
self.outdent()
skip_event_yield = False
if node.with_context:
self.writeline(
f"gen = template.root_render_func("
f"{self.choose_async()}for event in template.root_render_func("
"template.new_context(context.get_all(), True,"
f" {self.dump_local_context(frame)}))"
)
self.writeline("try:")
self.indent()
self.writeline(f"{self.choose_async()}for event in gen:")
loop_body()
self.outdent()
self.writeline(
f"finally: {self.choose_async('await gen.aclose()', 'gen.close()')}"
f" {self.dump_local_context(frame)})):"
)
elif self.environment.is_async:
self.writeline(
"for event in (await template._get_default_module_async())"
"._body_stream:"
)
loop_body()
else:
self.writeline("yield from template._get_default_module()._body_stream")
skip_event_yield = True
if not skip_event_yield:
self.indent()
self.simple_write("event", frame)
self.outdent()
if node.ignore_missing:
self.outdent()
def _import_common(
self, node: nodes.Import | nodes.FromImport, frame: Frame
self, node: t.Union[nodes.Import, nodes.FromImport], frame: Frame
) -> None:
self.write(f"{self.choose_async('await ')}environment.get_template(")
self.visit(node.template, frame)
@ -1135,14 +1125,9 @@ class CodeGenerator(NodeVisitor):
)
self.writeline(f"if {frame.symbols.ref(alias)} is missing:")
self.indent()
# The position will contain the template name, and will be formatted
# into a string that will be compiled into an f-string. Curly braces
# in the name must be replaced with escapes so that they will not be
# executed as part of the f-string.
position = self.position(node).replace("{", "{{").replace("}", "}}")
message = (
"the template {included_template.__name__!r}"
f" (imported on {position})"
f" (imported on {self.position(node)})"
f" does not export the requested name {name!r}"
)
self.writeline(
@ -1365,7 +1350,7 @@ class CodeGenerator(NodeVisitor):
with_frame = frame.inner()
with_frame.symbols.analyze_node(node)
self.enter_frame(with_frame)
for target, expr in zip(node.targets, node.values, strict=False):
for target, expr in zip(node.targets, node.values):
self.newline()
self.visit(target, with_frame)
self.write(" = ")
@ -1378,8 +1363,8 @@ class CodeGenerator(NodeVisitor):
self.visit(node.node, frame)
class _FinalizeInfo(t.NamedTuple):
const: t.Callable[..., str] | None
src: str | None
const: t.Optional[t.Callable[..., str]]
src: t.Optional[str]
@staticmethod
def _default_finalize(value: t.Any) -> t.Any:
@ -1389,7 +1374,7 @@ class CodeGenerator(NodeVisitor):
"""
return str(value)
_finalize: _FinalizeInfo | None = None
_finalize: t.Optional[_FinalizeInfo] = None
def _make_finalize(self) -> _FinalizeInfo:
"""Build the finalize function to be used on constants and at
@ -1407,7 +1392,7 @@ class CodeGenerator(NodeVisitor):
if self._finalize is not None:
return self._finalize
finalize: t.Callable[..., t.Any] | None
finalize: t.Optional[t.Callable[..., t.Any]]
finalize = default = self._default_finalize
src = None
@ -1505,7 +1490,7 @@ class CodeGenerator(NodeVisitor):
self.indent()
finalize = self._make_finalize()
body: list[list[t.Any] | nodes.Expr] = []
body: t.List[t.Union[t.List[t.Any], nodes.Expr]] = []
# Evaluate constants at compile time if possible. Each item in
# body will be either a list of static data or a node to be
@ -1575,29 +1560,6 @@ class CodeGenerator(NodeVisitor):
def visit_Assign(self, node: nodes.Assign, frame: Frame) -> None:
self.push_assign_tracking()
# ``a.b`` is allowed for assignment, and is parsed as an NSRef. However,
# it is only valid if it references a Namespace object. Emit a check for
# that for each ref here, before assignment code is emitted. This can't
# be done in visit_NSRef as the ref could be in the middle of a tuple.
seen_refs: set[str] = set()
for nsref in node.find_all(nodes.NSRef):
if nsref.name in seen_refs:
# Only emit the check for each reference once, in case the same
# ref is used multiple times in a tuple, `ns.a, ns.b = c, d`.
continue
seen_refs.add(nsref.name)
ref = frame.symbols.ref(nsref.name)
self.writeline(f"if not isinstance({ref}, Namespace):")
self.indent()
self.writeline(
"raise TemplateRuntimeError"
'("cannot assign attribute on non-namespace object")'
)
self.outdent()
self.newline(node)
self.visit(node.target, frame)
self.write(" = ")
@ -1654,11 +1616,17 @@ class CodeGenerator(NodeVisitor):
self.write(ref)
def visit_NSRef(self, node: nodes.NSRef, frame: Frame) -> None:
# NSRef is a dotted assignment target a.b=c, but uses a[b]=c internally.
# visit_Assign emits code to validate that each ref is to a Namespace
# object only. That can't be emitted here as the ref could be in the
# middle of a tuple assignment.
# NSRefs can only be used to store values; since they use the normal
# `foo.bar` notation they will be parsed as a normal attribute access
# when used anywhere but in a `set` context
ref = frame.symbols.ref(node.name)
self.writeline(f"if not isinstance({ref}, Namespace):")
self.indent()
self.writeline(
"raise TemplateRuntimeError"
'("cannot assign attribute on non-namespace object")'
)
self.outdent()
self.writeline(f"{ref}[{node.attr!r}]")
def visit_Const(self, node: nodes.Const, frame: Frame) -> None:
@ -1787,7 +1755,7 @@ class CodeGenerator(NodeVisitor):
@contextmanager
def _filter_test_common(
self, node: nodes.Filter | nodes.Test, frame: Frame, is_filter: bool
self, node: t.Union[nodes.Filter, nodes.Test], frame: Frame, is_filter: bool
) -> t.Iterator[None]:
if self.environment.is_async:
self.write("(await auto_await(")

View File

@ -11,7 +11,7 @@ if t.TYPE_CHECKING:
from .runtime import Context
def rewrite_traceback_stack(source: str | None = None) -> BaseException:
def rewrite_traceback_stack(source: t.Optional[str] = None) -> BaseException:
"""Rewrite the current exception to replace any tracebacks from
within compiled template code with tracebacks that look like they
came from the template source.
@ -74,7 +74,7 @@ def rewrite_traceback_stack(source: str | None = None) -> BaseException:
def fake_traceback( # type: ignore
exc_value: BaseException, tb: TracebackType | None, filename: str, lineno: int
exc_value: BaseException, tb: t.Optional[TracebackType], filename: str, lineno: int
) -> TracebackType:
"""Produce a new traceback object that looks like it came from the
template source instead of the compiled code. The filename, line
@ -118,7 +118,26 @@ def fake_traceback( # type: ignore
elif function.startswith("block_"):
location = f"block {function[6:]!r}"
code = code.replace(co_name=location)
if sys.version_info >= (3, 8):
code = code.replace(co_name=location)
else:
code = CodeType(
code.co_argcount,
code.co_kwonlyargcount,
code.co_nlocals,
code.co_stacksize,
code.co_flags,
code.co_code,
code.co_consts,
code.co_names,
code.co_varnames,
code.co_filename,
location,
code.co_firstlineno,
code.co_lnotab,
code.co_freevars,
code.co_cellvars,
)
# Execute the new code, which is guaranteed to raise, and return
# the new traceback without this frame.
@ -128,15 +147,15 @@ def fake_traceback( # type: ignore
return sys.exc_info()[2].tb_next # type: ignore
def get_template_locals(real_locals: t.Mapping[str, t.Any]) -> dict[str, t.Any]:
def get_template_locals(real_locals: t.Mapping[str, t.Any]) -> t.Dict[str, t.Any]:
"""Based on the runtime locals, get the context that would be
available at that point in the template.
"""
# Start with the current template context.
ctx: Context | None = real_locals.get("context")
ctx: "t.Optional[Context]" = real_locals.get("context")
if ctx is not None:
data: dict[str, t.Any] = ctx.get_all().copy()
data: t.Dict[str, t.Any] = ctx.get_all().copy()
else:
data = {}
@ -144,7 +163,7 @@ def get_template_locals(real_locals: t.Mapping[str, t.Any]) -> dict[str, t.Any]:
# rather than pushing a context. Local variables follow the scheme
# l_depth_name. Find the highest-depth local that has a value for
# each name.
local_overrides: dict[str, tuple[int, t.Any]] = {}
local_overrides: t.Dict[str, t.Tuple[int, t.Any]] = {}
for name, value in real_locals.items():
if not name.startswith("l_") or value is missing:

View File

@ -17,8 +17,8 @@ VARIABLE_START_STRING = "{{"
VARIABLE_END_STRING = "}}"
COMMENT_START_STRING = "{#"
COMMENT_END_STRING = "#}"
LINE_STATEMENT_PREFIX: str | None = None
LINE_COMMENT_PREFIX: str | None = None
LINE_STATEMENT_PREFIX: t.Optional[str] = None
LINE_COMMENT_PREFIX: t.Optional[str] = None
TRIM_BLOCKS = False
LSTRIP_BLOCKS = False
NEWLINE_SEQUENCE: "te.Literal['\\n', '\\r\\n', '\\r']" = "\n"
@ -36,7 +36,7 @@ DEFAULT_NAMESPACE = {
}
# default policies
DEFAULT_POLICIES: dict[str, t.Any] = {
DEFAULT_POLICIES: t.Dict[str, t.Any] = {
"compiler.ascii_str": True,
"urlize.rel": "noopener",
"urlize.target": None,

View File

@ -7,7 +7,6 @@ import typing
import typing as t
import weakref
from collections import ChainMap
from contextlib import aclosing
from functools import lru_cache
from functools import partial
from functools import reduce
@ -67,7 +66,7 @@ _env_bound = t.TypeVar("_env_bound", bound="Environment")
# for direct template usage we have up to ten living environments
@lru_cache(maxsize=10)
def get_spontaneous_environment(cls: type[_env_bound], *args: t.Any) -> _env_bound:
def get_spontaneous_environment(cls: t.Type[_env_bound], *args: t.Any) -> _env_bound:
"""Return a new spontaneous environment. A spontaneous environment
is used for templates created directly rather than through an
existing environment.
@ -82,7 +81,7 @@ def get_spontaneous_environment(cls: type[_env_bound], *args: t.Any) -> _env_bou
def create_cache(
size: int,
) -> t.MutableMapping[tuple["weakref.ref[BaseLoader]", str], "Template"] | None:
) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]:
"""Return the cache class for the given size."""
if size == 0:
return None
@ -94,8 +93,8 @@ def create_cache(
def copy_cache(
cache: t.MutableMapping[tuple["weakref.ref[BaseLoader]", str], "Template"] | None,
) -> t.MutableMapping[tuple["weakref.ref[BaseLoader]", str], "Template"] | None:
cache: t.Optional[t.MutableMapping[t.Any, t.Any]],
) -> t.Optional[t.MutableMapping[t.Tuple["weakref.ref[t.Any]", str], "Template"]]:
"""Create an empty copy of the given cache."""
if cache is None:
return None
@ -108,8 +107,8 @@ def copy_cache(
def load_extensions(
environment: "Environment",
extensions: t.Sequence[str | type["Extension"]],
) -> dict[str, "Extension"]:
extensions: t.Sequence[t.Union[str, t.Type["Extension"]]],
) -> t.Dict[str, "Extension"]:
"""Load the extensions from the list and bind it to the environment.
Returns a dict of instantiated extensions.
"""
@ -117,18 +116,18 @@ def load_extensions(
for extension in extensions:
if isinstance(extension, str):
extension = t.cast(type["Extension"], import_string(extension))
extension = t.cast(t.Type["Extension"], import_string(extension))
result[extension.identifier] = extension(environment)
return result
def _environment_config_check(environment: _env_bound) -> _env_bound:
def _environment_config_check(environment: "Environment") -> "Environment":
"""Perform a sanity check on the environment."""
assert issubclass(environment.undefined, Undefined), (
"'undefined' must be a subclass of 'jinja2.Undefined'."
)
assert issubclass(
environment.undefined, Undefined
), "'undefined' must be a subclass of 'jinja2.Undefined'."
assert (
environment.block_start_string
!= environment.variable_start_string
@ -282,15 +281,15 @@ class Environment:
#: the class that is used for code generation. See
#: :class:`~jinja2.compiler.CodeGenerator` for more information.
code_generator_class: type["CodeGenerator"] = CodeGenerator
code_generator_class: t.Type["CodeGenerator"] = CodeGenerator
concat = "".join
#: the context class that is used for templates. See
#: :class:`~jinja2.runtime.Context` for more information.
context_class: type[Context] = Context
context_class: t.Type[Context] = Context
template_class: type["Template"]
template_class: t.Type["Template"]
def __init__(
self,
@ -300,17 +299,17 @@ class Environment:
variable_end_string: str = VARIABLE_END_STRING,
comment_start_string: str = COMMENT_START_STRING,
comment_end_string: str = COMMENT_END_STRING,
line_statement_prefix: str | None = LINE_STATEMENT_PREFIX,
line_comment_prefix: str | None = LINE_COMMENT_PREFIX,
line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX,
line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX,
trim_blocks: bool = TRIM_BLOCKS,
lstrip_blocks: bool = LSTRIP_BLOCKS,
newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE,
keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE,
extensions: t.Sequence[str | type["Extension"]] = (),
extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (),
optimized: bool = True,
undefined: type[Undefined] = Undefined,
finalize: t.Callable[..., t.Any] | None = None,
autoescape: bool | t.Callable[[str | None], bool] = False,
undefined: t.Type[Undefined] = Undefined,
finalize: t.Optional[t.Callable[..., t.Any]] = None,
autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False,
loader: t.Optional["BaseLoader"] = None,
cache_size: int = 400,
auto_reload: bool = True,
@ -343,7 +342,7 @@ class Environment:
self.keep_trailing_newline = keep_trailing_newline
# runtime information
self.undefined: type[Undefined] = undefined
self.undefined: t.Type[Undefined] = undefined
self.optimized = optimized
self.finalize = finalize
self.autoescape = autoescape
@ -368,7 +367,7 @@ class Environment:
self.is_async = enable_async
_environment_config_check(self)
def add_extension(self, extension: str | type["Extension"]) -> None:
def add_extension(self, extension: t.Union[str, t.Type["Extension"]]) -> None:
"""Adds an extension after the environment was created.
.. versionadded:: 2.5
@ -392,23 +391,23 @@ class Environment:
variable_end_string: str = missing,
comment_start_string: str = missing,
comment_end_string: str = missing,
line_statement_prefix: str | None = missing,
line_comment_prefix: str | None = missing,
line_statement_prefix: t.Optional[str] = missing,
line_comment_prefix: t.Optional[str] = missing,
trim_blocks: bool = missing,
lstrip_blocks: bool = missing,
newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = missing,
keep_trailing_newline: bool = missing,
extensions: t.Sequence[str | type["Extension"]] = missing,
extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = missing,
optimized: bool = missing,
undefined: type[Undefined] = missing,
finalize: t.Callable[..., t.Any] | None = missing,
autoescape: bool | t.Callable[[str | None], bool] = missing,
undefined: t.Type[Undefined] = missing,
finalize: t.Optional[t.Callable[..., t.Any]] = missing,
autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = missing,
loader: t.Optional["BaseLoader"] = missing,
cache_size: int = missing,
auto_reload: bool = missing,
bytecode_cache: t.Optional["BytecodeCache"] = missing,
enable_async: bool = missing,
) -> "te.Self":
enable_async: bool = False,
) -> "Environment":
"""Create a new overlay environment that shares all the data with the
current environment except for cache and the overridden attributes.
Extensions cannot be removed for an overlayed environment. An overlayed
@ -420,11 +419,8 @@ class Environment:
copied over so modifications on the original environment may not shine
through.
.. versionchanged:: 3.1.5
``enable_async`` is applied correctly.
.. versionchanged:: 3.1.2
Added the ``newline_sequence``, ``keep_trailing_newline``,
Added the ``newline_sequence``,, ``keep_trailing_newline``,
and ``enable_async`` parameters to match ``__init__``.
"""
args = dict(locals())
@ -464,7 +460,9 @@ class Environment:
"""Iterates over the extensions by priority."""
return iter(sorted(self.extensions.values(), key=lambda x: x.priority))
def getitem(self, obj: t.Any, argument: str | t.Any) -> t.Any | Undefined:
def getitem(
self, obj: t.Any, argument: t.Union[str, t.Any]
) -> t.Union[t.Any, Undefined]:
"""Get an item or attribute of an object but prefer the item."""
try:
return obj[argument]
@ -496,12 +494,12 @@ class Environment:
def _filter_test_common(
self,
name: str | Undefined,
name: t.Union[str, Undefined],
value: t.Any,
args: t.Sequence[t.Any] | None,
kwargs: t.Mapping[str, t.Any] | None,
context: Context | None,
eval_ctx: EvalContext | None,
args: t.Optional[t.Sequence[t.Any]],
kwargs: t.Optional[t.Mapping[str, t.Any]],
context: t.Optional[Context],
eval_ctx: t.Optional[EvalContext],
is_filter: bool,
) -> t.Any:
if is_filter:
@ -552,10 +550,10 @@ class Environment:
self,
name: str,
value: t.Any,
args: t.Sequence[t.Any] | None = None,
kwargs: t.Mapping[str, t.Any] | None = None,
context: Context | None = None,
eval_ctx: EvalContext | None = None,
args: t.Optional[t.Sequence[t.Any]] = None,
kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
context: t.Optional[Context] = None,
eval_ctx: t.Optional[EvalContext] = None,
) -> t.Any:
"""Invoke a filter on a value the same way the compiler does.
@ -573,10 +571,10 @@ class Environment:
self,
name: str,
value: t.Any,
args: t.Sequence[t.Any] | None = None,
kwargs: t.Mapping[str, t.Any] | None = None,
context: Context | None = None,
eval_ctx: EvalContext | None = None,
args: t.Optional[t.Sequence[t.Any]] = None,
kwargs: t.Optional[t.Mapping[str, t.Any]] = None,
context: t.Optional[Context] = None,
eval_ctx: t.Optional[EvalContext] = None,
) -> t.Any:
"""Invoke a test on a value the same way the compiler does.
@ -598,8 +596,8 @@ class Environment:
def parse(
self,
source: str,
name: str | None = None,
filename: str | None = None,
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
) -> nodes.Template:
"""Parse the sourcecode and return the abstract syntax tree. This
tree of nodes is used by the compiler to convert the template into
@ -615,7 +613,7 @@ class Environment:
self.handle_exception(source=source)
def _parse(
self, source: str, name: str | None, filename: str | None
self, source: str, name: t.Optional[str], filename: t.Optional[str]
) -> nodes.Template:
"""Internal parsing function used by `parse` and `compile`."""
return Parser(self, source, name, filename).parse()
@ -623,9 +621,9 @@ class Environment:
def lex(
self,
source: str,
name: str | None = None,
filename: str | None = None,
) -> t.Iterator[tuple[int, str, str]]:
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
) -> t.Iterator[t.Tuple[int, str, str]]:
"""Lex the given sourcecode and return a generator that yields
tokens as tuples in the form ``(lineno, token_type, value)``.
This can be useful for :ref:`extension development <writing-extensions>`
@ -644,8 +642,8 @@ class Environment:
def preprocess(
self,
source: str,
name: str | None = None,
filename: str | None = None,
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
) -> str:
"""Preprocesses the source with all extensions. This is automatically
called for all parsing and compiling methods but *not* for :meth:`lex`
@ -660,9 +658,9 @@ class Environment:
def _tokenize(
self,
source: str,
name: str | None,
filename: str | None = None,
state: str | None = None,
name: t.Optional[str],
filename: t.Optional[str] = None,
state: t.Optional[str] = None,
) -> TokenStream:
"""Called by the parser to do the preprocessing and filtering
for all the extensions. Returns a :class:`~jinja2.lexer.TokenStream`.
@ -681,8 +679,8 @@ class Environment:
def _generate(
self,
source: nodes.Template,
name: str | None,
filename: str | None,
name: t.Optional[str],
filename: t.Optional[str],
defer_init: bool = False,
) -> str:
"""Internal hook that can be overridden to hook a different generate
@ -708,11 +706,11 @@ class Environment:
return compile(source, filename, "exec")
@typing.overload
def compile(
def compile( # type: ignore
self,
source: str | nodes.Template,
name: str | None = None,
filename: str | None = None,
source: t.Union[str, nodes.Template],
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
raw: "te.Literal[False]" = False,
defer_init: bool = False,
) -> CodeType: ...
@ -720,9 +718,9 @@ class Environment:
@typing.overload
def compile(
self,
source: str | nodes.Template,
name: str | None = None,
filename: str | None = None,
source: t.Union[str, nodes.Template],
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
raw: "te.Literal[True]" = ...,
defer_init: bool = False,
) -> str: ...
@ -730,12 +728,12 @@ class Environment:
@internalcode
def compile(
self,
source: str | nodes.Template,
name: str | None = None,
filename: str | None = None,
source: t.Union[str, nodes.Template],
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
raw: bool = False,
defer_init: bool = False,
) -> str | CodeType:
) -> t.Union[str, CodeType]:
"""Compile a node or template source code. The `name` parameter is
the load name of the template after it was joined using
:meth:`join_path` if necessary, not the filename on the file system.
@ -817,10 +815,10 @@ class Environment:
def compile_templates(
self,
target: t.Union[str, "os.PathLike[str]"],
extensions: t.Collection[str] | None = None,
filter_func: t.Callable[[str], bool] | None = None,
zip: str | None = "deflated",
log_function: t.Callable[[str], None] | None = None,
extensions: t.Optional[t.Collection[str]] = None,
filter_func: t.Optional[t.Callable[[str], bool]] = None,
zip: t.Optional[str] = "deflated",
log_function: t.Optional[t.Callable[[str], None]] = None,
ignore_errors: bool = True,
) -> None:
"""Finds all the templates the loader can find, compiles them
@ -897,9 +895,9 @@ class Environment:
def list_templates(
self,
extensions: t.Collection[str] | None = None,
filter_func: t.Callable[[str], bool] | None = None,
) -> list[str]:
extensions: t.Optional[t.Collection[str]] = None,
filter_func: t.Optional[t.Callable[[str], bool]] = None,
) -> t.List[str]:
"""Returns a list of templates for this environment. This requires
that the loader supports the loader's
:meth:`~BaseLoader.list_templates` method.
@ -932,7 +930,7 @@ class Environment:
return names
def handle_exception(self, source: str | None = None) -> "te.NoReturn":
def handle_exception(self, source: t.Optional[str] = None) -> "te.NoReturn":
"""Exception handling helper. This is used internally to either raise
rewritten exceptions or return a rendered traceback for the template.
"""
@ -954,7 +952,7 @@ class Environment:
@internalcode
def _load_template(
self, name: str, globals: t.MutableMapping[str, t.Any] | None
self, name: str, globals: t.Optional[t.MutableMapping[str, t.Any]]
) -> "Template":
if self.loader is None:
raise TypeError("no loader for this environment specified")
@ -981,8 +979,8 @@ class Environment:
def get_template(
self,
name: t.Union[str, "Template"],
parent: str | None = None,
globals: t.MutableMapping[str, t.Any] | None = None,
parent: t.Optional[str] = None,
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
) -> "Template":
"""Load a template by name with :attr:`loader` and return a
:class:`Template`. If the template does not exist a
@ -1018,8 +1016,8 @@ class Environment:
def select_template(
self,
names: t.Iterable[t.Union[str, "Template"]],
parent: str | None = None,
globals: t.MutableMapping[str, t.Any] | None = None,
parent: t.Optional[str] = None,
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
) -> "Template":
"""Like :meth:`get_template`, but tries loading multiple names.
If none of the names can be loaded a :exc:`TemplatesNotFound`
@ -1071,9 +1069,11 @@ class Environment:
@internalcode
def get_or_select_template(
self,
template_name_or_list: t.Union[str, "Template", list[t.Union[str, "Template"]]],
parent: str | None = None,
globals: t.MutableMapping[str, t.Any] | None = None,
template_name_or_list: t.Union[
str, "Template", t.List[t.Union[str, "Template"]]
],
parent: t.Optional[str] = None,
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
) -> "Template":
"""Use :meth:`select_template` if an iterable of template names
is given, or :meth:`get_template` if one name is given.
@ -1088,9 +1088,9 @@ class Environment:
def from_string(
self,
source: str | nodes.Template,
globals: t.MutableMapping[str, t.Any] | None = None,
template_class: type["Template"] | None = None,
source: t.Union[str, nodes.Template],
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
template_class: t.Optional[t.Type["Template"]] = None,
) -> "Template":
"""Load a template from a source string without using
:attr:`loader`.
@ -1108,7 +1108,7 @@ class Environment:
return cls.from_code(self, self.compile(source), gs, None)
def make_globals(
self, d: t.MutableMapping[str, t.Any] | None
self, d: t.Optional[t.MutableMapping[str, t.Any]]
) -> t.MutableMapping[str, t.Any]:
"""Make the globals map for a template. Any given template
globals overlay the environment :attr:`globals`.
@ -1149,38 +1149,38 @@ class Template:
#: Type of environment to create when creating a template directly
#: rather than through an existing environment.
environment_class: type[Environment] = Environment
environment_class: t.Type[Environment] = Environment
environment: Environment
globals: t.MutableMapping[str, t.Any]
name: str | None
filename: str | None
blocks: dict[str, t.Callable[[Context], t.Iterator[str]]]
name: t.Optional[str]
filename: t.Optional[str]
blocks: t.Dict[str, t.Callable[[Context], t.Iterator[str]]]
root_render_func: t.Callable[[Context], t.Iterator[str]]
_module: t.Optional["TemplateModule"]
_debug_info: str
_uptodate: t.Callable[[], bool] | None
_uptodate: t.Optional[t.Callable[[], bool]]
def __new__(
cls,
source: str | nodes.Template,
source: t.Union[str, nodes.Template],
block_start_string: str = BLOCK_START_STRING,
block_end_string: str = BLOCK_END_STRING,
variable_start_string: str = VARIABLE_START_STRING,
variable_end_string: str = VARIABLE_END_STRING,
comment_start_string: str = COMMENT_START_STRING,
comment_end_string: str = COMMENT_END_STRING,
line_statement_prefix: str | None = LINE_STATEMENT_PREFIX,
line_comment_prefix: str | None = LINE_COMMENT_PREFIX,
line_statement_prefix: t.Optional[str] = LINE_STATEMENT_PREFIX,
line_comment_prefix: t.Optional[str] = LINE_COMMENT_PREFIX,
trim_blocks: bool = TRIM_BLOCKS,
lstrip_blocks: bool = LSTRIP_BLOCKS,
newline_sequence: "te.Literal['\\n', '\\r\\n', '\\r']" = NEWLINE_SEQUENCE,
keep_trailing_newline: bool = KEEP_TRAILING_NEWLINE,
extensions: t.Sequence[str | type["Extension"]] = (),
extensions: t.Sequence[t.Union[str, t.Type["Extension"]]] = (),
optimized: bool = True,
undefined: type[Undefined] = Undefined,
finalize: t.Callable[..., t.Any] | None = None,
autoescape: bool | t.Callable[[str | None], bool] = False,
undefined: t.Type[Undefined] = Undefined,
finalize: t.Optional[t.Callable[..., t.Any]] = None,
autoescape: t.Union[bool, t.Callable[[t.Optional[str]], bool]] = False,
enable_async: bool = False,
) -> t.Any: # it returns a `Template`, but this breaks the sphinx build...
env = get_spontaneous_environment(
@ -1216,7 +1216,7 @@ class Template:
environment: Environment,
code: CodeType,
globals: t.MutableMapping[str, t.Any],
uptodate: t.Callable[[], bool] | None = None,
uptodate: t.Optional[t.Callable[[], bool]] = None,
) -> "Template":
"""Creates a template object from compiled code and the globals. This
is used by the loaders and environment to create a template object.
@ -1248,7 +1248,7 @@ class Template:
namespace: t.MutableMapping[str, t.Any],
globals: t.MutableMapping[str, t.Any],
) -> "Template":
t: Template = object.__new__(cls)
t: "Template" = object.__new__(cls)
t.environment = environment
t.globals = globals
t.name = namespace["name"]
@ -1282,7 +1282,19 @@ class Template:
if self.environment.is_async:
import asyncio
return asyncio.run(self.render_async(*args, **kwargs))
close = False
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
close = True
try:
return loop.run_until_complete(self.render_async(*args, **kwargs))
finally:
if close:
loop.close()
ctx = self.new_context(dict(*args, **kwargs))
@ -1331,7 +1343,7 @@ class Template:
if self.environment.is_async:
import asyncio
async def to_list() -> list[str]:
async def to_list() -> t.List[str]:
return [x async for x in self.generate_async(*args, **kwargs)]
yield from asyncio.run(to_list())
@ -1346,7 +1358,7 @@ class Template:
async def generate_async(
self, *args: t.Any, **kwargs: t.Any
) -> t.AsyncGenerator[str, object]:
) -> t.AsyncIterator[str]:
"""An async version of :meth:`generate`. Works very similarly but
returns an async iterator instead.
"""
@ -1358,19 +1370,16 @@ class Template:
ctx = self.new_context(dict(*args, **kwargs))
try:
agen: t.AsyncGenerator[str, None] = self.root_render_func(ctx) # type: ignore[assignment]
async with aclosing(agen):
async for event in agen:
yield event
async for event in self.root_render_func(ctx): # type: ignore
yield event
except Exception:
yield self.environment.handle_exception()
def new_context(
self,
vars: dict[str, t.Any] | None = None,
vars: t.Optional[t.Dict[str, t.Any]] = None,
shared: bool = False,
locals: t.Mapping[str, t.Any] | None = None,
locals: t.Optional[t.Mapping[str, t.Any]] = None,
) -> Context:
"""Create a new :class:`Context` for this template. The vars
provided will be passed to the template. Per default the globals
@ -1385,9 +1394,9 @@ class Template:
def make_module(
self,
vars: dict[str, t.Any] | None = None,
vars: t.Optional[t.Dict[str, t.Any]] = None,
shared: bool = False,
locals: t.Mapping[str, t.Any] | None = None,
locals: t.Optional[t.Mapping[str, t.Any]] = None,
) -> "TemplateModule":
"""This method works like the :attr:`module` attribute when called
without arguments but it will evaluate the template on every call
@ -1400,9 +1409,9 @@ class Template:
async def make_module_async(
self,
vars: dict[str, t.Any] | None = None,
vars: t.Optional[t.Dict[str, t.Any]] = None,
shared: bool = False,
locals: t.Mapping[str, t.Any] | None = None,
locals: t.Optional[t.Mapping[str, t.Any]] = None,
) -> "TemplateModule":
"""As template module creation can invoke template code for
asynchronous executions this method must be used instead of the
@ -1417,7 +1426,7 @@ class Template:
)
@internalcode
def _get_default_module(self, ctx: Context | None = None) -> "TemplateModule":
def _get_default_module(self, ctx: t.Optional[Context] = None) -> "TemplateModule":
"""If a context is passed in, this means that the template was
imported. Imported templates have access to the current
template's globals by default, but they can only be accessed via
@ -1444,7 +1453,7 @@ class Template:
return self._module
async def _get_default_module_async(
self, ctx: Context | None = None
self, ctx: t.Optional[Context] = None
) -> "TemplateModule":
if ctx is not None:
keys = ctx.globals_keys - self.globals.keys()
@ -1490,7 +1499,7 @@ class Template:
return self._uptodate()
@property
def debug_info(self) -> list[tuple[int, int]]:
def debug_info(self) -> t.List[t.Tuple[int, int]]:
"""The debug info mapping."""
if self._debug_info:
return [
@ -1518,7 +1527,7 @@ class TemplateModule:
self,
template: Template,
context: Context,
body_stream: t.Iterable[str] | None = None,
body_stream: t.Optional[t.Iterable[str]] = None,
) -> None:
if body_stream is None:
if context.environment.is_async:
@ -1558,7 +1567,7 @@ class TemplateExpression:
self._template = template
self._undefined_to_none = undefined_to_none
def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Any | None:
def __call__(self, *args: t.Any, **kwargs: t.Any) -> t.Optional[t.Any]:
context = self._template.new_context(dict(*args, **kwargs))
consume(self._template.root_render_func(context))
rv = context.vars["result"]
@ -1584,9 +1593,9 @@ class TemplateStream:
def dump(
self,
fp: str | t.IO[bytes],
encoding: str | None = None,
errors: str | None = "strict",
fp: t.Union[str, t.IO[bytes]],
encoding: t.Optional[str] = None,
errors: t.Optional[str] = "strict",
) -> None:
"""Dump the complete stream into a file or file-like object.
Per default strings are written, if you want to encode
@ -1628,7 +1637,7 @@ class TemplateStream:
self.buffered = False
def _buffered_generator(self, size: int) -> t.Iterator[str]:
buf: list[str] = []
buf: t.List[str] = []
c_size = 0
push = buf.append

View File

@ -7,11 +7,11 @@ if t.TYPE_CHECKING:
class TemplateError(Exception):
"""Baseclass for all template errors."""
def __init__(self, message: str | None = None) -> None:
def __init__(self, message: t.Optional[str] = None) -> None:
super().__init__(message)
@property
def message(self) -> str | None:
def message(self) -> t.Optional[str]:
return self.args[0] if self.args else None
@ -25,12 +25,12 @@ class TemplateNotFound(IOError, LookupError, TemplateError):
# Silence the Python warning about message being deprecated since
# it's not valid here.
message: str | None = None
message: t.Optional[str] = None
def __init__(
self,
name: t.Union[str, "Undefined"] | None,
message: str | None = None,
name: t.Optional[t.Union[str, "Undefined"]],
message: t.Optional[str] = None,
) -> None:
IOError.__init__(self, name)
@ -65,7 +65,7 @@ class TemplatesNotFound(TemplateNotFound):
def __init__(
self,
names: t.Sequence[t.Union[str, "Undefined"]] = (),
message: str | None = None,
message: t.Optional[str] = None,
) -> None:
if message is None:
from .runtime import Undefined
@ -92,14 +92,14 @@ class TemplateSyntaxError(TemplateError):
self,
message: str,
lineno: int,
name: str | None = None,
filename: str | None = None,
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
) -> None:
super().__init__(message)
self.lineno = lineno
self.name = name
self.filename = filename
self.source: str | None = None
self.source: t.Optional[str] = None
# this is set to True if the debug.translate_syntax_error
# function translated the syntax error into a new traceback

View File

@ -37,12 +37,12 @@ if t.TYPE_CHECKING:
self, context: str, singular: str, plural: str, n: int
) -> str: ...
_SupportedTranslations = _TranslationsBasic | _TranslationsContext
_SupportedTranslations = t.Union[_TranslationsBasic, _TranslationsContext]
# I18N functions available in Jinja templates. If the I18N library
# provides ugettext, it will be assigned to gettext.
GETTEXT_FUNCTIONS: tuple[str, ...] = (
GETTEXT_FUNCTIONS: t.Tuple[str, ...] = (
"_",
"gettext",
"ngettext",
@ -77,7 +77,7 @@ class Extension:
cls.identifier = f"{cls.__module__}.{cls.__name__}"
#: if this extension parses this is the list of tags it's listening to.
tags: set[str] = set()
tags: t.Set[str] = set()
#: the priority of that extension. This is especially useful for
#: extensions that preprocess values. A lower value means higher
@ -89,7 +89,7 @@ class Extension:
def __init__(self, environment: Environment) -> None:
self.environment = environment
def bind(self, environment: Environment) -> "te.Self":
def bind(self, environment: Environment) -> "Extension":
"""Create a copy of this extension bound to another environment."""
rv = object.__new__(self.__class__)
rv.__dict__.update(self.__dict__)
@ -97,7 +97,7 @@ class Extension:
return rv
def preprocess(
self, source: str, name: str | None, filename: str | None = None
self, source: str, name: t.Optional[str], filename: t.Optional[str] = None
) -> str:
"""This method is called before the actual lexing and can be used to
preprocess the source. The `filename` is optional. The return value
@ -115,7 +115,7 @@ class Extension:
"""
return stream
def parse(self, parser: "Parser") -> nodes.Node | list[nodes.Node]:
def parse(self, parser: "Parser") -> t.Union[nodes.Node, t.List[nodes.Node]]:
"""If any of the :attr:`tags` matched this method is called with the
parser as first argument. The token the parser stream is pointing at
is the name token that matched. This method has to return one or a
@ -123,7 +123,9 @@ class Extension:
"""
raise NotImplementedError()
def attr(self, name: str, lineno: int | None = None) -> nodes.ExtensionAttribute:
def attr(
self, name: str, lineno: t.Optional[int] = None
) -> nodes.ExtensionAttribute:
"""Return an attribute node for the current extension. This is useful
to pass constants on extensions to generated template code.
@ -136,11 +138,11 @@ class Extension:
def call_method(
self,
name: str,
args: list[nodes.Expr] | None = None,
kwargs: list[nodes.Keyword] | None = None,
dyn_args: nodes.Expr | None = None,
dyn_kwargs: nodes.Expr | None = None,
lineno: int | None = None,
args: t.Optional[t.List[nodes.Expr]] = None,
kwargs: t.Optional[t.List[nodes.Keyword]] = None,
dyn_args: t.Optional[nodes.Expr] = None,
dyn_kwargs: t.Optional[nodes.Expr] = None,
lineno: t.Optional[int] = None,
) -> nodes.Call:
"""Call a method of the extension. This is a shortcut for
:meth:`attr` + :class:`jinja2.nodes.Call`.
@ -162,7 +164,7 @@ class Extension:
@pass_context
def _gettext_alias(
__context: Context, *args: t.Any, **kwargs: t.Any
) -> t.Any | Undefined:
) -> t.Union[t.Any, Undefined]:
return __context.call(__context.resolve("gettext"), *args, **kwargs)
@ -266,7 +268,7 @@ class InternationalizationExtension(Extension):
)
def _install(
self, translations: "_SupportedTranslations", newstyle: bool | None = None
self, translations: "_SupportedTranslations", newstyle: t.Optional[bool] = None
) -> None:
# ugettext and ungettext are preferred in case the I18N library
# is providing compatibility with older Python versions.
@ -283,25 +285,41 @@ class InternationalizationExtension(Extension):
gettext, ngettext, newstyle=newstyle, pgettext=pgettext, npgettext=npgettext
)
def _install_null(self, newstyle: bool | None = None) -> None:
def _install_null(self, newstyle: t.Optional[bool] = None) -> None:
import gettext
translations = gettext.NullTranslations()
if hasattr(translations, "pgettext"):
# Python < 3.8
pgettext = translations.pgettext
else:
def pgettext(c: str, s: str) -> str: # type: ignore[misc]
return s
if hasattr(translations, "npgettext"):
npgettext = translations.npgettext
else:
def npgettext(c: str, s: str, p: str, n: int) -> str: # type: ignore[misc]
return s if n == 1 else p
self._install_callables(
gettext=translations.gettext,
ngettext=translations.ngettext,
newstyle=newstyle,
pgettext=translations.pgettext,
npgettext=translations.npgettext,
pgettext=pgettext,
npgettext=npgettext,
)
def _install_callables(
self,
gettext: t.Callable[[str], str],
ngettext: t.Callable[[str, str, int], str],
newstyle: bool | None = None,
pgettext: t.Callable[[str, str], str] | None = None,
npgettext: t.Callable[[str, str, str, int], str] | None = None,
newstyle: t.Optional[bool] = None,
pgettext: t.Optional[t.Callable[[str, str], str]] = None,
npgettext: t.Optional[t.Callable[[str, str, str, int], str]] = None,
) -> None:
if newstyle is not None:
self.environment.newstyle_gettext = newstyle # type: ignore
@ -325,14 +343,16 @@ class InternationalizationExtension(Extension):
def _extract(
self,
source: str | nodes.Template,
source: t.Union[str, nodes.Template],
gettext_functions: t.Sequence[str] = GETTEXT_FUNCTIONS,
) -> t.Iterator[tuple[int, str, str | None | tuple[str | None, ...]]]:
) -> t.Iterator[
t.Tuple[int, str, t.Union[t.Optional[str], t.Tuple[t.Optional[str], ...]]]
]:
if isinstance(source, str):
source = self.environment.parse(source)
return extract_from_ast(source, gettext_functions)
def parse(self, parser: "Parser") -> nodes.Node | list[nodes.Node]:
def parse(self, parser: "Parser") -> t.Union[nodes.Node, t.List[nodes.Node]]:
"""Parse a translatable tag."""
lineno = next(parser.stream).lineno
@ -345,10 +365,10 @@ class InternationalizationExtension(Extension):
# find all the variables referenced. Additionally a variable can be
# defined in the body of the trans block too, but this is checked at
# a later state.
plural_expr: nodes.Expr | None = None
plural_expr_assignment: nodes.Assign | None = None
plural_expr: t.Optional[nodes.Expr] = None
plural_expr_assignment: t.Optional[nodes.Assign] = None
num_called_num = False
variables: dict[str, nodes.Expr] = {}
variables: t.Dict[str, nodes.Expr] = {}
trimmed = None
while parser.stream.current.type != "block_end":
if variables:
@ -459,7 +479,7 @@ class InternationalizationExtension(Extension):
def _parse_block(
self, parser: "Parser", allow_pluralize: bool
) -> tuple[list[str], str]:
) -> t.Tuple[t.List[str], str]:
"""Parse until the next block tag with a given name."""
referenced = []
buf = []
@ -507,10 +527,10 @@ class InternationalizationExtension(Extension):
def _make_node(
self,
singular: str,
plural: str | None,
context: str | None,
variables: dict[str, nodes.Expr],
plural_expr: nodes.Expr | None,
plural: t.Optional[str],
context: t.Optional[str],
variables: t.Dict[str, nodes.Expr],
plural_expr: t.Optional[nodes.Expr],
vars_referenced: bool,
num_called_num: bool,
) -> nodes.Output:
@ -526,7 +546,7 @@ class InternationalizationExtension(Extension):
plural = plural.replace("%%", "%")
func_name = "gettext"
func_args: list[nodes.Expr] = [nodes.Const(singular)]
func_args: t.List[nodes.Expr] = [nodes.Const(singular)]
if context is not None:
func_args.insert(0, nodes.Const(context))
@ -585,7 +605,7 @@ class LoopControlExtension(Extension):
tags = {"break", "continue"}
def parse(self, parser: "Parser") -> nodes.Break | nodes.Continue:
def parse(self, parser: "Parser") -> t.Union[nodes.Break, nodes.Continue]:
token = next(parser.stream)
if token.value == "break":
return nodes.Break(lineno=token.lineno)
@ -636,7 +656,9 @@ def extract_from_ast(
ast: nodes.Template,
gettext_functions: t.Sequence[str] = GETTEXT_FUNCTIONS,
babel_style: bool = True,
) -> t.Iterator[tuple[int, str, str | None | tuple[str | None, ...]]]:
) -> t.Iterator[
t.Tuple[int, str, t.Union[t.Optional[str], t.Tuple[t.Optional[str], ...]]]
]:
"""Extract localizable strings from the given template node. Per
default this function returns matches in babel style that means non string
parameters as well as keyword arguments are returned as `None`. This
@ -671,7 +693,7 @@ def extract_from_ast(
to extract any comments. For comment support you have to use the babel
extraction interface or extract comments yourself.
"""
out: str | None | tuple[str | None, ...]
out: t.Union[t.Optional[str], t.Tuple[t.Optional[str], ...]]
for node in ast.find_all(nodes.Call):
if (
@ -680,7 +702,7 @@ def extract_from_ast(
):
continue
strings: list[str | None] = []
strings: t.List[t.Optional[str]] = []
for arg in node.args:
if isinstance(arg, nodes.Const) and isinstance(arg.value, str):
@ -717,14 +739,14 @@ class _CommentFinder:
"""
def __init__(
self, tokens: t.Sequence[tuple[int, str, str]], comment_tags: t.Sequence[str]
self, tokens: t.Sequence[t.Tuple[int, str, str]], comment_tags: t.Sequence[str]
) -> None:
self.tokens = tokens
self.comment_tags = comment_tags
self.offset = 0
self.last_lineno = 0
def find_backwards(self, offset: int) -> list[str]:
def find_backwards(self, offset: int) -> t.List[str]:
try:
for _, token_type, token_value in reversed(
self.tokens[self.offset : offset]
@ -740,7 +762,7 @@ class _CommentFinder:
finally:
self.offset = offset
def find_comments(self, lineno: int) -> list[str]:
def find_comments(self, lineno: int) -> t.List[str]:
if not self.comment_tags or self.last_lineno > lineno:
return []
for idx, (token_lineno, _, _) in enumerate(self.tokens[self.offset :]):
@ -753,8 +775,12 @@ def babel_extract(
fileobj: t.BinaryIO,
keywords: t.Sequence[str],
comment_tags: t.Sequence[str],
options: dict[str, t.Any],
) -> t.Iterator[tuple[int, str, str | None | tuple[str | None, ...], list[str]]]:
options: t.Dict[str, t.Any],
) -> t.Iterator[
t.Tuple[
int, str, t.Union[t.Optional[str], t.Tuple[t.Optional[str], ...]], t.List[str]
]
]:
"""Babel extraction method for Jinja templates.
.. versionchanged:: 2.3
@ -782,7 +808,7 @@ def babel_extract(
:return: an iterator over ``(lineno, funcname, message, comments)`` tuples.
(comments will be empty currently)
"""
extensions: dict[type[Extension], None] = {}
extensions: t.Dict[t.Type[Extension], None] = {}
for extension_name in options.get("extensions", "").split(","):
extension_name = extension_name.strip()

View File

@ -6,7 +6,6 @@ import re
import typing
import typing as t
from collections import abc
from inspect import getattr_static
from itertools import chain
from itertools import groupby
@ -57,9 +56,9 @@ def ignore_case(value: V) -> V:
def make_attrgetter(
environment: "Environment",
attribute: str | int | None,
postprocess: t.Callable[[t.Any], t.Any] | None = None,
default: t.Any | None = None,
attribute: t.Optional[t.Union[str, int]],
postprocess: t.Optional[t.Callable[[t.Any], t.Any]] = None,
default: t.Optional[t.Any] = None,
) -> t.Callable[[t.Any], t.Any]:
"""Returns a callable that looks up the given attribute from a
passed object with the rules of the environment. Dots are allowed
@ -85,9 +84,9 @@ def make_attrgetter(
def make_multi_attrgetter(
environment: "Environment",
attribute: str | int | None,
postprocess: t.Callable[[t.Any], t.Any] | None = None,
) -> t.Callable[[t.Any], list[t.Any]]:
attribute: t.Optional[t.Union[str, int]],
postprocess: t.Optional[t.Callable[[t.Any], t.Any]] = None,
) -> t.Callable[[t.Any], t.List[t.Any]]:
"""Returns a callable that looks up the given comma separated
attributes from a passed object with the rules of the environment.
Dots are allowed to access attributes of each attribute. Integer
@ -99,13 +98,13 @@ def make_multi_attrgetter(
Examples of attribute: "attr1,attr2", "attr1.inner1.0,attr2.inner2.0", etc.
"""
if isinstance(attribute, str):
split: t.Sequence[str | int | None] = attribute.split(",")
split: t.Sequence[t.Union[str, int, None]] = attribute.split(",")
else:
split = [attribute]
parts = [_prepare_attribute_parts(item) for item in split]
def attrgetter(item: t.Any) -> list[t.Any]:
def attrgetter(item: t.Any) -> t.List[t.Any]:
items = [None] * len(parts)
for i, attribute_part in enumerate(parts):
@ -125,8 +124,8 @@ def make_multi_attrgetter(
def _prepare_attribute_parts(
attr: str | int | None,
) -> list[str | int]:
attr: t.Optional[t.Union[str, int]],
) -> t.List[t.Union[str, int]]:
if attr is None:
return []
@ -136,7 +135,7 @@ def _prepare_attribute_parts(
return [attr]
def do_forceescape(value: "str | HasHTML") -> Markup:
def do_forceescape(value: "t.Union[str, HasHTML]") -> Markup:
"""Enforce HTML escaping. This will probably double escape variables."""
if hasattr(value, "__html__"):
value = t.cast("HasHTML", value).__html__()
@ -145,7 +144,7 @@ def do_forceescape(value: "str | HasHTML") -> Markup:
def do_urlencode(
value: str | t.Mapping[str, t.Any] | t.Iterable[tuple[str, t.Any]],
value: t.Union[str, t.Mapping[str, t.Any], t.Iterable[t.Tuple[str, t.Any]]],
) -> str:
"""Quote data for use in a URL path or query using UTF-8.
@ -166,7 +165,7 @@ def do_urlencode(
return url_quote(value)
if isinstance(value, dict):
items: t.Iterable[tuple[str, t.Any]] = value.items()
items: t.Iterable[t.Tuple[str, t.Any]] = value.items()
else:
items = value # type: ignore
@ -177,7 +176,7 @@ def do_urlencode(
@pass_eval_context
def do_replace(
eval_ctx: "EvalContext", s: str, old: str, new: str, count: int | None = None
eval_ctx: "EvalContext", s: str, old: str, new: str, count: t.Optional[int] = None
) -> str:
"""Return a copy of the value with all occurrences of a substring
replaced with a new one. The first argument is the substring
@ -221,7 +220,7 @@ def do_lower(s: str) -> str:
return soft_str(s).lower()
def do_items(value: t.Mapping[K, V] | Undefined) -> t.Iterator[tuple[K, V]]:
def do_items(value: t.Union[t.Mapping[K, V], Undefined]) -> t.Iterator[t.Tuple[K, V]]:
"""Return an iterator over the ``(key, value)`` items of a mapping.
``x|items`` is the same as ``x.items()``, except if ``x`` is
@ -346,7 +345,7 @@ def do_dictsort(
case_sensitive: bool = False,
by: 'te.Literal["key", "value"]' = "key",
reverse: bool = False,
) -> list[tuple[K, V]]:
) -> t.List[t.Tuple[K, V]]:
"""Sort a dict and yield (key, value) pairs. Python dicts may not
be in the order you want to display them in, so sort them first.
@ -371,7 +370,7 @@ def do_dictsort(
else:
raise FilterArgumentError('You can only sort by either "key" or "value"')
def sort_func(item: tuple[t.Any, t.Any]) -> t.Any:
def sort_func(item: t.Tuple[t.Any, t.Any]) -> t.Any:
value = item[pos]
if not case_sensitive:
@ -388,8 +387,8 @@ def do_sort(
value: "t.Iterable[V]",
reverse: bool = False,
case_sensitive: bool = False,
attribute: str | int | None = None,
) -> "list[V]":
attribute: t.Optional[t.Union[str, int]] = None,
) -> "t.List[V]":
"""Sort an iterable using Python's :func:`sorted`.
.. sourcecode:: jinja
@ -439,11 +438,11 @@ def do_sort(
@pass_environment
def sync_do_unique(
def do_unique(
environment: "Environment",
value: "t.Iterable[V]",
case_sensitive: bool = False,
attribute: str | int | None = None,
attribute: t.Optional[t.Union[str, int]] = None,
) -> "t.Iterator[V]":
"""Returns a list of unique items from the given iterable.
@ -471,25 +470,13 @@ def sync_do_unique(
yield item
@async_variant(sync_do_unique) # type: ignore
async def do_unique(
environment: "Environment",
value: "t.AsyncIterable[V] | t.Iterable[V]",
case_sensitive: bool = False,
attribute: str | int | None = None,
) -> "t.Iterator[V]":
return sync_do_unique(
environment, await auto_to_list(value), case_sensitive, attribute
)
def _min_or_max(
environment: "Environment",
value: "t.Iterable[V]",
func: "t.Callable[..., V]",
case_sensitive: bool,
attribute: str | int | None,
) -> "V | Undefined":
attribute: t.Optional[t.Union[str, int]],
) -> "t.Union[V, Undefined]":
it = iter(value)
try:
@ -508,8 +495,8 @@ def do_min(
environment: "Environment",
value: "t.Iterable[V]",
case_sensitive: bool = False,
attribute: str | int | None = None,
) -> "V | Undefined":
attribute: t.Optional[t.Union[str, int]] = None,
) -> "t.Union[V, Undefined]":
"""Return the smallest item from the sequence.
.. sourcecode:: jinja
@ -528,8 +515,8 @@ def do_max(
environment: "Environment",
value: "t.Iterable[V]",
case_sensitive: bool = False,
attribute: str | int | None = None,
) -> "V | Undefined":
attribute: t.Optional[t.Union[str, int]] = None,
) -> "t.Union[V, Undefined]":
"""Return the largest item from the sequence.
.. sourcecode:: jinja
@ -581,7 +568,7 @@ def sync_do_join(
eval_ctx: "EvalContext",
value: t.Iterable[t.Any],
d: str = "",
attribute: str | int | None = None,
attribute: t.Optional[t.Union[str, int]] = None,
) -> str:
"""Return a string which is the concatenation of the strings in the
sequence. The separator between elements is an empty string per
@ -637,9 +624,9 @@ def sync_do_join(
@async_variant(sync_do_join) # type: ignore
async def do_join(
eval_ctx: "EvalContext",
value: t.AsyncIterable[t.Any] | t.Iterable[t.Any],
value: t.Union[t.AsyncIterable[t.Any], t.Iterable[t.Any]],
d: str = "",
attribute: str | int | None = None,
attribute: t.Optional[t.Union[str, int]] = None,
) -> str:
return sync_do_join(eval_ctx, await auto_to_list(value), d, attribute)
@ -650,7 +637,9 @@ def do_center(value: str, width: int = 80) -> str:
@pass_environment
def sync_do_first(environment: "Environment", seq: "t.Iterable[V]") -> "V | Undefined":
def sync_do_first(
environment: "Environment", seq: "t.Iterable[V]"
) -> "t.Union[V, Undefined]":
"""Return the first item of a sequence."""
try:
return next(iter(seq))
@ -660,8 +649,8 @@ def sync_do_first(environment: "Environment", seq: "t.Iterable[V]") -> "V | Unde
@async_variant(sync_do_first) # type: ignore
async def do_first(
environment: "Environment", seq: "t.AsyncIterable[V] | t.Iterable[V]"
) -> "V | Undefined":
environment: "Environment", seq: "t.Union[t.AsyncIterable[V], t.Iterable[V]]"
) -> "t.Union[V, Undefined]":
try:
return await auto_aiter(seq).__anext__()
except StopAsyncIteration:
@ -669,7 +658,9 @@ async def do_first(
@pass_environment
def do_last(environment: "Environment", seq: "t.Reversible[V]") -> "V | Undefined":
def do_last(
environment: "Environment", seq: "t.Reversible[V]"
) -> "t.Union[V, Undefined]":
"""Return the last item of a sequence.
Note: Does not work with generators. You may want to explicitly
@ -689,7 +680,7 @@ def do_last(environment: "Environment", seq: "t.Reversible[V]") -> "V | Undefine
@pass_context
def do_random(context: "Context", seq: "t.Sequence[V]") -> "V | Undefined":
def do_random(context: "Context", seq: "t.Sequence[V]") -> "t.Union[V, Undefined]":
"""Return a random item from the sequence."""
try:
return random.choice(seq)
@ -697,7 +688,7 @@ def do_random(context: "Context", seq: "t.Sequence[V]") -> "V | Undefined":
return context.environment.undefined("No random item, sequence was empty.")
def do_filesizeformat(value: str | float | int, binary: bool = False) -> str:
def do_filesizeformat(value: t.Union[str, float, int], binary: bool = False) -> str:
"""Format the value like a 'human-readable' file size (i.e. 13 kB,
4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega,
Giga, etc.), if the second parameter is set to `True` the binary
@ -742,11 +733,11 @@ _uri_scheme_re = re.compile(r"^([\w.+-]{2,}:(/){0,2})$")
def do_urlize(
eval_ctx: "EvalContext",
value: str,
trim_url_limit: int | None = None,
trim_url_limit: t.Optional[int] = None,
nofollow: bool = False,
target: str | None = None,
rel: str | None = None,
extra_schemes: t.Iterable[str] | None = None,
target: t.Optional[str] = None,
rel: t.Optional[str] = None,
extra_schemes: t.Optional[t.Iterable[str]] = None,
) -> str:
"""Convert URLs in text into clickable links.
@ -819,7 +810,7 @@ def do_urlize(
def do_indent(
s: str, width: int | str = 4, first: bool = False, blank: bool = False
s: str, width: t.Union[int, str] = 4, first: bool = False, blank: bool = False
) -> str:
"""Return a copy of the string with each line indented by 4 spaces. The
first line and blank lines are not indented by default.
@ -873,7 +864,7 @@ def do_truncate(
length: int = 255,
killwords: bool = False,
end: str = "...",
leeway: int | None = None,
leeway: t.Optional[int] = None,
) -> str:
"""Return a truncated copy of the string. The length is specified
with the first parameter which defaults to ``255``. If the second
@ -920,7 +911,7 @@ def do_wordwrap(
s: str,
width: int = 79,
break_long_words: bool = True,
wrapstring: str | None = None,
wrapstring: t.Optional[str] = None,
break_on_hyphens: bool = True,
) -> str:
"""Wrap a string to the given width. Existing newlines are treated
@ -996,7 +987,7 @@ def do_int(value: t.Any, default: int = 0, base: int = 10) -> int:
# this quirk is necessary so that "42.23"|int gives 42.
try:
return int(float(value))
except (TypeError, ValueError, OverflowError):
except (TypeError, ValueError):
return default
@ -1039,12 +1030,12 @@ def do_format(value: str, *args: t.Any, **kwargs: t.Any) -> str:
return soft_str(value) % (kwargs or args)
def do_trim(value: str, chars: str | None = None) -> str:
def do_trim(value: str, chars: t.Optional[str] = None) -> str:
"""Strip leading and trailing characters, by default whitespace."""
return soft_str(value).strip(chars)
def do_striptags(value: "str | HasHTML") -> str:
def do_striptags(value: "t.Union[str, HasHTML]") -> str:
"""Strip SGML/XML tags and replace adjacent whitespace by one space."""
if hasattr(value, "__html__"):
value = t.cast("HasHTML", value).__html__()
@ -1053,8 +1044,8 @@ def do_striptags(value: "str | HasHTML") -> str:
def sync_do_slice(
value: "t.Collection[V]", slices: int, fill_with: "V | None" = None
) -> "t.Iterator[list[V]]":
value: "t.Collection[V]", slices: int, fill_with: "t.Optional[V]" = None
) -> "t.Iterator[t.List[V]]":
"""Slice an iterator and return a list of lists containing
those items. Useful if you want to create a div containing
three ul tags that represent columns:
@ -1097,16 +1088,16 @@ def sync_do_slice(
@async_variant(sync_do_slice) # type: ignore
async def do_slice(
value: "t.AsyncIterable[V] | t.Iterable[V]",
value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
slices: int,
fill_with: t.Any | None = None,
) -> "t.Iterator[list[V]]":
fill_with: t.Optional[t.Any] = None,
) -> "t.Iterator[t.List[V]]":
return sync_do_slice(await auto_to_list(value), slices, fill_with)
def do_batch(
value: "t.Iterable[V]", linecount: int, fill_with: "V | None" = None
) -> "t.Iterator[list[V]]":
value: "t.Iterable[V]", linecount: int, fill_with: "t.Optional[V]" = None
) -> "t.Iterator[t.List[V]]":
"""
A filter that batches items. It works pretty much like `slice`
just the other way round. It returns a list of lists with the
@ -1125,7 +1116,7 @@ def do_batch(
{%- endfor %}
</table>
"""
tmp: list[V] = []
tmp: "t.List[V]" = []
for item in value:
if len(tmp) == linecount:
@ -1183,7 +1174,7 @@ def do_round(
class _GroupTuple(t.NamedTuple):
grouper: t.Any
list: list[t.Any]
list: t.List[t.Any]
# Use the regular tuple repr to hide this subclass if users print
# out the value during debugging.
@ -1198,10 +1189,10 @@ class _GroupTuple(t.NamedTuple):
def sync_do_groupby(
environment: "Environment",
value: "t.Iterable[V]",
attribute: str | int,
default: t.Any | None = None,
attribute: t.Union[str, int],
default: t.Optional[t.Any] = None,
case_sensitive: bool = False,
) -> "list[_GroupTuple]":
) -> "t.List[_GroupTuple]":
"""Group a sequence of objects by an attribute using Python's
:func:`itertools.groupby`. The attribute can use dot notation for
nested access, like ``"address.city"``. Unlike Python's ``groupby``,
@ -1281,11 +1272,11 @@ def sync_do_groupby(
@async_variant(sync_do_groupby) # type: ignore
async def do_groupby(
environment: "Environment",
value: "t.AsyncIterable[V] | t.Iterable[V]",
attribute: str | int,
default: t.Any | None = None,
value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
attribute: t.Union[str, int],
default: t.Optional[t.Any] = None,
case_sensitive: bool = False,
) -> "list[_GroupTuple]":
) -> "t.List[_GroupTuple]":
expr = make_attrgetter(
environment,
attribute,
@ -1309,7 +1300,7 @@ async def do_groupby(
def sync_do_sum(
environment: "Environment",
iterable: "t.Iterable[V]",
attribute: str | int | None = None,
attribute: t.Optional[t.Union[str, int]] = None,
start: V = 0, # type: ignore
) -> V:
"""Returns the sum of a sequence of numbers plus the value of parameter
@ -1335,8 +1326,8 @@ def sync_do_sum(
@async_variant(sync_do_sum) # type: ignore
async def do_sum(
environment: "Environment",
iterable: "t.AsyncIterable[V] | t.Iterable[V]",
attribute: str | int | None = None,
iterable: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
attribute: t.Optional[t.Union[str, int]] = None,
start: V = 0, # type: ignore
) -> V:
rv = start
@ -1354,7 +1345,7 @@ async def do_sum(
return rv
def sync_do_list(value: "t.Iterable[V]") -> "list[V]":
def sync_do_list(value: "t.Iterable[V]") -> "t.List[V]":
"""Convert the value into a list. If it was a string the returned list
will be a list of characters.
"""
@ -1362,7 +1353,7 @@ def sync_do_list(value: "t.Iterable[V]") -> "list[V]":
@async_variant(sync_do_list) # type: ignore
async def do_list(value: "t.AsyncIterable[V] | t.Iterable[V]") -> "list[V]":
async def do_list(value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]") -> "t.List[V]":
return await auto_to_list(value)
@ -1386,7 +1377,7 @@ def do_reverse(value: str) -> str: ...
def do_reverse(value: "t.Iterable[V]") -> "t.Iterable[V]": ...
def do_reverse(value: str | t.Iterable[V]) -> str | t.Iterable[V]:
def do_reverse(value: t.Union[str, t.Iterable[V]]) -> t.Union[str, t.Iterable[V]]:
"""Reverse the object or return an iterator that iterates over it the other
way round.
"""
@ -1405,26 +1396,34 @@ def do_reverse(value: str | t.Iterable[V]) -> str | t.Iterable[V]:
@pass_environment
def do_attr(environment: "Environment", obj: t.Any, name: str) -> Undefined | t.Any:
"""Get an attribute of an object. ``foo|attr("bar")`` works like
``foo.bar``, but returns undefined instead of falling back to ``foo["bar"]``
if the attribute doesn't exist.
def do_attr(
environment: "Environment", obj: t.Any, name: str
) -> t.Union[Undefined, t.Any]:
"""Get an attribute of an object. ``foo|attr("bar")`` works like
``foo.bar`` just that always an attribute is returned and items are not
looked up.
See :ref:`Notes on subscriptions <notes-on-subscriptions>` for more details.
"""
# Environment.getattr will fall back to obj[name] if obj.name doesn't exist.
# But we want to call env.getattr to get behavior such as sandboxing.
# Determine if the attr exists first, so we know the fallback won't trigger.
try:
# This avoids executing properties/descriptors, but misses __getattr__
# and __getattribute__ dynamic attrs.
getattr_static(obj, name)
except AttributeError:
# This finds dynamic attrs, and we know it's not a descriptor at this point.
if not hasattr(obj, name):
return environment.undefined(obj=obj, name=name)
name = str(name)
except UnicodeError:
pass
else:
try:
value = getattr(obj, name)
except AttributeError:
pass
else:
if environment.sandboxed:
environment = t.cast("SandboxedEnvironment", environment)
return environment.getattr(obj, name)
if not environment.is_safe_attribute(obj, name, value):
return environment.unsafe_undefined(obj, name)
return value
return environment.undefined(obj=obj, name=name)
@typing.overload
@ -1443,7 +1442,7 @@ def sync_do_map(
value: t.Iterable[t.Any],
*,
attribute: str = ...,
default: t.Any | None = None,
default: t.Optional[t.Any] = None,
) -> t.Iterable[t.Any]: ...
@ -1500,7 +1499,7 @@ def sync_do_map(
@typing.overload
def do_map(
context: "Context",
value: t.AsyncIterable[t.Any] | t.Iterable[t.Any],
value: t.Union[t.AsyncIterable[t.Any], t.Iterable[t.Any]],
name: str,
*args: t.Any,
**kwargs: t.Any,
@ -1510,17 +1509,17 @@ def do_map(
@typing.overload
def do_map(
context: "Context",
value: t.AsyncIterable[t.Any] | t.Iterable[t.Any],
value: t.Union[t.AsyncIterable[t.Any], t.Iterable[t.Any]],
*,
attribute: str = ...,
default: t.Any | None = None,
default: t.Optional[t.Any] = None,
) -> t.Iterable[t.Any]: ...
@async_variant(sync_do_map) # type: ignore
async def do_map(
context: "Context",
value: t.AsyncIterable[t.Any] | t.Iterable[t.Any],
value: t.Union[t.AsyncIterable[t.Any], t.Iterable[t.Any]],
*args: t.Any,
**kwargs: t.Any,
) -> t.AsyncIterable[t.Any]:
@ -1565,7 +1564,7 @@ def sync_do_select(
@async_variant(sync_do_select) # type: ignore
async def do_select(
context: "Context",
value: "t.AsyncIterable[V] | t.Iterable[V]",
value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
*args: t.Any,
**kwargs: t.Any,
) -> "t.AsyncIterator[V]":
@ -1601,7 +1600,7 @@ def sync_do_reject(
@async_variant(sync_do_reject) # type: ignore
async def do_reject(
context: "Context",
value: "t.AsyncIterable[V] | t.Iterable[V]",
value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
*args: t.Any,
**kwargs: t.Any,
) -> "t.AsyncIterator[V]":
@ -1630,8 +1629,8 @@ def sync_do_selectattr(
.. code-block:: python
(user for user in users if user.is_active)
(user for user in users if test_none(user.email))
(u for user in users if user.is_active)
(u for user in users if test_none(user.email))
.. versionadded:: 2.7
"""
@ -1641,7 +1640,7 @@ def sync_do_selectattr(
@async_variant(sync_do_selectattr) # type: ignore
async def do_selectattr(
context: "Context",
value: "t.AsyncIterable[V] | t.Iterable[V]",
value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
*args: t.Any,
**kwargs: t.Any,
) -> "t.AsyncIterator[V]":
@ -1668,8 +1667,8 @@ def sync_do_rejectattr(
.. code-block:: python
(user for user in users if not user.is_active)
(user for user in users if not test_none(user.email))
(u for user in users if not user.is_active)
(u for user in users if not test_none(user.email))
.. versionadded:: 2.7
"""
@ -1679,7 +1678,7 @@ def sync_do_rejectattr(
@async_variant(sync_do_rejectattr) # type: ignore
async def do_rejectattr(
context: "Context",
value: "t.AsyncIterable[V] | t.Iterable[V]",
value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
*args: t.Any,
**kwargs: t.Any,
) -> "t.AsyncIterator[V]":
@ -1688,7 +1687,7 @@ async def do_rejectattr(
@pass_eval_context
def do_tojson(
eval_ctx: "EvalContext", value: t.Any, indent: int | None = None
eval_ctx: "EvalContext", value: t.Any, indent: t.Optional[int] = None
) -> Markup:
"""Serialize an object to a string of JSON, and mark it safe to
render in HTML. This filter is only for use in HTML documents.
@ -1716,7 +1715,7 @@ def do_tojson(
def prepare_map(
context: "Context", args: tuple[t.Any, ...], kwargs: dict[str, t.Any]
context: "Context", args: t.Tuple[t.Any, ...], kwargs: t.Dict[str, t.Any]
) -> t.Callable[[t.Any], t.Any]:
if not args and "attribute" in kwargs:
attribute = kwargs.pop("attribute")
@ -1745,8 +1744,8 @@ def prepare_map(
def prepare_select_or_reject(
context: "Context",
args: tuple[t.Any, ...],
kwargs: dict[str, t.Any],
args: t.Tuple[t.Any, ...],
kwargs: t.Dict[str, t.Any],
modfunc: t.Callable[[t.Any], t.Any],
lookup_attr: bool,
) -> t.Callable[[t.Any], t.Any]:
@ -1769,7 +1768,7 @@ def prepare_select_or_reject(
args = args[1 + off :]
def func(item: t.Any) -> t.Any:
return context.environment.call_test(name, item, args, kwargs, context)
return context.environment.call_test(name, item, args, kwargs)
except LookupError:
func = bool # type: ignore
@ -1780,8 +1779,8 @@ def prepare_select_or_reject(
def select_or_reject(
context: "Context",
value: "t.Iterable[V]",
args: tuple[t.Any, ...],
kwargs: dict[str, t.Any],
args: t.Tuple[t.Any, ...],
kwargs: t.Dict[str, t.Any],
modfunc: t.Callable[[t.Any], t.Any],
lookup_attr: bool,
) -> "t.Iterator[V]":
@ -1795,9 +1794,9 @@ def select_or_reject(
async def async_select_or_reject(
context: "Context",
value: "t.AsyncIterable[V] | t.Iterable[V]",
args: tuple[t.Any, ...],
kwargs: dict[str, t.Any],
value: "t.Union[t.AsyncIterable[V], t.Iterable[V]]",
args: t.Tuple[t.Any, ...],
kwargs: t.Dict[str, t.Any],
modfunc: t.Callable[[t.Any], t.Any],
lookup_attr: bool,
) -> "t.AsyncIterator[V]":

View File

@ -3,9 +3,6 @@ import typing as t
from . import nodes
from .visitor import NodeVisitor
if t.TYPE_CHECKING:
import typing_extensions as te
VAR_LOAD_PARAMETER = "param"
VAR_LOAD_RESOLVE = "resolve"
VAR_LOAD_ALIAS = "alias"
@ -32,7 +29,7 @@ def symbols_for_node(
class Symbols:
def __init__(
self, parent: t.Optional["Symbols"] = None, level: int | None = None
self, parent: t.Optional["Symbols"] = None, level: t.Optional[int] = None
) -> None:
if level is None:
if parent is None:
@ -42,22 +39,24 @@ class Symbols:
self.level: int = level
self.parent = parent
self.refs: dict[str, str] = {}
self.loads: dict[str, t.Any] = {}
self.stores: set[str] = set()
self.refs: t.Dict[str, str] = {}
self.loads: t.Dict[str, t.Any] = {}
self.stores: t.Set[str] = set()
def analyze_node(self, node: nodes.Node, **kwargs: t.Any) -> None:
visitor = RootVisitor(self)
visitor.visit(node, **kwargs)
def _define_ref(self, name: str, load: tuple[str, str | None] | None = None) -> str:
def _define_ref(
self, name: str, load: t.Optional[t.Tuple[str, t.Optional[str]]] = None
) -> str:
ident = f"l_{self.level}_{name}"
self.refs[name] = ident
if load is not None:
self.loads[ident] = load
return ident
def find_load(self, target: str) -> t.Any | None:
def find_load(self, target: str) -> t.Optional[t.Any]:
if target in self.loads:
return self.loads[target]
@ -66,7 +65,7 @@ class Symbols:
return None
def find_ref(self, name: str) -> str | None:
def find_ref(self, name: str) -> t.Optional[str]:
if name in self.refs:
return self.refs[name]
@ -84,7 +83,7 @@ class Symbols:
)
return rv
def copy(self) -> "te.Self":
def copy(self) -> "Symbols":
rv = object.__new__(self.__class__)
rv.__dict__.update(self.__dict__)
rv.refs = self.refs.copy()
@ -119,20 +118,23 @@ class Symbols:
self._define_ref(name, load=(VAR_LOAD_RESOLVE, name))
def branch_update(self, branch_symbols: t.Sequence["Symbols"]) -> None:
stores: set[str] = set()
stores: t.Dict[str, int] = {}
for branch in branch_symbols:
stores.update(branch.stores)
stores.difference_update(self.stores)
for target in branch.stores:
if target in self.stores:
continue
stores[target] = stores.get(target, 0) + 1
for sym in branch_symbols:
self.refs.update(sym.refs)
self.loads.update(sym.loads)
self.stores.update(sym.stores)
for name in stores:
target = self.find_ref(name)
for name, branch_count in stores.items():
if branch_count == len(branch_symbols):
continue
target = self.find_ref(name) # type: ignore
assert target is not None, "should not happen"
if self.parent is not None:
@ -142,9 +144,9 @@ class Symbols:
continue
self.loads[target] = (VAR_LOAD_RESOLVE, name)
def dump_stores(self) -> dict[str, str]:
rv: dict[str, str] = {}
node: Symbols | None = self
def dump_stores(self) -> t.Dict[str, str]:
rv: t.Dict[str, str] = {}
node: t.Optional["Symbols"] = self
while node is not None:
for name in sorted(node.stores):
@ -155,9 +157,9 @@ class Symbols:
return rv
def dump_param_targets(self) -> set[str]:
def dump_param_targets(self) -> t.Set[str]:
rv = set()
node: Symbols | None = self
node: t.Optional["Symbols"] = self
while node is not None:
for target, (instr, _) in self.loads.items():

View File

@ -21,7 +21,7 @@ if t.TYPE_CHECKING:
# cache for the lexers. Exists in order to be able to have multiple
# environments with the same lexer
_lexer_cache: t.MutableMapping[tuple, "Lexer"] = LRUCache(50) # type: ignore
_lexer_cache: t.MutableMapping[t.Tuple, "Lexer"] = LRUCache(50) # type: ignore
# static regular expressions
whitespace_re = re.compile(r"\s+")
@ -210,7 +210,7 @@ def count_newlines(value: str) -> int:
return len(newline_re.findall(value))
def compile_rules(environment: "Environment") -> list[tuple[str, str]]:
def compile_rules(environment: "Environment") -> t.List[t.Tuple[str, str]]:
"""Compiles all the rules from the environment into a list of rules."""
e = re.escape
rules = [
@ -257,12 +257,12 @@ class Failure:
"""
def __init__(
self, message: str, cls: type[TemplateSyntaxError] = TemplateSyntaxError
self, message: str, cls: t.Type[TemplateSyntaxError] = TemplateSyntaxError
) -> None:
self.message = message
self.error_class = cls
def __call__(self, lineno: int, filename: str | None) -> "te.NoReturn":
def __call__(self, lineno: int, filename: str) -> "te.NoReturn":
raise self.error_class(self.message, lineno, filename)
@ -325,11 +325,11 @@ class TokenStream:
def __init__(
self,
generator: t.Iterable[Token],
name: str | None,
filename: str | None,
name: t.Optional[str],
filename: t.Optional[str],
):
self._iter = iter(generator)
self._pushed: deque[Token] = deque()
self._pushed: "te.Deque[Token]" = deque()
self.name = name
self.filename = filename
self.closed = False
@ -364,7 +364,7 @@ class TokenStream:
for _ in range(n):
next(self)
def next_if(self, expr: str) -> Token | None:
def next_if(self, expr: str) -> t.Optional[Token]:
"""Perform the token test and return the token if it matched.
Otherwise the return value is `None`.
"""
@ -464,8 +464,8 @@ class OptionalLStrip(tuple): # type: ignore[type-arg]
class _Rule(t.NamedTuple):
pattern: t.Pattern[str]
tokens: str | tuple[str, ...] | tuple[Failure]
command: str | None
tokens: t.Union[str, t.Tuple[str, ...], t.Tuple[Failure]]
command: t.Optional[str]
class Lexer:
@ -484,7 +484,7 @@ class Lexer:
return re.compile(x, re.M | re.S)
# lexing rules for tags
tag_rules: list[_Rule] = [
tag_rules: t.List[_Rule] = [
_Rule(whitespace_re, TOKEN_WHITESPACE, None),
_Rule(float_re, TOKEN_FLOAT, None),
_Rule(integer_re, TOKEN_INTEGER, None),
@ -523,7 +523,7 @@ class Lexer:
)
# global lexing rules
self.rules: dict[str, list[_Rule]] = {
self.rules: t.Dict[str, t.List[_Rule]] = {
"root": [
# directives
_Rule(
@ -604,9 +604,9 @@ class Lexer:
def tokenize(
self,
source: str,
name: str | None = None,
filename: str | None = None,
state: str | None = None,
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
state: t.Optional[str] = None,
) -> TokenStream:
"""Calls tokeniter + tokenize and wraps it in a token stream."""
stream = self.tokeniter(source, name, filename, state)
@ -614,9 +614,9 @@ class Lexer:
def wrap(
self,
stream: t.Iterable[tuple[int, str, str]],
name: str | None = None,
filename: str | None = None,
stream: t.Iterable[t.Tuple[int, str, str]],
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
) -> t.Iterator[Token]:
"""This is called with the stream as returned by `tokenize` and wraps
every token in a :class:`Token` and converts the value.
@ -669,10 +669,10 @@ class Lexer:
def tokeniter(
self,
source: str,
name: str | None,
filename: str | None = None,
state: str | None = None,
) -> t.Iterator[tuple[int, str, str]]:
name: t.Optional[str],
filename: t.Optional[str] = None,
state: t.Optional[str] = None,
) -> t.Iterator[t.Tuple[int, str, str]]:
"""This method tokenizes the text and returns the tokens in a
generator. Use this method if you just want to tokenize a template.
@ -696,7 +696,7 @@ class Lexer:
statetokens = self.rules[stack[-1]]
source_length = len(source)
balancing_stack: list[str] = []
balancing_stack: t.List[str] = []
newlines_stripped = 0
line_starting = True
@ -757,7 +757,7 @@ class Lexer:
for idx, token in enumerate(tokens):
# failure group
if isinstance(token, Failure):
if token.__class__ is Failure:
raise token(lineno, filename)
# bygroup is a bit more complex, in that case we
# yield for the current token the first named
@ -778,7 +778,7 @@ class Lexer:
data = groups[idx]
if data or token not in ignore_if_empty:
yield lineno, token, data # type: ignore[misc]
yield lineno, token, data
lineno += data.count("\n") + newlines_stripped
newlines_stripped = 0

View File

@ -22,14 +22,14 @@ if t.TYPE_CHECKING:
from .environment import Template
def split_template_path(template: str) -> list[str]:
def split_template_path(template: str) -> t.List[str]:
"""Split a path into segments and perform a sanity check. If it detects
'..' in the path it will raise a `TemplateNotFound` error.
"""
pieces = []
for piece in template.split("/"):
if (
os.sep in piece
os.path.sep in piece
or (os.path.altsep and os.path.altsep in piece)
or piece == os.path.pardir
):
@ -74,7 +74,7 @@ class BaseLoader:
def get_source(
self, environment: "Environment", template: str
) -> tuple[str, str | None, t.Callable[[], bool] | None]:
) -> t.Tuple[str, t.Optional[str], t.Optional[t.Callable[[], bool]]]:
"""Get the template source, filename and reload helper for a template.
It's passed the environment and template name and has to return a
tuple in the form ``(source, filename, uptodate)`` or raise a
@ -98,7 +98,7 @@ class BaseLoader:
)
raise TemplateNotFound(template)
def list_templates(self) -> list[str]:
def list_templates(self) -> t.List[str]:
"""Iterates over all templates. If the loader does not support that
it should raise a :exc:`TypeError` which is the default behavior.
"""
@ -109,7 +109,7 @@ class BaseLoader:
self,
environment: "Environment",
name: str,
globals: t.MutableMapping[str, t.Any] | None = None,
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
) -> "Template":
"""Loads a template. This method looks up the template in the cache
or loads one by calling :meth:`get_source`. Subclasses should not
@ -193,7 +193,7 @@ class FileSystemLoader(BaseLoader):
def get_source(
self, environment: "Environment", template: str
) -> tuple[str, str, t.Callable[[], bool]]:
) -> t.Tuple[str, str, t.Callable[[], bool]]:
pieces = split_template_path(template)
for searchpath in self.searchpath:
@ -204,12 +204,7 @@ class FileSystemLoader(BaseLoader):
if os.path.isfile(filename):
break
else:
plural = "path" if len(self.searchpath) == 1 else "paths"
paths_str = ", ".join(repr(p) for p in self.searchpath)
raise TemplateNotFound(
template,
f"{template!r} not found in search {plural}: {paths_str}",
)
raise TemplateNotFound(template)
with open(filename, encoding=self.encoding) as f:
contents = f.read()
@ -225,7 +220,7 @@ class FileSystemLoader(BaseLoader):
# Use normpath to convert Windows altsep to sep.
return contents, os.path.normpath(filename), uptodate
def list_templates(self) -> list[str]:
def list_templates(self) -> t.List[str]:
found = set()
for searchpath in self.searchpath:
walk_dir = os.walk(searchpath, followlinks=self.followlinks)
@ -233,8 +228,8 @@ class FileSystemLoader(BaseLoader):
for filename in filenames:
template = (
os.path.join(dirpath, filename)[len(searchpath) :]
.strip(os.sep)
.replace(os.sep, "/")
.strip(os.path.sep)
.replace(os.path.sep, "/")
)
if template[:2] == "./":
template = template[2:]
@ -243,29 +238,6 @@ class FileSystemLoader(BaseLoader):
return sorted(found)
if sys.version_info >= (3, 13):
def _get_zipimporter_files(z: t.Any) -> dict[str, object]:
try:
get_files = z._get_files
except AttributeError as e:
raise TypeError(
"This zip import does not have the required metadata to list templates."
) from e
return get_files()
else:
def _get_zipimporter_files(z: t.Any) -> dict[str, object]:
try:
files = z._files
except AttributeError as e:
raise TypeError(
"This zip import does not have the required metadata to list templates."
) from e
return files # type: ignore[no-any-return]
class PackageLoader(BaseLoader):
"""Load templates from a directory in a Python package.
@ -305,12 +277,12 @@ class PackageLoader(BaseLoader):
package_path: "str" = "templates",
encoding: str = "utf-8",
) -> None:
package_path = os.path.normpath(package_path).rstrip(os.sep)
package_path = os.path.normpath(package_path).rstrip(os.path.sep)
# normpath preserves ".", which isn't valid in zip paths.
if package_path == os.path.curdir:
package_path = ""
elif package_path[:2] == os.path.curdir + os.sep:
elif package_path[:2] == os.path.curdir + os.path.sep:
package_path = package_path[2:]
self.package_path = package_path
@ -326,13 +298,14 @@ class PackageLoader(BaseLoader):
assert loader is not None, "A loader was not found for the package."
self._loader = loader
self._archive = None
template_root = None
if isinstance(loader, zipimport.zipimporter):
self._archive = loader.archive
pkgdir = next(iter(spec.submodule_search_locations)) # type: ignore
template_root = os.path.join(pkgdir, package_path).rstrip(os.sep)
template_root = os.path.join(pkgdir, package_path).rstrip(os.path.sep)
else:
roots: list[str] = []
roots: t.List[str] = []
# One element for regular packages, multiple for namespace
# packages, or None for single module file.
@ -342,36 +315,31 @@ class PackageLoader(BaseLoader):
elif spec.origin is not None:
roots.append(os.path.dirname(spec.origin))
if not roots:
raise ValueError(
f"The {package_name!r} package was not installed in a"
" way that PackageLoader understands."
)
for root in roots:
root = os.path.join(root, package_path)
if os.path.isdir(root):
template_root = root
break
else:
raise ValueError(
f"PackageLoader could not find a {package_path!r} directory"
f" in the {package_name!r} package."
)
if template_root is None:
raise ValueError(
f"The {package_name!r} package was not installed in a"
" way that PackageLoader understands."
)
self._template_root = template_root
def get_source(
self, environment: "Environment", template: str
) -> tuple[str, str, t.Callable[[], bool] | None]:
) -> t.Tuple[str, str, t.Optional[t.Callable[[], bool]]]:
# Use posixpath even on Windows to avoid "drive:" or UNC
# segments breaking out of the search directory. Use normpath to
# convert Windows altsep to sep.
p = os.path.normpath(
posixpath.join(self._template_root, *split_template_path(template))
)
up_to_date: t.Callable[[], bool] | None
up_to_date: t.Optional[t.Callable[[], bool]]
if self._archive is None:
# Package is a directory.
@ -400,30 +368,37 @@ class PackageLoader(BaseLoader):
return source.decode(self.encoding), p, up_to_date
def list_templates(self) -> list[str]:
results: list[str] = []
def list_templates(self) -> t.List[str]:
results: t.List[str] = []
if self._archive is None:
# Package is a directory.
offset = len(self._template_root)
for dirpath, _, filenames in os.walk(self._template_root):
dirpath = dirpath[offset:].lstrip(os.sep)
dirpath = dirpath[offset:].lstrip(os.path.sep)
results.extend(
os.path.join(dirpath, name).replace(os.sep, "/")
os.path.join(dirpath, name).replace(os.path.sep, "/")
for name in filenames
)
else:
files = _get_zipimporter_files(self._loader)
if not hasattr(self._loader, "_files"):
raise TypeError(
"This zip import does not have the required"
" metadata to list templates."
)
# Package is a zip file.
prefix = self._template_root[len(self._archive) :].lstrip(os.sep) + os.sep
prefix = (
self._template_root[len(self._archive) :].lstrip(os.path.sep)
+ os.path.sep
)
offset = len(prefix)
for name in files:
for name in self._loader._files.keys():
# Find names under the templates directory that aren't directories.
if name.startswith(prefix) and name[-1] != os.sep:
results.append(name[offset:].replace(os.sep, "/"))
if name.startswith(prefix) and name[-1] != os.path.sep:
results.append(name[offset:].replace(os.path.sep, "/"))
results.sort()
return results
@ -435,7 +410,7 @@ class DictLoader(BaseLoader):
>>> loader = DictLoader({'index.html': 'source here'})
Because auto reloading is rarely useful this is disabled by default.
Because auto reloading is rarely useful this is disabled per default.
"""
def __init__(self, mapping: t.Mapping[str, str]) -> None:
@ -443,13 +418,13 @@ class DictLoader(BaseLoader):
def get_source(
self, environment: "Environment", template: str
) -> tuple[str, None, t.Callable[[], bool]]:
) -> t.Tuple[str, None, t.Callable[[], bool]]:
if template in self.mapping:
source = self.mapping[template]
return source, None, lambda: source == self.mapping.get(template)
raise TemplateNotFound(template)
def list_templates(self) -> list[str]:
def list_templates(self) -> t.List[str]:
return sorted(self.mapping)
@ -475,14 +450,18 @@ class FunctionLoader(BaseLoader):
self,
load_func: t.Callable[
[str],
str | tuple[str, str | None, t.Callable[[], bool] | None] | None,
t.Optional[
t.Union[
str, t.Tuple[str, t.Optional[str], t.Optional[t.Callable[[], bool]]]
]
],
],
) -> None:
self.load_func = load_func
def get_source(
self, environment: "Environment", template: str
) -> tuple[str, str | None, t.Callable[[], bool] | None]:
) -> t.Tuple[str, t.Optional[str], t.Optional[t.Callable[[], bool]]]:
rv = self.load_func(template)
if rv is None:
@ -515,7 +494,7 @@ class PrefixLoader(BaseLoader):
self.mapping = mapping
self.delimiter = delimiter
def get_loader(self, template: str) -> tuple[BaseLoader, str]:
def get_loader(self, template: str) -> t.Tuple[BaseLoader, str]:
try:
prefix, name = template.split(self.delimiter, 1)
loader = self.mapping[prefix]
@ -525,7 +504,7 @@ class PrefixLoader(BaseLoader):
def get_source(
self, environment: "Environment", template: str
) -> tuple[str, str | None, t.Callable[[], bool] | None]:
) -> t.Tuple[str, t.Optional[str], t.Optional[t.Callable[[], bool]]]:
loader, name = self.get_loader(template)
try:
return loader.get_source(environment, name)
@ -539,7 +518,7 @@ class PrefixLoader(BaseLoader):
self,
environment: "Environment",
name: str,
globals: t.MutableMapping[str, t.Any] | None = None,
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
) -> "Template":
loader, local_name = self.get_loader(name)
try:
@ -549,7 +528,7 @@ class PrefixLoader(BaseLoader):
# (the one that includes the prefix)
raise TemplateNotFound(name) from e
def list_templates(self) -> list[str]:
def list_templates(self) -> t.List[str]:
result = []
for prefix, loader in self.mapping.items():
for template in loader.list_templates():
@ -576,7 +555,7 @@ class ChoiceLoader(BaseLoader):
def get_source(
self, environment: "Environment", template: str
) -> tuple[str, str | None, t.Callable[[], bool] | None]:
) -> t.Tuple[str, t.Optional[str], t.Optional[t.Callable[[], bool]]]:
for loader in self.loaders:
try:
return loader.get_source(environment, template)
@ -589,7 +568,7 @@ class ChoiceLoader(BaseLoader):
self,
environment: "Environment",
name: str,
globals: t.MutableMapping[str, t.Any] | None = None,
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
) -> "Template":
for loader in self.loaders:
try:
@ -598,7 +577,7 @@ class ChoiceLoader(BaseLoader):
pass
raise TemplateNotFound(name)
def list_templates(self) -> list[str]:
def list_templates(self) -> t.List[str]:
found = set()
for loader in self.loaders:
found.update(loader.list_templates())
@ -614,7 +593,10 @@ class ModuleLoader(BaseLoader):
Example usage:
>>> loader = ModuleLoader('/path/to/compiled/templates')
>>> loader = ChoiceLoader([
... ModuleLoader('/path/to/compiled/templates'),
... FileSystemLoader('/path/to/templates')
... ])
Templates can be precompiled with :meth:`Environment.compile_templates`.
"""
@ -661,7 +643,7 @@ class ModuleLoader(BaseLoader):
self,
environment: "Environment",
name: str,
globals: t.MutableMapping[str, t.Any] | None = None,
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
) -> "Template":
key = self.get_template_key(name)
module = f"{self.package_name}.{key}"

View File

@ -17,7 +17,7 @@ class TrackingCodeGenerator(CodeGenerator):
def __init__(self, environment: "Environment") -> None:
super().__init__(environment, "<introspection>", "<introspection>")
self.undeclared_identifiers: set[str] = set()
self.undeclared_identifiers: t.Set[str] = set()
def write(self, x: str) -> None:
"""Don't write."""
@ -31,7 +31,7 @@ class TrackingCodeGenerator(CodeGenerator):
self.undeclared_identifiers.add(param)
def find_undeclared_variables(ast: nodes.Template) -> set[str]:
def find_undeclared_variables(ast: nodes.Template) -> t.Set[str]:
"""Returns a set of all variables in the AST that will be looked up from
the context at runtime. Because at compile time it's not known which
variables will be used depending on the path the execution takes at
@ -56,10 +56,10 @@ def find_undeclared_variables(ast: nodes.Template) -> set[str]:
_ref_types = (nodes.Extends, nodes.FromImport, nodes.Import, nodes.Include)
_RefType = nodes.Extends | nodes.FromImport | nodes.Import | nodes.Include
_RefType = t.Union[nodes.Extends, nodes.FromImport, nodes.Import, nodes.Include]
def find_referenced_templates(ast: nodes.Template) -> t.Iterator[str | None]:
def find_referenced_templates(ast: nodes.Template) -> t.Iterator[t.Optional[str]]:
"""Finds all the referenced templates from the AST. This will return an
iterator over all the hardcoded template extensions, inclusions and
imports. If dynamic inheritance or inclusion is used, `None` will be

View File

@ -13,7 +13,7 @@ from .environment import Environment
from .environment import Template
def native_concat(values: t.Iterable[t.Any]) -> t.Any | None:
def native_concat(values: t.Iterable[t.Any]) -> t.Optional[t.Any]:
"""Return a native Python type from the list of compiled nodes. If
the result is a single node, its value is returned. Otherwise, the
nodes are concatenated as strings. If the result can be parsed with

View File

@ -19,7 +19,7 @@ if t.TYPE_CHECKING:
_NodeBound = t.TypeVar("_NodeBound", bound="Node")
_binop_to_func: dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {
_binop_to_func: t.Dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {
"*": operator.mul,
"/": operator.truediv,
"//": operator.floordiv,
@ -29,13 +29,13 @@ _binop_to_func: dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {
"-": operator.sub,
}
_uaop_to_func: dict[str, t.Callable[[t.Any], t.Any]] = {
_uaop_to_func: t.Dict[str, t.Callable[[t.Any], t.Any]] = {
"not": operator.not_,
"+": operator.pos,
"-": operator.neg,
}
_cmpop_to_func: dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {
_cmpop_to_func: t.Dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {
"eq": operator.eq,
"ne": operator.ne,
"gt": operator.gt,
@ -58,7 +58,7 @@ class NodeType(type):
def __new__(mcs, name, bases, d): # type: ignore
for attr in "fields", "attributes":
storage: list[tuple[str, ...]] = []
storage: t.List[t.Tuple[str, ...]] = []
storage.extend(getattr(bases[0] if bases else object, attr, ()))
storage.extend(d.get(attr, ()))
assert len(bases) <= 1, "multiple inheritance not allowed"
@ -74,7 +74,7 @@ class EvalContext:
"""
def __init__(
self, environment: "Environment", template_name: str | None = None
self, environment: "Environment", template_name: t.Optional[str] = None
) -> None:
self.environment = environment
if callable(environment.autoescape):
@ -91,7 +91,7 @@ class EvalContext:
self.__dict__.update(old)
def get_eval_context(node: "Node", ctx: EvalContext | None) -> EvalContext:
def get_eval_context(node: "Node", ctx: t.Optional[EvalContext]) -> EvalContext:
if ctx is None:
if node.environment is None:
raise RuntimeError(
@ -119,8 +119,8 @@ class Node(metaclass=NodeType):
all nodes automatically.
"""
fields: tuple[str, ...] = ()
attributes: tuple[str, ...] = ("lineno", "environment")
fields: t.Tuple[str, ...] = ()
attributes: t.Tuple[str, ...] = ("lineno", "environment")
abstract = True
lineno: int
@ -137,7 +137,7 @@ class Node(metaclass=NodeType):
f"{type(self).__name__!r} takes 0 or {len(self.fields)}"
f" argument{'s' if len(self.fields) != 1 else ''}"
)
for name, arg in zip(self.fields, fields, strict=False):
for name, arg in zip(self.fields, fields):
setattr(self, name, arg)
for attr in self.attributes:
setattr(self, attr, attributes.pop(attr, None))
@ -146,9 +146,9 @@ class Node(metaclass=NodeType):
def iter_fields(
self,
exclude: t.Container[str] | None = None,
only: t.Container[str] | None = None,
) -> t.Iterator[tuple[str, t.Any]]:
exclude: t.Optional[t.Container[str]] = None,
only: t.Optional[t.Container[str]] = None,
) -> t.Iterator[t.Tuple[str, t.Any]]:
"""This method iterates over all fields that are defined and yields
``(key, value)`` tuples. Per default all fields are returned, but
it's possible to limit that to some fields by providing the `only`
@ -168,8 +168,8 @@ class Node(metaclass=NodeType):
def iter_child_nodes(
self,
exclude: t.Container[str] | None = None,
only: t.Container[str] | None = None,
exclude: t.Optional[t.Container[str]] = None,
only: t.Optional[t.Container[str]] = None,
) -> t.Iterator["Node"]:
"""Iterates over all direct child nodes of the node. This iterates
over all fields and yields the values of they are nodes. If the value
@ -183,7 +183,7 @@ class Node(metaclass=NodeType):
elif isinstance(item, Node):
yield item
def find(self, node_type: type[_NodeBound]) -> _NodeBound | None:
def find(self, node_type: t.Type[_NodeBound]) -> t.Optional[_NodeBound]:
"""Find the first node of a given type. If no such node exists the
return value is `None`.
"""
@ -193,7 +193,7 @@ class Node(metaclass=NodeType):
return None
def find_all(
self, node_type: type[_NodeBound] | tuple[type[_NodeBound], ...]
self, node_type: t.Union[t.Type[_NodeBound], t.Tuple[t.Type[_NodeBound], ...]]
) -> t.Iterator[_NodeBound]:
"""Find all the nodes of a given type. If the type is a tuple,
the check is performed for any of the tuple items.
@ -250,7 +250,7 @@ class Node(metaclass=NodeType):
return f"{type(self).__name__}({args_str})"
def dump(self) -> str:
def _dump(node: Node | t.Any) -> None:
def _dump(node: t.Union[Node, t.Any]) -> None:
if not isinstance(node, Node):
buf.append(repr(node))
return
@ -274,7 +274,7 @@ class Node(metaclass=NodeType):
_dump(value)
buf.append(")")
buf: list[str] = []
buf: t.List[str] = []
_dump(self)
return "".join(buf)
@ -297,7 +297,7 @@ class Template(Node):
"""
fields = ("body",)
body: list[Node]
body: t.List[Node]
class Output(Stmt):
@ -306,7 +306,7 @@ class Output(Stmt):
"""
fields = ("nodes",)
nodes: list["Expr"]
nodes: t.List["Expr"]
class Extends(Stmt):
@ -328,9 +328,9 @@ class For(Stmt):
fields = ("target", "iter", "body", "else_", "test", "recursive")
target: Node
iter: Node
body: list[Node]
else_: list[Node]
test: Node | None
body: t.List[Node]
else_: t.List[Node]
test: t.Optional[Node]
recursive: bool
@ -339,9 +339,9 @@ class If(Stmt):
fields = ("test", "body", "elif_", "else_")
test: Node
body: list[Node]
elif_: list["If"]
else_: list[Node]
body: t.List[Node]
elif_: t.List["If"]
else_: t.List[Node]
class Macro(Stmt):
@ -352,9 +352,9 @@ class Macro(Stmt):
fields = ("name", "args", "defaults", "body")
name: str
args: list["Name"]
defaults: list["Expr"]
body: list[Node]
args: t.List["Name"]
defaults: t.List["Expr"]
body: t.List[Node]
class CallBlock(Stmt):
@ -364,16 +364,16 @@ class CallBlock(Stmt):
fields = ("call", "args", "defaults", "body")
call: "Call"
args: list["Name"]
defaults: list["Expr"]
body: list[Node]
args: t.List["Name"]
defaults: t.List["Expr"]
body: t.List[Node]
class FilterBlock(Stmt):
"""Node for filter sections."""
fields = ("body", "filter")
body: list[Node]
body: t.List[Node]
filter: "Filter"
@ -385,9 +385,9 @@ class With(Stmt):
"""
fields = ("targets", "values", "body")
targets: list["Expr"]
values: list["Expr"]
body: list[Node]
targets: t.List["Expr"]
values: t.List["Expr"]
body: t.List[Node]
class Block(Stmt):
@ -399,7 +399,7 @@ class Block(Stmt):
fields = ("name", "body", "scoped", "required")
name: str
body: list[Node]
body: t.List[Node]
scoped: bool
required: bool
@ -436,7 +436,7 @@ class FromImport(Stmt):
fields = ("template", "names", "with_context")
template: "Expr"
names: list[str | tuple[str, str]]
names: t.List[t.Union[str, t.Tuple[str, str]]]
with_context: bool
@ -461,7 +461,7 @@ class AssignBlock(Stmt):
fields = ("target", "filter", "body")
target: "Expr"
filter: t.Optional["Filter"]
body: list[Node]
body: t.List[Node]
class Expr(Node):
@ -469,7 +469,7 @@ class Expr(Node):
abstract = True
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
"""Return the value of the expression as constant or raise
:exc:`Impossible` if this was not possible.
@ -496,7 +496,7 @@ class BinExpr(Expr):
operator: str
abstract = True
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
eval_ctx = get_eval_context(self, eval_ctx)
# intercepted operators cannot be folded at compile time
@ -520,7 +520,7 @@ class UnaryExpr(Expr):
operator: str
abstract = True
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
eval_ctx = get_eval_context(self, eval_ctx)
# intercepted operators cannot be folded at compile time
@ -584,15 +584,15 @@ class Const(Literal):
fields = ("value",)
value: t.Any
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
return self.value
@classmethod
def from_untrusted(
cls,
value: t.Any,
lineno: int | None = None,
environment: "Environment | None" = None,
lineno: t.Optional[int] = None,
environment: "t.Optional[Environment]" = None,
) -> "Const":
"""Return a const object if the value is representable as
constant value in the generated code, otherwise it will raise
@ -611,7 +611,7 @@ class TemplateData(Literal):
fields = ("data",)
data: str
def as_const(self, eval_ctx: EvalContext | None = None) -> str:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> str:
eval_ctx = get_eval_context(self, eval_ctx)
if eval_ctx.volatile:
raise Impossible()
@ -627,10 +627,10 @@ class Tuple(Literal):
"""
fields = ("items", "ctx")
items: list[Expr]
items: t.List[Expr]
ctx: str
def as_const(self, eval_ctx: EvalContext | None = None) -> tuple[t.Any, ...]:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Tuple[t.Any, ...]:
eval_ctx = get_eval_context(self, eval_ctx)
return tuple(x.as_const(eval_ctx) for x in self.items)
@ -645,9 +645,9 @@ class List(Literal):
"""Any list literal such as ``[1, 2, 3]``"""
fields = ("items",)
items: list[Expr]
items: t.List[Expr]
def as_const(self, eval_ctx: EvalContext | None = None) -> list[t.Any]:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.List[t.Any]:
eval_ctx = get_eval_context(self, eval_ctx)
return [x.as_const(eval_ctx) for x in self.items]
@ -658,9 +658,11 @@ class Dict(Literal):
"""
fields = ("items",)
items: list["Pair"]
items: t.List["Pair"]
def as_const(self, eval_ctx: EvalContext | None = None) -> dict[t.Any, t.Any]:
def as_const(
self, eval_ctx: t.Optional[EvalContext] = None
) -> t.Dict[t.Any, t.Any]:
eval_ctx = get_eval_context(self, eval_ctx)
return dict(x.as_const(eval_ctx) for x in self.items)
@ -672,7 +674,9 @@ class Pair(Helper):
key: Expr
value: Expr
def as_const(self, eval_ctx: EvalContext | None = None) -> tuple[t.Any, t.Any]:
def as_const(
self, eval_ctx: t.Optional[EvalContext] = None
) -> t.Tuple[t.Any, t.Any]:
eval_ctx = get_eval_context(self, eval_ctx)
return self.key.as_const(eval_ctx), self.value.as_const(eval_ctx)
@ -684,7 +688,7 @@ class Keyword(Helper):
key: str
value: Expr
def as_const(self, eval_ctx: EvalContext | None = None) -> tuple[str, t.Any]:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Tuple[str, t.Any]:
eval_ctx = get_eval_context(self, eval_ctx)
return self.key, self.value.as_const(eval_ctx)
@ -697,9 +701,9 @@ class CondExpr(Expr):
fields = ("test", "expr1", "expr2")
test: Expr
expr1: Expr
expr2: Expr | None
expr2: t.Optional[Expr]
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
eval_ctx = get_eval_context(self, eval_ctx)
if self.test.as_const(eval_ctx):
return self.expr1.as_const(eval_ctx)
@ -712,8 +716,8 @@ class CondExpr(Expr):
def args_as_const(
node: t.Union["_FilterTestCommon", "Call"], eval_ctx: EvalContext | None
) -> tuple[list[t.Any], dict[t.Any, t.Any]]:
node: t.Union["_FilterTestCommon", "Call"], eval_ctx: t.Optional[EvalContext]
) -> t.Tuple[t.List[t.Any], t.Dict[t.Any, t.Any]]:
args = [x.as_const(eval_ctx) for x in node.args]
kwargs = dict(x.as_const(eval_ctx) for x in node.kwargs)
@ -736,14 +740,14 @@ class _FilterTestCommon(Expr):
fields = ("node", "name", "args", "kwargs", "dyn_args", "dyn_kwargs")
node: Expr
name: str
args: list[Expr]
kwargs: list[Pair]
dyn_args: Expr | None
dyn_kwargs: Expr | None
args: t.List[Expr]
kwargs: t.List[Pair]
dyn_args: t.Optional[Expr]
dyn_kwargs: t.Optional[Expr]
abstract = True
_is_filter = True
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
eval_ctx = get_eval_context(self, eval_ctx)
if eval_ctx.volatile:
@ -788,9 +792,9 @@ class Filter(_FilterTestCommon):
and is applied to the content of the block.
"""
node: Expr | None # type: ignore
node: t.Optional[Expr] # type: ignore
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
if self.node is None:
raise Impossible()
@ -820,10 +824,10 @@ class Call(Expr):
fields = ("node", "args", "kwargs", "dyn_args", "dyn_kwargs")
node: Expr
args: list[Expr]
kwargs: list[Keyword]
dyn_args: Expr | None
dyn_kwargs: Expr | None
args: t.List[Expr]
kwargs: t.List[Keyword]
dyn_args: t.Optional[Expr]
dyn_kwargs: t.Optional[Expr]
class Getitem(Expr):
@ -834,7 +838,7 @@ class Getitem(Expr):
arg: Expr
ctx: str
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
if self.ctx != "load":
raise Impossible()
@ -858,7 +862,7 @@ class Getattr(Expr):
attr: str
ctx: str
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
if self.ctx != "load":
raise Impossible()
@ -876,14 +880,14 @@ class Slice(Expr):
"""
fields = ("start", "stop", "step")
start: Expr | None
stop: Expr | None
step: Expr | None
start: t.Optional[Expr]
stop: t.Optional[Expr]
step: t.Optional[Expr]
def as_const(self, eval_ctx: EvalContext | None = None) -> slice:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> slice:
eval_ctx = get_eval_context(self, eval_ctx)
def const(obj: Expr | None) -> t.Any | None:
def const(obj: t.Optional[Expr]) -> t.Optional[t.Any]:
if obj is None:
return None
return obj.as_const(eval_ctx)
@ -897,9 +901,9 @@ class Concat(Expr):
"""
fields = ("nodes",)
nodes: list[Expr]
nodes: t.List[Expr]
def as_const(self, eval_ctx: EvalContext | None = None) -> str:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> str:
eval_ctx = get_eval_context(self, eval_ctx)
return "".join(str(x.as_const(eval_ctx)) for x in self.nodes)
@ -911,9 +915,9 @@ class Compare(Expr):
fields = ("expr", "ops")
expr: Expr
ops: list["Operand"]
ops: t.List["Operand"]
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
eval_ctx = get_eval_context(self, eval_ctx)
result = value = self.expr.as_const(eval_ctx)
@ -989,7 +993,7 @@ class And(BinExpr):
operator = "and"
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
eval_ctx = get_eval_context(self, eval_ctx)
return self.left.as_const(eval_ctx) and self.right.as_const(eval_ctx)
@ -999,7 +1003,7 @@ class Or(BinExpr):
operator = "or"
def as_const(self, eval_ctx: EvalContext | None = None) -> t.Any:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> t.Any:
eval_ctx = get_eval_context(self, eval_ctx)
return self.left.as_const(eval_ctx) or self.right.as_const(eval_ctx)
@ -1082,7 +1086,7 @@ class MarkSafe(Expr):
fields = ("expr",)
expr: Expr
def as_const(self, eval_ctx: EvalContext | None = None) -> Markup:
def as_const(self, eval_ctx: t.Optional[EvalContext] = None) -> Markup:
eval_ctx = get_eval_context(self, eval_ctx)
return Markup(self.expr.as_const(eval_ctx))
@ -1097,7 +1101,9 @@ class MarkSafeIfAutoescape(Expr):
fields = ("expr",)
expr: Expr
def as_const(self, eval_ctx: EvalContext | None = None) -> Markup | t.Any:
def as_const(
self, eval_ctx: t.Optional[EvalContext] = None
) -> t.Union[Markup, t.Any]:
eval_ctx = get_eval_context(self, eval_ctx)
if eval_ctx.volatile:
raise Impossible()
@ -1146,7 +1152,7 @@ class Scope(Stmt):
"""An artificial scope."""
fields = ("body",)
body: list[Node]
body: t.List[Node]
class OverlayScope(Stmt):
@ -1165,7 +1171,7 @@ class OverlayScope(Stmt):
fields = ("context", "body")
context: Expr
body: list[Node]
body: t.List[Node]
class EvalContextModifier(Stmt):
@ -1178,7 +1184,7 @@ class EvalContextModifier(Stmt):
"""
fields = ("options",)
options: list[Keyword]
options: t.List[Keyword]
class ScopedEvalContextModifier(EvalContextModifier):
@ -1188,7 +1194,7 @@ class ScopedEvalContextModifier(EvalContextModifier):
"""
fields = ("body",)
body: list[Node]
body: t.List[Node]
# make sure nobody creates custom nodes

View File

@ -25,7 +25,7 @@ def optimize(node: nodes.Node, environment: "Environment") -> nodes.Node:
class Optimizer(NodeTransformer):
def __init__(self, environment: "Environment | None") -> None:
def __init__(self, environment: "t.Optional[Environment]") -> None:
self.environment = environment
def generic_visit(

View File

@ -35,7 +35,7 @@ _statement_keywords = frozenset(
)
_compare_operators = frozenset(["eq", "ne", "lt", "lteq", "gt", "gteq"])
_math_nodes: dict[str, type[nodes.Expr]] = {
_math_nodes: t.Dict[str, t.Type[nodes.Expr]] = {
"add": nodes.Add,
"sub": nodes.Sub,
"mul": nodes.Mul,
@ -54,30 +54,30 @@ class Parser:
self,
environment: "Environment",
source: str,
name: str | None = None,
filename: str | None = None,
state: str | None = None,
name: t.Optional[str] = None,
filename: t.Optional[str] = None,
state: t.Optional[str] = None,
) -> None:
self.environment = environment
self.stream = environment._tokenize(source, name, filename, state)
self.name = name
self.filename = filename
self.closed = False
self.extensions: dict[
str, t.Callable[[Parser], nodes.Node | list[nodes.Node]]
self.extensions: t.Dict[
str, t.Callable[["Parser"], t.Union[nodes.Node, t.List[nodes.Node]]]
] = {}
for extension in environment.iter_extensions():
for tag in extension.tags:
self.extensions[tag] = extension.parse
self._last_identifier = 0
self._tag_stack: list[str] = []
self._end_token_stack: list[tuple[str, ...]] = []
self._tag_stack: t.List[str] = []
self._end_token_stack: t.List[t.Tuple[str, ...]] = []
def fail(
self,
msg: str,
lineno: int | None = None,
exc: type[TemplateSyntaxError] = TemplateSyntaxError,
lineno: t.Optional[int] = None,
exc: t.Type[TemplateSyntaxError] = TemplateSyntaxError,
) -> "te.NoReturn":
"""Convenience method that raises `exc` with the message, passed
line number or last line number as well as the current name and
@ -89,15 +89,15 @@ class Parser:
def _fail_ut_eof(
self,
name: str | None,
end_token_stack: list[tuple[str, ...]],
lineno: int | None,
name: t.Optional[str],
end_token_stack: t.List[t.Tuple[str, ...]],
lineno: t.Optional[int],
) -> "te.NoReturn":
expected: set[str] = set()
expected: t.Set[str] = set()
for exprs in end_token_stack:
expected.update(map(describe_token_expr, exprs))
if end_token_stack:
currently_looking: str | None = " or ".join(
currently_looking: t.Optional[str] = " or ".join(
map(repr, map(describe_token_expr, end_token_stack[-1]))
)
else:
@ -127,7 +127,9 @@ class Parser:
self.fail(" ".join(message), lineno)
def fail_unknown_tag(self, name: str, lineno: int | None = None) -> "te.NoReturn":
def fail_unknown_tag(
self, name: str, lineno: t.Optional[int] = None
) -> "te.NoReturn":
"""Called if the parser encounters an unknown tag. Tries to fail
with a human readable error message that could help to identify
the problem.
@ -136,8 +138,8 @@ class Parser:
def fail_eof(
self,
end_tokens: tuple[str, ...] | None = None,
lineno: int | None = None,
end_tokens: t.Optional[t.Tuple[str, ...]] = None,
lineno: t.Optional[int] = None,
) -> "te.NoReturn":
"""Like fail_unknown_tag but for end of template situations."""
stack = list(self._end_token_stack)
@ -145,7 +147,9 @@ class Parser:
stack.append(end_tokens)
self._fail_ut_eof(None, stack, lineno)
def is_tuple_end(self, extra_end_rules: tuple[str, ...] | None = None) -> bool:
def is_tuple_end(
self, extra_end_rules: t.Optional[t.Tuple[str, ...]] = None
) -> bool:
"""Are we at the end of a tuple?"""
if self.stream.current.type in ("variable_end", "block_end", "rparen"):
return True
@ -153,14 +157,14 @@ class Parser:
return self.stream.current.test_any(extra_end_rules) # type: ignore
return False
def free_identifier(self, lineno: int | None = None) -> nodes.InternalName:
def free_identifier(self, lineno: t.Optional[int] = None) -> nodes.InternalName:
"""Return a new free identifier as :class:`~jinja2.nodes.InternalName`."""
self._last_identifier += 1
rv = object.__new__(nodes.InternalName)
nodes.Node.__init__(rv, f"fi{self._last_identifier}", lineno=lineno)
return rv
def parse_statement(self) -> nodes.Node | list[nodes.Node]:
def parse_statement(self) -> t.Union[nodes.Node, t.List[nodes.Node]]:
"""Parse a single statement."""
token = self.stream.current
if token.type != "name":
@ -190,8 +194,8 @@ class Parser:
self._tag_stack.pop()
def parse_statements(
self, end_tokens: tuple[str, ...], drop_needle: bool = False
) -> list[nodes.Node]:
self, end_tokens: t.Tuple[str, ...], drop_needle: bool = False
) -> t.List[nodes.Node]:
"""Parse multiple statements into a list until one of the end tokens
is reached. This is used to parse the body of statements as it also
parses template data if appropriate. The parser checks first if the
@ -218,7 +222,7 @@ class Parser:
next(self.stream)
return result
def parse_set(self) -> nodes.Assign | nodes.AssignBlock:
def parse_set(self) -> t.Union[nodes.Assign, nodes.AssignBlock]:
"""Parse an assign statement."""
lineno = next(self.stream).lineno
target = self.parse_assign_target(with_namespace=True)
@ -268,8 +272,8 @@ class Parser:
def parse_with(self) -> nodes.With:
node = nodes.With(lineno=next(self.stream).lineno)
targets: list[nodes.Expr] = []
values: list[nodes.Expr] = []
targets: t.List[nodes.Expr] = []
values: t.List[nodes.Expr] = []
while self.stream.current.type != "block_end":
if targets:
self.stream.expect("comma")
@ -462,17 +466,17 @@ class Parser:
self,
with_tuple: bool = True,
name_only: bool = False,
extra_end_rules: tuple[str, ...] | None = None,
extra_end_rules: t.Optional[t.Tuple[str, ...]] = None,
with_namespace: bool = False,
) -> nodes.NSRef | nodes.Name | nodes.Tuple: ...
) -> t.Union[nodes.NSRef, nodes.Name, nodes.Tuple]: ...
def parse_assign_target(
self,
with_tuple: bool = True,
name_only: bool = False,
extra_end_rules: tuple[str, ...] | None = None,
extra_end_rules: t.Optional[t.Tuple[str, ...]] = None,
with_namespace: bool = False,
) -> nodes.NSRef | nodes.Name | nodes.Tuple:
) -> t.Union[nodes.NSRef, nodes.Name, nodes.Tuple]:
"""Parse an assignment target. As Jinja allows assignments to
tuples, this function can parse all allowed assignment targets. Per
default assignments to tuples are parsed, that can be disable however
@ -483,18 +487,21 @@ class Parser:
"""
target: nodes.Expr
if name_only:
if with_namespace and self.stream.look().type == "dot":
token = self.stream.expect("name")
next(self.stream) # dot
attr = self.stream.expect("name")
target = nodes.NSRef(token.value, attr.value, lineno=token.lineno)
elif name_only:
token = self.stream.expect("name")
target = nodes.Name(token.value, "store", lineno=token.lineno)
else:
if with_tuple:
target = self.parse_tuple(
simplified=True,
extra_end_rules=extra_end_rules,
with_namespace=with_namespace,
simplified=True, extra_end_rules=extra_end_rules
)
else:
target = self.parse_primary(with_namespace=with_namespace)
target = self.parse_primary()
target.set_ctx("store")
@ -517,7 +524,7 @@ class Parser:
def parse_condexpr(self) -> nodes.Expr:
lineno = self.stream.current.lineno
expr1 = self.parse_or()
expr3: nodes.Expr | None
expr3: t.Optional[nodes.Expr]
while self.stream.skip_if("name:if"):
expr2 = self.parse_or()
@ -636,25 +643,17 @@ class Parser:
node = self.parse_filter_expr(node)
return node
def parse_primary(self, with_namespace: bool = False) -> nodes.Expr:
"""Parse a name or literal value. If ``with_namespace`` is enabled, also
parse namespace attr refs, for use in assignments."""
def parse_primary(self) -> nodes.Expr:
token = self.stream.current
node: nodes.Expr
if token.type == "name":
next(self.stream)
if token.value in ("true", "false", "True", "False"):
node = nodes.Const(token.value in ("true", "True"), lineno=token.lineno)
elif token.value in ("none", "None"):
node = nodes.Const(None, lineno=token.lineno)
elif with_namespace and self.stream.current.type == "dot":
# If namespace attributes are allowed at this point, and the next
# token is a dot, produce a namespace reference.
next(self.stream)
attr = self.stream.expect("name")
node = nodes.NSRef(token.value, attr.value, lineno=token.lineno)
else:
node = nodes.Name(token.value, "load", lineno=token.lineno)
next(self.stream)
elif token.type == "string":
next(self.stream)
buf = [token.value]
@ -682,19 +681,17 @@ class Parser:
self,
simplified: bool = False,
with_condexpr: bool = True,
extra_end_rules: tuple[str, ...] | None = None,
extra_end_rules: t.Optional[t.Tuple[str, ...]] = None,
explicit_parentheses: bool = False,
with_namespace: bool = False,
) -> nodes.Tuple | nodes.Expr:
) -> t.Union[nodes.Tuple, nodes.Expr]:
"""Works like `parse_expression` but if multiple expressions are
delimited by a comma a :class:`~jinja2.nodes.Tuple` node is created.
This method could also return a regular expression instead of a tuple
if no commas where found.
The default parsing mode is a full tuple. If `simplified` is `True`
only names and literals are parsed; ``with_namespace`` allows namespace
attr refs as well. The `no_condexpr` parameter is forwarded to
:meth:`parse_expression`.
only names and literals are parsed. The `no_condexpr` parameter is
forwarded to :meth:`parse_expression`.
Because tuples do not require delimiters and may end in a bogus comma
an extra hint is needed that marks the end of a tuple. For example
@ -707,16 +704,15 @@ class Parser:
"""
lineno = self.stream.current.lineno
if simplified:
def parse() -> nodes.Expr:
return self.parse_primary(with_namespace=with_namespace)
parse = self.parse_primary
elif with_condexpr:
parse = self.parse_expression
else:
def parse() -> nodes.Expr:
return self.parse_expression(with_condexpr=with_condexpr)
return self.parse_expression(with_condexpr=False)
args: list[nodes.Expr] = []
args: t.List[nodes.Expr] = []
is_tuple = False
while True:
@ -749,7 +745,7 @@ class Parser:
def parse_list(self) -> nodes.List:
token = self.stream.expect("lbracket")
items: list[nodes.Expr] = []
items: t.List[nodes.Expr] = []
while self.stream.current.type != "rbracket":
if items:
self.stream.expect("comma")
@ -761,7 +757,7 @@ class Parser:
def parse_dict(self) -> nodes.Dict:
token = self.stream.expect("lbrace")
items: list[nodes.Pair] = []
items: t.List[nodes.Pair] = []
while self.stream.current.type != "rbrace":
if items:
self.stream.expect("comma")
@ -802,7 +798,9 @@ class Parser:
break
return node
def parse_subscript(self, node: nodes.Expr) -> nodes.Getattr | nodes.Getitem:
def parse_subscript(
self, node: nodes.Expr
) -> t.Union[nodes.Getattr, nodes.Getitem]:
token = next(self.stream)
arg: nodes.Expr
@ -818,7 +816,7 @@ class Parser:
arg = nodes.Const(attr_token.value, lineno=attr_token.lineno)
return nodes.Getitem(node, arg, "load", lineno=token.lineno)
if token.type == "lbracket":
args: list[nodes.Expr] = []
args: t.List[nodes.Expr] = []
while self.stream.current.type != "rbracket":
if args:
self.stream.expect("comma")
@ -833,7 +831,7 @@ class Parser:
def parse_subscribed(self) -> nodes.Expr:
lineno = self.stream.current.lineno
args: list[nodes.Expr | None]
args: t.List[t.Optional[nodes.Expr]]
if self.stream.current.type == "colon":
next(self.stream)
@ -865,11 +863,11 @@ class Parser:
def parse_call_args(
self,
) -> tuple[
list[nodes.Expr],
list[nodes.Keyword],
nodes.Expr | None,
nodes.Expr | None,
) -> t.Tuple[
t.List[nodes.Expr],
t.List[nodes.Keyword],
t.Optional[nodes.Expr],
t.Optional[nodes.Expr],
]:
token = self.stream.expect("lparen")
args = []
@ -927,8 +925,8 @@ class Parser:
return nodes.Call(node, args, kwargs, dyn_args, dyn_kwargs, lineno=token.lineno)
def parse_filter(
self, node: nodes.Expr | None, start_inline: bool = False
) -> nodes.Expr | None:
self, node: t.Optional[nodes.Expr], start_inline: bool = False
) -> t.Optional[nodes.Expr]:
while self.stream.current.type == "pipe" or start_inline:
if not start_inline:
next(self.stream)
@ -961,7 +959,7 @@ class Parser:
next(self.stream)
name += "." + self.stream.expect("name").value
dyn_args = dyn_kwargs = None
kwargs: list[nodes.Keyword] = []
kwargs: t.List[nodes.Keyword] = []
if self.stream.current.type == "lparen":
args, kwargs, dyn_args, dyn_kwargs = self.parse_call_args()
elif self.stream.current.type in {
@ -987,9 +985,11 @@ class Parser:
node = nodes.Not(node, lineno=token.lineno)
return node
def subparse(self, end_tokens: tuple[str, ...] | None = None) -> list[nodes.Node]:
body: list[nodes.Node] = []
data_buffer: list[nodes.Node] = []
def subparse(
self, end_tokens: t.Optional[t.Tuple[str, ...]] = None
) -> t.List[nodes.Node]:
body: t.List[nodes.Node] = []
data_buffer: t.List[nodes.Node] = []
add_data = data_buffer.append
if end_tokens is not None:

View File

@ -92,12 +92,12 @@ def str_join(seq: t.Iterable[t.Any]) -> str:
def new_context(
environment: "Environment",
template_name: str | None,
blocks: dict[str, t.Callable[["Context"], t.Iterator[str]]],
vars: dict[str, t.Any] | None = None,
template_name: t.Optional[str],
blocks: t.Dict[str, t.Callable[["Context"], t.Iterator[str]]],
vars: t.Optional[t.Dict[str, t.Any]] = None,
shared: bool = False,
globals: t.MutableMapping[str, t.Any] | None = None,
locals: t.Mapping[str, t.Any] | None = None,
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
locals: t.Optional[t.Mapping[str, t.Any]] = None,
) -> "Context":
"""Internal helper for context creation."""
if vars is None:
@ -165,16 +165,16 @@ class Context:
def __init__(
self,
environment: "Environment",
parent: dict[str, t.Any],
name: str | None,
blocks: dict[str, t.Callable[["Context"], t.Iterator[str]]],
globals: t.MutableMapping[str, t.Any] | None = None,
parent: t.Dict[str, t.Any],
name: t.Optional[str],
blocks: t.Dict[str, t.Callable[["Context"], t.Iterator[str]]],
globals: t.Optional[t.MutableMapping[str, t.Any]] = None,
):
self.parent = parent
self.vars: dict[str, t.Any] = {}
self.environment: Environment = environment
self.vars: t.Dict[str, t.Any] = {}
self.environment: "Environment" = environment
self.eval_ctx = EvalContext(self.environment, name)
self.exported_vars: set[str] = set()
self.exported_vars: t.Set[str] = set()
self.name = name
self.globals_keys = set() if globals is None else set(globals)
@ -244,11 +244,11 @@ class Context:
return missing
def get_exported(self) -> dict[str, t.Any]:
def get_exported(self) -> t.Dict[str, t.Any]:
"""Get a new dict with the exported variables."""
return {k: self.vars[k] for k in self.exported_vars}
def get_all(self) -> dict[str, t.Any]:
def get_all(self) -> t.Dict[str, t.Any]:
"""Return the complete context as dict including the exported
variables. For optimizations reasons this might not return an
actual copy so be careful with using it.
@ -261,10 +261,10 @@ class Context:
@internalcode
def call(
__self, # noqa: B902
__self,
__obj: t.Callable[..., t.Any],
*args: t.Any,
**kwargs: t.Any,
**kwargs: t.Any, # noqa: B902
) -> t.Union[t.Any, "Undefined"]:
"""Call the callable with the arguments and keyword arguments
provided but inject the active context or environment as first
@ -307,7 +307,7 @@ class Context:
" StopIteration exception"
)
def derived(self, locals: dict[str, t.Any] | None = None) -> "Context":
def derived(self, locals: t.Optional[t.Dict[str, t.Any]] = None) -> "Context":
"""Internal helper function to create a derived context. This is
used in situations where the system needs a new context in the same
template that is independent.
@ -348,7 +348,7 @@ class BlockReference:
self,
name: str,
context: "Context",
stack: list[t.Callable[["Context"], t.Iterator[str]]],
stack: t.List[t.Callable[["Context"], t.Iterator[str]]],
depth: int,
) -> None:
self.name = name
@ -367,7 +367,7 @@ class BlockReference:
@internalcode
async def _async_call(self) -> str:
rv = self._context.environment.concat( # type: ignore
rv = concat(
[x async for x in self._stack[self._depth](self._context)] # type: ignore
)
@ -381,9 +381,7 @@ class BlockReference:
if self._context.environment.is_async:
return self._async_call() # type: ignore
rv = self._context.environment.concat( # type: ignore
self._stack[self._depth](self._context)
)
rv = concat(self._stack[self._depth](self._context))
if self._context.eval_ctx.autoescape:
return Markup(rv)
@ -399,7 +397,7 @@ class LoopContext:
#: Current iteration of the loop, starting at 0.
index0 = -1
_length: int | None = None
_length: t.Optional[int] = None
_after: t.Any = missing
_current: t.Any = missing
_before: t.Any = missing
@ -408,7 +406,7 @@ class LoopContext:
def __init__(
self,
iterable: t.Iterable[V],
undefined: type["Undefined"],
undefined: t.Type["Undefined"],
recurse: t.Optional["LoopRenderFunc"] = None,
depth0: int = 0,
) -> None:
@ -558,7 +556,7 @@ class LoopContext:
def __iter__(self) -> "LoopContext":
return self
def __next__(self) -> tuple[t.Any, "LoopContext"]:
def __next__(self) -> t.Tuple[t.Any, "LoopContext"]:
if self._after is not missing:
rv = self._after
self._after = missing
@ -593,7 +591,7 @@ class AsyncLoopContext(LoopContext):
@staticmethod
def _to_iterator( # type: ignore
iterable: t.Iterable[V] | t.AsyncIterable[V],
iterable: t.Union[t.Iterable[V], t.AsyncIterable[V]],
) -> t.AsyncIterator[V]:
return auto_aiter(iterable)
@ -646,7 +644,7 @@ class AsyncLoopContext(LoopContext):
def __aiter__(self) -> "AsyncLoopContext":
return self
async def __anext__(self) -> tuple[t.Any, "AsyncLoopContext"]:
async def __anext__(self) -> t.Tuple[t.Any, "AsyncLoopContext"]:
if self._after is not missing:
rv = self._after
self._after = missing
@ -667,11 +665,11 @@ class Macro:
environment: "Environment",
func: t.Callable[..., str],
name: str,
arguments: list[str],
arguments: t.List[str],
catch_kwargs: bool,
catch_varargs: bool,
caller: bool,
default_autoescape: bool | None = None,
default_autoescape: t.Optional[bool] = None,
):
self._environment = environment
self._func = func
@ -769,7 +767,7 @@ class Macro:
return self._invoke(arguments, autoescape)
async def _async_invoke(self, arguments: list[t.Any], autoescape: bool) -> str:
async def _async_invoke(self, arguments: t.List[t.Any], autoescape: bool) -> str:
rv = await self._func(*arguments) # type: ignore
if autoescape:
@ -777,7 +775,7 @@ class Macro:
return rv # type: ignore
def _invoke(self, arguments: list[t.Any], autoescape: bool) -> str:
def _invoke(self, arguments: t.List[t.Any], autoescape: bool) -> str:
if self._environment.is_async:
return self._async_invoke(arguments, autoescape) # type: ignore
@ -794,8 +792,8 @@ class Macro:
class Undefined:
"""The default undefined type. This can be printed, iterated, and treated as
a boolean. Any other operation will raise an :exc:`UndefinedError`.
"""The default undefined type. This undefined type can be printed and
iterated over, but every other access will raise an :exc:`UndefinedError`:
>>> foo = Undefined(name='foo')
>>> str(foo)
@ -817,10 +815,10 @@ class Undefined:
def __init__(
self,
hint: str | None = None,
hint: t.Optional[str] = None,
obj: t.Any = missing,
name: str | None = None,
exc: type[TemplateRuntimeError] = UndefinedError,
name: t.Optional[str] = None,
exc: t.Type[TemplateRuntimeError] = UndefinedError,
) -> None:
self._undefined_hint = hint
self._undefined_obj = obj
@ -860,11 +858,7 @@ class Undefined:
@internalcode
def __getattr__(self, name: str) -> t.Any:
# 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:] == "__":
if name[:2] == "__":
raise AttributeError(name)
return self._fail_with_undefined_error()
@ -910,8 +904,8 @@ class Undefined:
def make_logging_undefined(
logger: t.Optional["logging.Logger"] = None, base: type[Undefined] = Undefined
) -> type[Undefined]:
logger: t.Optional["logging.Logger"] = None, base: t.Type[Undefined] = Undefined
) -> t.Type[Undefined]:
"""Given a logger object this returns a new undefined class that will
log certain failures. It will log iterations and printing. If no
logger is given a default logger is created.
@ -988,20 +982,10 @@ class ChainableUndefined(Undefined):
def __html__(self) -> str:
return str(self)
def __getattr__(self, name: str) -> "ChainableUndefined":
# 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)
def __getattr__(self, _: str) -> "ChainableUndefined":
return self
def __getitem__(self, _name: str) -> "ChainableUndefined": # type: ignore[override]
return self
__getitem__ = __getattr__ # type: ignore
class DebugUndefined(Undefined):
@ -1060,3 +1044,13 @@ class StrictUndefined(Undefined):
__iter__ = __str__ = __len__ = Undefined._fail_with_undefined_error
__eq__ = __ne__ = __bool__ = __hash__ = Undefined._fail_with_undefined_error
__contains__ = Undefined._fail_with_undefined_error
# Remove slots attributes, after the metaclass is applied they are
# unneeded and contain wrong data for subclasses.
del (
Undefined.__slots__,
ChainableUndefined.__slots__,
DebugUndefined.__slots__,
StrictUndefined.__slots__,
)

View File

@ -5,12 +5,11 @@ Useful when the template itself comes from an untrusted source.
import operator
import types
import typing as t
from _string import formatter_field_name_split # type: ignore
from collections import abc
from collections import deque
from functools import update_wrapper
from string import Formatter
from _string import formatter_field_name_split # type: ignore
from markupsafe import EscapeFormatter
from markupsafe import Markup
@ -25,10 +24,10 @@ F = t.TypeVar("F", bound=t.Callable[..., t.Any])
MAX_RANGE = 100000
#: Unsafe function attributes.
UNSAFE_FUNCTION_ATTRIBUTES: set[str] = set()
UNSAFE_FUNCTION_ATTRIBUTES: t.Set[str] = set()
#: Unsafe method attributes. Function attributes are unsafe for methods too.
UNSAFE_METHOD_ATTRIBUTES: set[str] = set()
UNSAFE_METHOD_ATTRIBUTES: t.Set[str] = set()
#: unsafe generator attributes.
UNSAFE_GENERATOR_ATTRIBUTES = {"gi_frame", "gi_code"}
@ -39,7 +38,7 @@ UNSAFE_COROUTINE_ATTRIBUTES = {"cr_frame", "cr_code"}
#: unsafe attributes on async generators
UNSAFE_ASYNC_GENERATOR_ATTRIBUTES = {"ag_code", "ag_frame"}
_mutable_spec: tuple[tuple[type[t.Any], frozenset[str]], ...] = (
_mutable_spec: t.Tuple[t.Tuple[t.Type[t.Any], t.FrozenSet[str]], ...] = (
(
abc.MutableSet,
frozenset(
@ -61,9 +60,7 @@ _mutable_spec: tuple[tuple[type[t.Any], frozenset[str]], ...] = (
),
(
abc.MutableSequence,
frozenset(
["append", "clear", "pop", "reverse", "insert", "sort", "extend", "remove"]
),
frozenset(["append", "reverse", "insert", "sort", "extend", "remove"]),
),
(
deque,
@ -84,6 +81,20 @@ _mutable_spec: tuple[tuple[type[t.Any], frozenset[str]], ...] = (
)
def inspect_format_method(callable: t.Callable[..., t.Any]) -> t.Optional[str]:
if not isinstance(
callable, (types.MethodType, types.BuiltinMethodType)
) or callable.__name__ not in ("format", "format_map"):
return None
obj = callable.__self__
if isinstance(obj, str):
return obj
return None
def safe_range(*args: int) -> range:
"""A range that can't generate ranges with a length of more than
MAX_RANGE items.
@ -190,7 +201,7 @@ class SandboxedEnvironment(Environment):
#: default callback table for the binary operators. A copy of this is
#: available on each instance of a sandboxed environment as
#: :attr:`binop_table`
default_binop_table: dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {
default_binop_table: t.Dict[str, t.Callable[[t.Any, t.Any], t.Any]] = {
"+": operator.add,
"-": operator.sub,
"*": operator.mul,
@ -203,7 +214,7 @@ class SandboxedEnvironment(Environment):
#: default callback table for the unary operators. A copy of this is
#: available on each instance of a sandboxed environment as
#: :attr:`unop_table`
default_unop_table: dict[str, t.Callable[[t.Any], t.Any]] = {
default_unop_table: t.Dict[str, t.Callable[[t.Any], t.Any]] = {
"+": operator.pos,
"-": operator.neg,
}
@ -222,7 +233,7 @@ class SandboxedEnvironment(Environment):
#: interested in.
#:
#: .. versionadded:: 2.6
intercepted_binops: frozenset[str] = frozenset()
intercepted_binops: t.FrozenSet[str] = frozenset()
#: a set of unary operators that should be intercepted. Each operator
#: that is added to this set (empty by default) is delegated to the
@ -237,7 +248,7 @@ class SandboxedEnvironment(Environment):
#: interested in.
#:
#: .. versionadded:: 2.6
intercepted_unops: frozenset[str] = frozenset()
intercepted_unops: t.FrozenSet[str] = frozenset()
def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
super().__init__(*args, **kwargs)
@ -285,7 +296,9 @@ class SandboxedEnvironment(Environment):
"""
return self.unop_table[operator](arg)
def getitem(self, obj: t.Any, argument: str | t.Any) -> t.Any | Undefined:
def getitem(
self, obj: t.Any, argument: t.Union[str, t.Any]
) -> t.Union[t.Any, Undefined]:
"""Subscribe an object from sandboxed code."""
try:
return obj[argument]
@ -301,15 +314,12 @@ class SandboxedEnvironment(Environment):
except AttributeError:
pass
else:
fmt = self.wrap_str_format(value)
if fmt is not None:
return fmt
if self.is_safe_attribute(obj, argument, value):
return value
return self.unsafe_undefined(obj, argument)
return self.undefined(obj=obj, name=argument)
def getattr(self, obj: t.Any, attribute: str) -> t.Any | Undefined:
def getattr(self, obj: t.Any, attribute: str) -> t.Union[t.Any, Undefined]:
"""Subscribe an object from sandboxed code and prefer the
attribute. The attribute passed *must* be a bytestring.
"""
@ -321,9 +331,6 @@ class SandboxedEnvironment(Environment):
except (TypeError, LookupError):
pass
else:
fmt = self.wrap_str_format(value)
if fmt is not None:
return fmt
if self.is_safe_attribute(obj, attribute, value):
return value
return self.unsafe_undefined(obj, attribute)
@ -339,49 +346,34 @@ class SandboxedEnvironment(Environment):
exc=SecurityError,
)
def wrap_str_format(self, value: t.Any) -> t.Callable[..., str] | None:
"""If the given value is a ``str.format`` or ``str.format_map`` method,
return a new function than handles sandboxing. This is done at access
rather than in :meth:`call`, so that calls made without ``call`` are
also sandboxed.
def format_string(
self,
s: str,
args: t.Tuple[t.Any, ...],
kwargs: t.Dict[str, t.Any],
format_func: t.Optional[t.Callable[..., t.Any]] = None,
) -> str:
"""If a format call is detected, then this is routed through this
method so that our safety sandbox can be used for it.
"""
if not isinstance(
value, (types.MethodType, types.BuiltinMethodType)
) or value.__name__ not in ("format", "format_map"):
return None
f_self: t.Any = value.__self__
if not isinstance(f_self, str):
return None
str_type: type[str] = type(f_self)
is_format_map = value.__name__ == "format_map"
formatter: SandboxedFormatter
if isinstance(f_self, Markup):
formatter = SandboxedEscapeFormatter(self, escape=f_self.escape)
if isinstance(s, Markup):
formatter = SandboxedEscapeFormatter(self, escape=s.escape)
else:
formatter = SandboxedFormatter(self)
vformat = formatter.vformat
if format_func is not None and format_func.__name__ == "format_map":
if len(args) != 1 or kwargs:
raise TypeError(
"format_map() takes exactly one argument"
f" {len(args) + (kwargs is not None)} given"
)
def wrapper(*args: t.Any, **kwargs: t.Any) -> str:
if is_format_map:
if kwargs:
raise TypeError("format_map() takes no keyword arguments")
kwargs = args[0]
args = ()
if len(args) != 1:
raise TypeError(
f"format_map() takes exactly one argument ({len(args)} given)"
)
kwargs = args[0]
args = ()
return str_type(vformat(f_self, args, kwargs))
return update_wrapper(wrapper, value)
rv = formatter.vformat(s, args, kwargs)
return type(s)(rv)
def call(
__self, # noqa: B902
@ -391,6 +383,9 @@ class SandboxedEnvironment(Environment):
**kwargs: t.Any,
) -> t.Any:
"""Call an object from sandboxed code."""
fmt = inspect_format_method(__obj)
if fmt is not None:
return __self.format_string(fmt, args, kwargs, __obj)
# the double prefixes are to avoid double keyword argument
# errors when proxying the call.
@ -419,7 +414,7 @@ class SandboxedFormatter(Formatter):
def get_field(
self, field_name: str, args: t.Sequence[t.Any], kwargs: t.Mapping[str, t.Any]
) -> tuple[t.Any, str]:
) -> t.Tuple[t.Any, str]:
first, rest = formatter_field_name_split(field_name)
obj = self.get_value(first, args, kwargs)
for is_attr, i in rest:

View File

@ -18,17 +18,8 @@ if t.TYPE_CHECKING:
F = t.TypeVar("F", bound=t.Callable[..., t.Any])
class _MissingType:
def __repr__(self) -> str:
return "missing"
def __reduce__(self) -> str:
return "missing"
missing: t.Any = _MissingType()
"""Special singleton representing missing values for the runtime."""
# special singleton representing missing values for the runtime
missing: t.Any = type("MissingType", (), {"__repr__": lambda x: "missing"})()
internal_code: t.MutableSet[CodeType] = set()
@ -161,7 +152,7 @@ def import_string(import_name: str, silent: bool = False) -> t.Any:
raise
def open_if_exists(filename: str, mode: str = "rb") -> t.IO[t.Any] | None:
def open_if_exists(filename: str, mode: str = "rb") -> t.Optional[t.IO[t.Any]]:
"""Returns a file descriptor for the filename if that file exists,
otherwise ``None``.
"""
@ -229,10 +220,10 @@ _email_re = re.compile(r"^\S+@\w[\w.-]*\.\w+$")
def urlize(
text: str,
trim_url_limit: int | None = None,
rel: str | None = None,
target: str | None = None,
extra_schemes: t.Iterable[str] | None = None,
trim_url_limit: t.Optional[int] = None,
rel: t.Optional[str] = None,
target: t.Optional[str] = None,
extra_schemes: t.Optional[t.Iterable[str]] = None,
) -> str:
"""Convert URLs in text into clickable links.
@ -333,8 +324,6 @@ def urlize(
elif (
"@" in middle
and not middle.startswith("www.")
# ignore values like `@a@b`
and not middle.startswith("@")
and ":" not in middle
and _email_re.match(middle)
):
@ -438,8 +427,8 @@ class LRUCache:
def __init__(self, capacity: int) -> None:
self.capacity = capacity
self._mapping: dict[t.Any, t.Any] = {}
self._queue: deque[t.Any] = deque()
self._mapping: t.Dict[t.Any, t.Any] = {}
self._queue: "te.Deque[t.Any]" = deque()
self._postinit()
def _postinit(self) -> None:
@ -461,10 +450,10 @@ class LRUCache:
self.__dict__.update(d)
self._postinit()
def __getnewargs__(self) -> tuple[t.Any, ...]:
def __getnewargs__(self) -> t.Tuple[t.Any, ...]:
return (self.capacity,)
def copy(self) -> "te.Self":
def copy(self) -> "LRUCache":
"""Return a shallow copy of the instance."""
rv = self.__class__(self.capacity)
rv._mapping.update(self._mapping)
@ -552,7 +541,7 @@ class LRUCache:
except ValueError:
pass
def items(self) -> t.Iterable[tuple[t.Any, t.Any]]:
def items(self) -> t.Iterable[t.Tuple[t.Any, t.Any]]:
"""Return a list of items."""
result = [(key, self._mapping[key]) for key in list(self._queue)]
result.reverse()
@ -583,7 +572,7 @@ def select_autoescape(
disabled_extensions: t.Collection[str] = (),
default_for_string: bool = True,
default: bool = False,
) -> t.Callable[[str | None], bool]:
) -> t.Callable[[t.Optional[str]], bool]:
"""Intelligently sets the initial value of autoescaping based on the
filename of the template. This is the recommended way to configure
autoescaping if you do not want to write a custom function yourself.
@ -621,7 +610,7 @@ def select_autoescape(
enabled_patterns = tuple(f".{x.lstrip('.').lower()}" for x in enabled_extensions)
disabled_patterns = tuple(f".{x.lstrip('.').lower()}" for x in disabled_extensions)
def autoescape(template_name: str | None) -> bool:
def autoescape(template_name: t.Optional[str]) -> bool:
if template_name is None:
return default_for_string
template_name = template_name.lower()
@ -635,7 +624,7 @@ def select_autoescape(
def htmlsafe_json_dumps(
obj: t.Any, dumps: t.Callable[..., str] | None = None, **kwargs: t.Any
obj: t.Any, dumps: t.Optional[t.Callable[..., str]] = None, **kwargs: t.Any
) -> markupsafe.Markup:
"""Serialize an object to a string of JSON with :func:`json.dumps`,
then replace HTML-unsafe characters with Unicode escapes and mark

View File

@ -25,7 +25,7 @@ class NodeVisitor:
(return value `None`) the `generic_visit` visitor is used instead.
"""
def get_visitor(self, node: Node) -> "VisitCallable | None":
def get_visitor(self, node: Node) -> "t.Optional[VisitCallable]":
"""Return the visitor function for this node or `None` if no visitor
exists for this node. In that case the generic visit function is
used instead.
@ -80,7 +80,7 @@ class NodeTransformer(NodeVisitor):
setattr(node, field, new_node)
return node
def visit_list(self, node: Node, *args: t.Any, **kwargs: t.Any) -> list[Node]:
def visit_list(self, node: Node, *args: t.Any, **kwargs: t.Any) -> t.List[Node]:
"""As transformers may return lists in some places this method
can be used to enforce a list as return value.
"""

View File

@ -1,22 +1,11 @@
import asyncio
from pathlib import Path
import pytest
import trio
from jinja2 import loaders
from jinja2.environment import Environment
def _asyncio_run(async_fn, *args):
return asyncio.run(async_fn(*args))
@pytest.fixture(params=[_asyncio_run, trio.run], ids=["asyncio", "trio"])
def run_async_fn(request):
return request.param
@pytest.fixture
def env():
"""returns a new environment."""

View File

@ -323,6 +323,8 @@ class TestUndefined:
assert und1 == und2
assert und1 != 42
assert hash(und1) == hash(und2) == hash(Undefined())
with pytest.raises(AttributeError):
getattr(Undefined, "__slots__") # noqa: B009
def test_chainable_undefined(self):
env = Environment(undefined=ChainableUndefined)
@ -333,6 +335,8 @@ class TestUndefined:
assert env.from_string("{{ foo.missing }}").render(foo=42) == ""
assert env.from_string("{{ not missing }}").render() == "True"
pytest.raises(UndefinedError, env.from_string("{{ missing - 1}}").render)
with pytest.raises(AttributeError):
getattr(ChainableUndefined, "__slots__") # noqa: B009
# The following tests ensure subclass functionality works as expected
assert env.from_string('{{ missing.bar["baz"] }}').render() == ""
@ -364,6 +368,8 @@ class TestUndefined:
str(DebugUndefined(hint=undefined_hint))
== f"{{{{ undefined value printed: {undefined_hint} }}}}"
)
with pytest.raises(AttributeError):
getattr(DebugUndefined, "__slots__") # noqa: B009
def test_strict_undefined(self):
env = Environment(undefined=StrictUndefined)
@ -380,6 +386,8 @@ class TestUndefined:
env.from_string('{{ missing|default("default", true) }}').render()
== "default"
)
with pytest.raises(AttributeError):
getattr(StrictUndefined, "__slots__") # noqa: B009
assert env.from_string('{{ "foo" if false }}').render() == ""
def test_indexing_gives_undefined(self):
@ -425,11 +433,3 @@ class TestLowLevel:
env = CustomEnvironment()
tmpl = env.from_string("{{ foo }}")
assert tmpl.render() == "resolve-foo"
def test_overlay_enable_async(env):
assert not env.is_async
assert not env.overlay().is_async
env_async = env.overlay(enable_async=True)
assert env_async.is_async
assert not env_async.overlay(enable_async=False).is_async

View File

@ -1,3 +1,5 @@
import asyncio
import pytest
from jinja2 import ChainableUndefined
@ -11,7 +13,7 @@ from jinja2.exceptions import UndefinedError
from jinja2.nativetypes import NativeEnvironment
def test_basic_async(run_async_fn):
def test_basic_async():
t = Template(
"{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}", enable_async=True
)
@ -19,11 +21,11 @@ def test_basic_async(run_async_fn):
async def func():
return await t.render_async()
rv = run_async_fn(func)
rv = asyncio.run(func())
assert rv == "[1][2][3]"
def test_await_on_calls(run_async_fn):
def test_await_on_calls():
t = Template("{{ async_func() + normal_func() }}", enable_async=True)
async def async_func():
@ -35,7 +37,7 @@ def test_await_on_calls(run_async_fn):
async def func():
return await t.render_async(async_func=async_func, normal_func=normal_func)
rv = run_async_fn(func)
rv = asyncio.run(func())
assert rv == "65"
@ -52,7 +54,7 @@ def test_await_on_calls_normal_render():
assert rv == "65"
def test_await_and_macros(run_async_fn):
def test_await_and_macros():
t = Template(
"{% macro foo(x) %}[{{ x }}][{{ async_func() }}]{% endmacro %}{{ foo(42) }}",
enable_async=True,
@ -64,11 +66,11 @@ def test_await_and_macros(run_async_fn):
async def func():
return await t.render_async(async_func=async_func)
rv = run_async_fn(func)
rv = asyncio.run(func())
assert rv == "[42][42]"
def test_async_blocks(run_async_fn):
def test_async_blocks():
t = Template(
"{% block foo %}<Test>{% endblock %}{{ self.foo() }}",
enable_async=True,
@ -78,7 +80,7 @@ def test_async_blocks(run_async_fn):
async def func():
return await t.render_async()
rv = run_async_fn(func)
rv = asyncio.run(func())
assert rv == "<Test><Test>"
@ -154,8 +156,8 @@ class TestAsyncImports:
test_env_async.from_string('{% from "foo" import bar, with, context %}')
test_env_async.from_string('{% from "foo" import bar, with with context %}')
def test_exports(self, test_env_async, run_async_fn):
coro_fn = test_env_async.from_string(
def test_exports(self, test_env_async):
coro = test_env_async.from_string(
"""
{% macro toplevel() %}...{% endmacro %}
{% macro __private() %}...{% endmacro %}
@ -164,9 +166,9 @@ class TestAsyncImports:
{% macro notthere() %}{% endmacro %}
{% endfor %}
"""
)._get_default_module_async
m = run_async_fn(coro_fn)
assert run_async_fn(m.toplevel) == "..."
)._get_default_module_async()
m = asyncio.run(coro)
assert asyncio.run(m.toplevel()) == "..."
assert not hasattr(m, "__missing")
assert m.variable == 42
assert not hasattr(m, "notthere")
@ -449,23 +451,23 @@ class TestAsyncForLoop:
def test_reversed_bug(self, test_env_async):
tmpl = test_env_async.from_string(
"{% for i in items %}{{ i }}{% if not loop.last %},{% endif %}{% endfor %}"
"{% for i in items %}{{ i }}"
"{% if not loop.last %}"
",{% endif %}{% endfor %}"
)
assert tmpl.render(items=reversed([3, 2, 1])) == "1,2,3"
def test_loop_errors(self, test_env_async, run_async_fn):
def test_loop_errors(self, test_env_async):
tmpl = test_env_async.from_string(
"""{% for item in [1] if loop.index
== 0 %}...{% endfor %}"""
)
with pytest.raises(UndefinedError):
run_async_fn(tmpl.render_async)
pytest.raises(UndefinedError, tmpl.render)
tmpl = test_env_async.from_string(
"""{% for item in [] %}...{% else
%}{{ loop }}{% endfor %}"""
)
assert run_async_fn(tmpl.render_async) == ""
assert tmpl.render() == ""
def test_loop_filter(self, test_env_async):
tmpl = test_env_async.from_string(
@ -595,7 +597,7 @@ class TestAsyncForLoop:
assert t.render(a=dict(b=[1, 2, 3])) == "1"
def test_namespace_awaitable(test_env_async, run_async_fn):
def test_namespace_awaitable(test_env_async):
async def _test():
t = test_env_async.from_string(
'{% set ns = namespace(foo="Bar") %}{{ ns.foo }}'
@ -603,10 +605,10 @@ def test_namespace_awaitable(test_env_async, run_async_fn):
actual = await t.render_async()
assert actual == "Bar"
run_async_fn(_test)
asyncio.run(_test())
def test_chainable_undefined_aiter(run_async_fn):
def test_chainable_undefined_aiter():
async def _test():
t = Template(
"{% for x in a['b']['c'] %}{{ x }}{% endfor %}",
@ -616,7 +618,7 @@ def test_chainable_undefined_aiter(run_async_fn):
rv = await t.render_async(a={})
assert rv == ""
run_async_fn(_test)
asyncio.run(_test())
@pytest.fixture
@ -624,22 +626,22 @@ def async_native_env():
return NativeEnvironment(enable_async=True)
def test_native_async(async_native_env, run_async_fn):
def test_native_async(async_native_env):
async def _test():
t = async_native_env.from_string("{{ x }}")
rv = await t.render_async(x=23)
assert rv == 23
run_async_fn(_test)
asyncio.run(_test())
def test_native_list_async(async_native_env, run_async_fn):
def test_native_list_async(async_native_env):
async def _test():
t = async_native_env.from_string("{{ x }}")
rv = await t.render_async(x=list(range(3)))
assert rv == [0, 1, 2]
run_async_fn(_test)
asyncio.run(_test())
def test_getitem_after_filter():
@ -656,65 +658,3 @@ def test_getitem_after_call():
t = env.from_string("{{ add_each(a, 2)[1:] }}")
out = t.render(a=range(3))
assert out == "[3, 4]"
def test_basic_generate_async(run_async_fn):
t = Template(
"{% for item in [1, 2, 3] %}[{{ item }}]{% endfor %}", enable_async=True
)
async def func():
agen = t.generate_async()
try:
return await agen.__anext__()
finally:
await agen.aclose()
rv = run_async_fn(func)
assert rv == "["
def test_include_generate_async(run_async_fn, test_env_async):
t = test_env_async.from_string('{% include "header" %}')
async def func():
agen = t.generate_async()
try:
return await agen.__anext__()
finally:
await agen.aclose()
rv = run_async_fn(func)
assert rv == "["
def test_blocks_generate_async(run_async_fn):
t = Template(
"{% block foo %}<Test>{% endblock %}{{ self.foo() }}",
enable_async=True,
autoescape=True,
)
async def func():
agen = t.generate_async()
try:
return await agen.__anext__()
finally:
await agen.aclose()
rv = run_async_fn(func)
assert rv == "<Test>"
def test_async_extend(run_async_fn, test_env_async):
t = test_env_async.from_string('{% extends "header" %}')
async def func():
agen = t.generate_async()
try:
return await agen.__anext__()
finally:
await agen.aclose()
rv = run_async_fn(func)
assert rv == "["

View File

@ -1,4 +1,3 @@
import contextlib
from collections import namedtuple
import pytest
@ -27,30 +26,10 @@ def env_async():
return Environment(enable_async=True)
@contextlib.asynccontextmanager
async def closing_factory():
async with contextlib.AsyncExitStack() as stack:
def closing(maybe_agen):
try:
aclose = maybe_agen.aclose
except AttributeError:
pass
else:
stack.push_async_callback(aclose)
return maybe_agen
yield closing
@mark_dualiter("foo", lambda: range(10))
def test_first(env_async, foo, run_async_fn):
async def test():
async with closing_factory() as closing:
tmpl = env_async.from_string("{{ closing(foo())|first }}")
return await tmpl.render_async(foo=foo, closing=closing)
out = run_async_fn(test)
def test_first(env_async, foo):
tmpl = env_async.from_string("{{ foo()|first }}")
out = tmpl.render(foo=foo)
assert out == "0"
@ -266,30 +245,18 @@ def test_slice(env_async, items):
)
def test_unique_with_async_gen(env_async):
items = ["a", "b", "c", "c", "a", "d", "z"]
tmpl = env_async.from_string("{{ items|reject('==', 'z')|unique|list }}")
out = tmpl.render(items=items)
assert out == "['a', 'b', 'c', 'd']"
def test_custom_async_filter(env_async, run_async_fn):
def test_custom_async_filter(env_async):
async def customfilter(val):
return str(val)
async def test():
env_async.filters["customfilter"] = customfilter
tmpl = env_async.from_string(
"{{ 'static'|customfilter }} {{ arg|customfilter }}"
)
return await tmpl.render_async(arg="dynamic")
out = run_async_fn(test)
env_async.filters["customfilter"] = customfilter
tmpl = env_async.from_string("{{ 'static'|customfilter }} {{ arg|customfilter }}")
out = tmpl.render(arg="dynamic")
assert out == "static dynamic"
@mark_dualiter("items", lambda: range(10))
def test_custom_async_iteratable_filter(env_async, items, run_async_fn):
def test_custom_async_iteratable_filter(env_async, items):
async def customfilter(iterable):
items = []
async for item in auto_aiter(iterable):
@ -298,13 +265,9 @@ def test_custom_async_iteratable_filter(env_async, items, run_async_fn):
break
return ",".join(items)
async def test():
async with closing_factory() as closing:
env_async.filters["customfilter"] = customfilter
tmpl = env_async.from_string(
"{{ closing(items())|customfilter }} .. {{ [3, 4, 5, 6]|customfilter }}"
)
return await tmpl.render_async(items=items, closing=closing)
out = run_async_fn(test)
env_async.filters["customfilter"] = customfilter
tmpl = env_async.from_string(
"{{ items()|customfilter }} .. {{ [3, 4, 5, 6]|customfilter }}"
)
out = tmpl.render(items=items)
assert out == "0,1,2 .. 3,4,5"

View File

@ -1,9 +1,6 @@
import os
import re
import pytest
from jinja2 import UndefinedError
from jinja2.environment import Environment
from jinja2.loaders import DictLoader
@ -29,80 +26,3 @@ def test_import_as_with_context_deterministic(tmp_path):
expect = [f"'bar{i}': " for i in range(10)]
found = re.findall(r"'bar\d': ", content)[:10]
assert found == expect
def test_top_level_set_vars_unpacking_deterministic(tmp_path):
src = "\n".join(f"{{% set a{i}, b{i}, c{i} = tuple_var{i} %}}" for i in range(10))
env = Environment(loader=DictLoader({"foo": src}))
env.compile_templates(tmp_path, zip=None)
name = os.listdir(tmp_path)[0]
content = (tmp_path / name).read_text("utf8")
expect = [
f"context.vars.update({{'a{i}': l_0_a{i}, 'b{i}': l_0_b{i}, 'c{i}': l_0_c{i}}})"
for i in range(10)
]
found = re.findall(
r"context\.vars\.update\(\{'a\d': l_0_a\d, 'b\d': l_0_b\d, 'c\d': l_0_c\d\}\)",
content,
)[:10]
assert found == expect
expect = [
f"context.exported_vars.update(('a{i}', 'b{i}', 'c{i}'))" for i in range(10)
]
found = re.findall(
r"context\.exported_vars\.update\(\('a\d', 'b\d', 'c\d'\)\)",
content,
)[:10]
assert found == expect
def test_loop_set_vars_unpacking_deterministic(tmp_path):
src = "\n".join(f" {{% set a{i}, b{i}, c{i} = tuple_var{i} %}}" for i in range(10))
src = f"{{% for i in seq %}}\n{src}\n{{% endfor %}}"
env = Environment(loader=DictLoader({"foo": src}))
env.compile_templates(tmp_path, zip=None)
name = os.listdir(tmp_path)[0]
content = (tmp_path / name).read_text("utf8")
expect = [
f"_loop_vars.update({{'a{i}': l_1_a{i}, 'b{i}': l_1_b{i}, 'c{i}': l_1_c{i}}})"
for i in range(10)
]
found = re.findall(
r"_loop_vars\.update\(\{'a\d': l_1_a\d, 'b\d': l_1_b\d, 'c\d': l_1_c\d\}\)",
content,
)[:10]
assert found == expect
def test_block_set_vars_unpacking_deterministic(tmp_path):
src = "\n".join(f" {{% set a{i}, b{i}, c{i} = tuple_var{i} %}}" for i in range(10))
src = f"{{% block test %}}\n{src}\n{{% endblock test %}}"
env = Environment(loader=DictLoader({"foo": src}))
env.compile_templates(tmp_path, zip=None)
name = os.listdir(tmp_path)[0]
content = (tmp_path / name).read_text("utf8")
expect = [
f"_block_vars.update({{'a{i}': l_0_a{i}, 'b{i}': l_0_b{i}, 'c{i}': l_0_c{i}}})"
for i in range(10)
]
found = re.findall(
r"_block_vars\.update\(\{'a\d': l_0_a\d, 'b\d': l_0_b\d, 'c\d': l_0_c\d\}\)",
content,
)[:10]
assert found == expect
def test_undefined_import_curly_name():
env = Environment(
loader=DictLoader(
{
"{bad}": "{% from 'macro' import m %}{{ m() }}",
"macro": "",
}
)
)
# Must not raise `NameError: 'bad' is not defined`, as that would indicate
# that `{bad}` is being interpreted as an f-string. It must be escaped.
with pytest.raises(UndefinedError):
env.get_template("{bad}").render()

View File

@ -191,7 +191,9 @@ class TestForLoop:
def test_reversed_bug(self, env):
tmpl = env.from_string(
"{% for i in items %}{{ i }}{% if not loop.last %},{% endif %}{% endfor %}"
"{% for i in items %}{{ i }}"
"{% if not loop.last %}"
",{% endif %}{% endfor %}"
)
assert tmpl.render(items=reversed([3, 2, 1])) == "1,2,3"
@ -536,14 +538,6 @@ class TestSet:
)
assert tmpl.render() == "13|37"
def test_namespace_set_tuple(self, env_trim):
tmpl = env_trim.from_string(
"{% set ns = namespace(a=12, b=36) %}"
"{% set ns.a, ns.b = ns.a + 1, ns.b + 1 %}"
"{{ ns.a }}|{{ ns.b }}"
)
assert tmpl.render() == "13|37"
def test_block_escaping_filtered(self):
env = Environment(autoescape=True)
tmpl = env.from_string(

View File

@ -23,9 +23,9 @@ class TestDebug:
tb = format_exception(exc_info.type, exc_info.value, exc_info.tb)
m = re.search(expected_tb.strip(), "".join(tb))
assert m is not None, (
f"Traceback did not match:\n\n{''.join(tb)}\nexpected:\n{expected_tb}"
)
assert (
m is not None
), f"Traceback did not match:\n\n{''.join(tb)}\nexpected:\n{expected_tb}"
def test_runtime_error(self, fs_env):
def test():

View File

@ -554,7 +554,8 @@ class TestNewstyleInternationalization:
newstyle=True,
)
t = env.from_string(
'{% autoescape ae %}{{ gettext("foo", name="<test>") }}{% endautoescape %}'
'{% autoescape ae %}{{ gettext("foo", name='
'"<test>") }}{% endautoescape %}'
)
assert t.render(ae=True) == "<strong>Wert: &lt;test&gt;</strong>"
assert t.render(ae=False) == "<strong>Wert: <test></strong>"

View File

@ -196,7 +196,6 @@ class TestFilter:
("abc", "0"),
("32.32", "32"),
("12345678901234567890", "12345678901234567890"),
("1e10000", "0"),
),
)
def test_int(self, env, value, expect):
@ -357,7 +356,7 @@ class TestFilter:
def test_urlize(self, env):
tmpl = env.from_string('{{ "foo example.org bar"|urlize }}')
assert tmpl.render() == (
'foo <a href="https://example.org" rel="noopener">example.org</a> bar'
'foo <a href="https://example.org" rel="noopener">' "example.org</a> bar"
)
tmpl = env.from_string('{{ "foo http://www.example.com/ bar"|urlize }}')
assert tmpl.render() == (

View File

@ -43,7 +43,8 @@ class TestTokenStream:
class TestLexer:
def test_raw1(self, env):
tmpl = env.from_string(
"{% raw %}foo{% endraw %}|{%raw%}{{ bar }}|{% baz %}{% endraw %}"
"{% raw %}foo{% endraw %}|"
"{%raw%}{{ bar }}|{% baz %}{% endraw %}"
)
assert tmpl.render() == "foo|{{ bar }}|{% baz %}"

View File

@ -2,6 +2,7 @@ import importlib.abc
import importlib.machinery
import importlib.util
import os
import platform
import shutil
import sys
import tempfile
@ -179,24 +180,6 @@ class TestFileSystemLoader:
t = e.get_template("foo/test.html")
assert t.filename == str(self.searchpath / "foo" / "test.html")
def test_error_includes_paths(self, env, filesystem_loader):
env.loader = filesystem_loader
with pytest.raises(TemplateNotFound) as info:
env.get_template("missing")
e_str = str(info.value)
assert e_str.startswith("'missing' not found in search path: ")
filesystem_loader.searchpath.append("other")
with pytest.raises(TemplateNotFound) as info:
env.get_template("missing")
e_str = str(info.value)
assert e_str.startswith("'missing' not found in search paths: ")
assert ", 'other'" in e_str
class TestModuleLoader:
archive = None
@ -381,8 +364,8 @@ def test_package_zip_source(package_zip_loader, template, expect):
@pytest.mark.xfail(
sys.implementation.name == "pypy",
reason="zipimporter doesn't have a '_files' attribute",
platform.python_implementation() == "PyPy",
reason="PyPy's zipimporter doesn't have a '_files' attribute.",
raises=TypeError,
)
def test_package_zip_list(package_zip_loader):
@ -429,8 +412,3 @@ def test_pep_451_import_hook():
assert "test.html" in package_loader.list_templates()
finally:
sys.meta_path[:] = before
def test_package_loader_no_dir() -> None:
with pytest.raises(ValueError, match="could not find a 'templates' directory"):
PackageLoader("jinja2")

View File

@ -13,11 +13,6 @@ def env():
return NativeEnvironment()
@pytest.fixture
def async_native_env():
return NativeEnvironment(enable_async=True)
def test_is_defined_native_return(env):
t = env.from_string("{{ missing is defined }}")
assert not t.render()
@ -127,18 +122,6 @@ def test_string_top_level(env):
assert result == "Jinja"
def test_string_concatenation(async_native_env, run_async_fn):
async def async_render():
t = async_native_env.from_string(
"{%- macro x(y) -%}{{ y }}{%- endmacro -%}{{- x('not') }} {{ x('bad') -}}"
)
result = await t.render_async()
assert isinstance(result, str)
assert result == "not bad"
run_async_fn(async_render)
def test_tuple_of_variable_strings(env):
t = env.from_string("'{{ a }}', 'data', '{{ b }}', b'{{ c }}'")
result = t.render(a=1, b=2, c="bytes")
@ -177,13 +160,3 @@ def test_macro(env):
result = t.render()
assert result == 2
assert isinstance(result, int)
def test_block(env):
t = env.from_string(
"{% block b %}{% for i in range(1) %}{{ loop.index }}{% endfor %}"
"{% endblock %}{{ self.b() }}"
)
result = t.render()
assert result == 11
assert isinstance(result, int)

View File

@ -737,28 +737,6 @@ End"""
)
assert tmpl.render() == "hellohellohello"
def test_pass_context_with_select(self, env):
@pass_context
def is_foo(ctx, s):
assert ctx is not None
return s == "foo"
env.tests["foo"] = is_foo
tmpl = env.from_string(
"{% for x in ['one', 'foo'] | select('foo') %}{{ x }}{% endfor %}"
)
assert tmpl.render() == "foo"
def test_load_parameter_when_set_in_all_if_branches(env):
tmpl = env.from_string(
"{% if True %}{{ a.b }}{% set a = 1 %}"
"{% elif False %}{% set a = 2 %}"
"{% else %}{% set a = 3 %}{% endif %}"
"{{ a }}"
)
assert tmpl.render(a={"b": 0}) == "01"
@pytest.mark.parametrize("unicode_char", ["\N{FORM FEED}", "\x85"])
def test_unicode_whitespace(env, unicode_char):

View File

@ -1,15 +1,6 @@
import copy
import itertools
import pickle
import pytest
from jinja2 import ChainableUndefined
from jinja2 import DebugUndefined
from jinja2 import StrictUndefined
from jinja2 import Template
from jinja2 import TemplateRuntimeError
from jinja2 import Undefined
from jinja2.runtime import LoopContext
TEST_IDX_TEMPLATE_STR_1 = (
@ -82,44 +73,3 @@ def test_mock_not_pass_arg_marker():
out = t.render(calc=Calc())
# Would be "1" if context argument was passed.
assert out == "0"
_undefined_types = (Undefined, ChainableUndefined, DebugUndefined, StrictUndefined)
@pytest.mark.parametrize("undefined_type", _undefined_types)
def test_undefined_copy(undefined_type):
undef = undefined_type("a hint", ["foo"], "a name", TemplateRuntimeError)
copied = copy.copy(undef)
assert copied is not undef
assert copied._undefined_hint is undef._undefined_hint
assert copied._undefined_obj is undef._undefined_obj
assert copied._undefined_name is undef._undefined_name
assert copied._undefined_exception is undef._undefined_exception
@pytest.mark.parametrize("undefined_type", _undefined_types)
def test_undefined_deepcopy(undefined_type):
undef = undefined_type("a hint", ["foo"], "a name", TemplateRuntimeError)
copied = copy.deepcopy(undef)
assert copied._undefined_hint is undef._undefined_hint
assert copied._undefined_obj is not undef._undefined_obj
assert copied._undefined_obj == undef._undefined_obj
assert copied._undefined_name is undef._undefined_name
assert copied._undefined_exception is undef._undefined_exception
@pytest.mark.parametrize("undefined_type", _undefined_types)
def test_undefined_pickle(undefined_type):
undef = undefined_type("a hint", ["foo"], "a name", TemplateRuntimeError)
copied = pickle.loads(pickle.dumps(undef))
assert copied._undefined_hint is not undef._undefined_hint
assert copied._undefined_hint == undef._undefined_hint
assert copied._undefined_obj is not undef._undefined_obj
assert copied._undefined_obj == undef._undefined_obj
assert copied._undefined_name is not undef._undefined_name
assert copied._undefined_name == undef._undefined_name
assert copied._undefined_exception is undef._undefined_exception

View File

@ -58,8 +58,6 @@ class TestSandbox:
def test_immutable_environment(self, env):
env = ImmutableSandboxedEnvironment()
pytest.raises(SecurityError, env.from_string("{{ [].append(23) }}").render)
pytest.raises(SecurityError, env.from_string("{{ [].clear() }}").render)
pytest.raises(SecurityError, env.from_string("{{ [1].pop() }}").render)
pytest.raises(SecurityError, env.from_string("{{ {1:2}.clear() }}").render)
def test_restricted(self, env):
@ -173,30 +171,3 @@ class TestStringFormatMap:
'{{ ("a{x.foo}b{y}"|safe).format_map({"x":{"foo": 42}, "y":"<foo>"}) }}'
)
assert t.render() == "a42b&lt;foo&gt;"
def test_indirect_call(self):
def run(value, arg):
return value.run(arg)
env = SandboxedEnvironment()
env.filters["run"] = run
t = env.from_string(
"""{% set
ns = namespace(run="{0.__call__.__builtins__[__import__]}".format)
%}
{{ ns | run(not_here) }}
"""
)
with pytest.raises(SecurityError):
t.render()
def test_attr_filter(self) -> None:
env = SandboxedEnvironment()
t = env.from_string(
"""{{ "{0.__call__.__builtins__[__import__]}"
| attr("format")(not_here) }}"""
)
with pytest.raises(SecurityError):
t.render()

View File

@ -1,4 +1,3 @@
import copy
import pickle
import random
from collections import deque
@ -142,14 +141,6 @@ class TestEscapeUrlizeTarget:
"http://example.org</a>"
)
def test_urlize_mail_mastodon(self):
fr = "nabijaczleweli@nabijaczleweli.xyz\n@eater@cijber.social\n"
to = (
'<a href="mailto:nabijaczleweli@nabijaczleweli.xyz">'
"nabijaczleweli@nabijaczleweli.xyz</a>\n@eater@cijber.social\n"
)
assert urlize(fr) == to
class TestLoremIpsum:
def test_lorem_ipsum_markup(self):
@ -192,14 +183,3 @@ def test_consume():
consume(x)
with pytest.raises(StopIteration):
next(x)
@pytest.mark.parametrize("protocol", range(pickle.HIGHEST_PROTOCOL + 1))
def test_pickle_missing(protocol: int) -> None:
"""Test that missing can be pickled while remaining a singleton."""
assert pickle.loads(pickle.dumps(missing, protocol)) is missing
def test_copy_missing() -> None:
"""Test that missing can be copied while remaining a singleton."""
assert copy.copy(missing) is missing

43
tox.ini Normal file
View File

@ -0,0 +1,43 @@
[tox]
envlist =
py3{12,11,10,9,8,7}
pypy310
style
typing
docs
skip_missing_interpreters = true
[testenv]
package = wheel
wheel_build_env = .pkg
constrain_package_deps = true
use_frozen_constraints = true
deps = -r requirements/tests.txt
commands = pytest -v --tb=short --basetemp={envtmpdir} {posargs}
[testenv:style]
deps = pre-commit
skip_install = true
commands = pre-commit run --all-files
[testenv:typing]
deps = -r requirements/typing.txt
commands = mypy
[testenv:docs]
deps = -r requirements/docs.txt
commands = sphinx-build -E -W -b dirhtml docs docs/_build/dirhtml
[testenv:update-requirements]
deps =
pip-tools
pre-commit
skip_install = true
change_dir = requirements
commands =
pre-commit autoupdate -j4
pip-compile -U build.in
pip-compile -U docs.in
pip-compile -U tests.in
pip-compile -U typing.in
pip-compile -U dev.in

1244
uv.lock generated

File diff suppressed because it is too large Load Diff