Skip to content

Upgrade Jinja support to work with Django 5 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,38 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: pipx install "poetry>=1.1.12,<2"
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version-file: pyproject.toml
python-version: "3.12"
cache: 'poetry'
- run: pip install tox
- run: tox -e lint,py311-dj42
- run: tox -e lint,py312-dj50
test_compatibility:
needs: test
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
# Test with all supported Django versions, for all compatible Python versions.
# See https://docs.djangoproject.com/en/4.0/faq/install/#what-python-version-can-i-use-with-django for the official matrix.
# See https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django for the official matrix.
# Additionally test on Django’s main branch with the most recent Python version.
- python: "3.8"
toxenv: py38-dj32,py38-dj41,py38-dj42
toxenv: py38-dj32,py38-dj42
- python: "3.9"
toxenv: py39-dj32,py39-dj41,py39-dj42
toxenv: py39-dj32,py39-dj42
- python: "3.10"
# Skip testing Django 4.0, already tested in previous workflow job.
toxenv: py310-dj32,py310-dj41,py310-dj42,py310-djmain
toxenv: py310-dj32,py310-dj42,py310-dj50,py310-djmain
- python: "3.11"
toxenv: py311-dj41,py311-dj42,py311-djmain
# Tentative support for next Python pre-release.
# When django supports the version, uncomment this
# - python: "3.12"
# toxenv: py312-dj41
toxenv: py311-dj42,py311-dj50,py311-djmain
- python: "3.12"
toxenv: py312-dj42,py312-djmain
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: pipx install "poetry>=1.1.12,<2"
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
allow-prereleases: true
Expand All @@ -53,12 +51,12 @@ jobs:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
- id: node-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
# Cache node_modules rather than the npm cache, as we rarely update npm packages.
path: node_modules
Expand All @@ -67,7 +65,7 @@ jobs:
run: npm ci --no-audit
- run: npm run build
- run: pipx install "poetry>=1.1.12,<2"
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version-file: pyproject.toml
cache: 'poetry'
Expand All @@ -86,8 +84,8 @@ jobs:
- run: cat pyproject.toml | awk '{sub(/^version = .+/,"version = \"0.0.0.dev\"")}1' > pyproject.toml.tmp && mv pyproject.toml.tmp pyproject.toml
- run: poetry build
- run: mv dist site
- uses: actions/configure-pages@v3
- uses: actions/upload-pages-artifact@v2
- uses: actions/configure-pages@v4
- uses: actions/upload-pages-artifact@v3
with:
path: site
deploy_site:
Expand All @@ -101,4 +99,4 @@ jobs:
url: ${{ steps.deployment.outputs.page_url }}
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/deploy-pages@v2
- uses: actions/deploy-pages@v4
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## [1.2.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.2.0) - 2024-01-16

### Added

- Add support for Django 5.0 ([#241](https://github.com/torchbox/django-pattern-library/pull/241))

### Changed

- From Django >= 4.0, calls to `Node.render()` must always return a string, but this app previously allowed non-string values to be passed in the `default_html` parameter to `override_tag`. Passing a non-string now raises a `TypeError` when using Django >= 4.0, and raises a warning for older versions ([issue #211](https://github.com/torchbox/django-pattern-library/issues/211)).

## [1.1.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.1.0) - 2023-10-25

### Added
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/automated-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Although pattern libraries often start as tools for manual tests during development, they can also be useful for automated UI testing. There are a few benefits to doing UI tests with a pattern library:

- Test the components in isolation. When tests fail, you will know exactly which component has issues, rather than having to inspect whole pages to understand what might have changed.
- Test the components with mock data. One of the issues with UI tests is to have test data for your UIs to render – you can reuse the pattern library data for this purpose (althoug there are [limitations](../guides/multiple-variants.md)).
- Test the components with mock data. One of the issues with UI tests is to have test data for your UIs to render – you can reuse the pattern library data for this purpose (although there are [limitations](../guides/multiple-variants.md)).

## Setting up automated UI tests

Expand Down
2 changes: 1 addition & 1 deletion docs/reference/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ YAML isn’t everyone’s favorite markup language, but it has the advantage of

Here is what you need to know:

- Use `.yaml` or `.yml` as the file extension for pattern configuration files. If both are present, the `.yaml` file takes precendence.
- Use `.yaml` or `.yml` as the file extension for pattern configuration files. If both are present, the `.yaml` file takes precedence.
- Use Mappings in place of Python Dictionaries.
- Use Sequences in place of Python lists (or iterables like QuerySets).
- The pattern library uses [PyYAML](https://pyyaml.org/wiki/PyYAMLDocumentation) in particular
Expand Down
27 changes: 24 additions & 3 deletions pattern_library/monkey_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import inspect
import logging
from typing import Optional
import typing
import warnings

import django
from django.template.library import SimpleNode
Expand All @@ -11,7 +13,9 @@


def override_tag(
register: django.template.Library, name: str, default_html: Optional[str] = None
register: django.template.Library,
name: str,
default_html: typing.Optional[typing.Any] = UNSPECIFIED,
):
"""
An utility that helps you override original tags for use in your pattern library.
Expand All @@ -29,7 +33,7 @@ def node_render(context):
tag_overridden = False
result = ""

# Get overriden tag config.
# Get overridden tag config.
tag_overrides = context.get("__pattern_library_tag_overrides", {})

# Extract values for lookup from the token
Expand Down Expand Up @@ -79,6 +83,23 @@ def node_render(context):
# See https://github.com/torchbox/django-pattern-library/issues/166.
return str(result)
elif default_html is not UNSPECIFIED:
# Ensure default_html is a string.
if not isinstance(default_html, str):
# Save the caller for the override tag in case it's needed for error reporting.
trace = inspect.stack()[1]
if django.VERSION < (4, 0):
warnings.warn(
"default_html argument to override_tag should be a string to ensure compatibility "
'with Django >= 4.0 (line %s in "%s")'
% (trace.lineno, trace.filename),
Warning,
)
else:
raise TypeError(
'default_html argument to override_tag must be a string (line %s in "%s")'
% (trace.lineno, trace.filename)
)

# Render provided default;
# if no stub data supplied.
return default_html
Expand Down
Loading