Skip to content

Commit c473ebb

Browse files
committed
Test and document deprecations.
1 parent c38fb6f commit c473ebb

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

CHANGES.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,33 @@
22
Change log
33
==========
44

5+
Deprecation Warnings
6+
====================
7+
8+
Removed in v9.0:
9+
----------------
10+
11+
- Importing a PIL drawer from ``qrcode.image.styles.moduledrawers`` has been deprecated.
12+
Update your code to import directly from the ``pil`` module instead:
13+
14+
.. code-block:: python
15+
16+
from qrcode.image.styles.moduledrawers import SquareModuleDrawer # Old
17+
from qrcode.image.styles.moduledrawers.pil import SquareModuleDrawer # New
18+
19+
- Calling ``QRCode.make_image`` or ``StyledPilImage`` with the arguments ``embeded_image``
20+
or ``embeded_image_path`` have been deprecated due to typographical errors. Update
21+
your code to use the correct arguments ``embedded_image`` and ``embededd_image_path``:
22+
23+
.. code-block:: python
24+
25+
qr = QRCode()
26+
qr.make_image(embeded_image=..., embeded_image_path=...) # Old
27+
qr.make_image(embedded_image=..., embedded_image_path=...) # New
28+
29+
StyledPilImage(embeded_image=..., embeded_image_path=...) # Old
30+
StyledPilImage(embedded_image=..., embedded_image_path=...) # New
31+
532
WIP
633
===
734

qrcode/tests/conftest.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import tempfile
2+
from importlib.util import find_spec
3+
4+
import pytest
5+
6+
7+
@pytest.fixture
8+
def dummy_image() -> tempfile.NamedTemporaryFile:
9+
"""
10+
This function creates a red pixel image with full opacity, saves it as a PNG
11+
file in a temporary location, and returns the temporary file.
12+
13+
The file is not automatically deleted. The caller is responsible for deleting it.
14+
"""
15+
# Must import here as PIL might be not installed
16+
from PIL import Image
17+
18+
# A 1x1 Red Pixel
19+
dummy_image = Image.new("RGBA", (1, 1), (255, 0, 0, 255))
20+
21+
# Save the image to a temporary file
22+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
23+
dummy_image.save(temp_file.name)
24+
25+
return temp_file

qrcode/tests/test_deprecation.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
from __future__ import annotations
2+
3+
from pathlib import Path
4+
from typing import TYPE_CHECKING
5+
6+
import pytest
7+
8+
from qrcode.constants import ERROR_CORRECT_H, PIL_AVAILABLE
9+
from qrcode.main import QRCode
10+
11+
if TYPE_CHECKING:
12+
from tempfile import NamedTemporaryFile
13+
14+
15+
@pytest.mark.skipif(not PIL_AVAILABLE, reason="PIL is not installed")
16+
def test_moduledrawer_import() -> None:
17+
"""
18+
Importing a drawer from qrcode.image.styles.moduledrawers is deprecated
19+
and will raise a DeprecationWarning.
20+
21+
Removed in v9.0.
22+
"""
23+
# These module imports are fine to import
24+
from qrcode.image.styles.moduledrawers import base, pil, svg
25+
26+
with pytest.warns(
27+
DeprecationWarning,
28+
match="Importing 'SquareModuleDrawer' directly from this module is deprecated.",
29+
):
30+
from qrcode.image.styles.moduledrawers import SquareModuleDrawer
31+
32+
33+
@pytest.mark.skipif(PIL_AVAILABLE, reason="PIL is installed")
34+
def test_moduledrawer_import_pil_not_installed() -> None:
35+
"""
36+
Importing from qrcode.image.styles.moduledrawers is deprecated, however,
37+
if PIL is not installed, there will be no (false) warning; it's a simple
38+
ImportError.
39+
40+
Removed in v9.0.
41+
"""
42+
# These module imports are fine to import
43+
from qrcode.image.styles.moduledrawers import base, svg
44+
45+
# Importing a backwards compatible module drawer does normally render a
46+
# DeprecationWarning; however, since PIL is not installed, it will raise an
47+
# ImportError.
48+
with pytest.raises(ImportError):
49+
from qrcode.image.styles.moduledrawers import SquareModuleDrawer
50+
51+
52+
@pytest.mark.skipif(not PIL_AVAILABLE, reason="PIL is not installed")
53+
def test_make_image_embeded_parameters() -> None:
54+
"""
55+
Using 'embeded_image_path' or 'embeded_image' parameters with QRCode.make_image()
56+
is deprecated and will raise a DeprecationWarning.
57+
58+
Removed in v9.0.
59+
"""
60+
61+
# Create a QRCode required for embedded images
62+
qr = QRCode(error_correction=ERROR_CORRECT_H)
63+
qr.add_data("test")
64+
65+
# Test with embeded_image_path parameter
66+
with pytest.warns(
67+
DeprecationWarning, match="The 'embeded_\\*' parameters are deprecated"
68+
):
69+
qr.make_image(embeded_image_path="dummy_path")
70+
71+
# Test with embeded_image parameter
72+
with pytest.warns(
73+
DeprecationWarning, match="The 'embeded_\\*' parameters are deprecated."
74+
):
75+
qr.make_image(embeded_image="dummy_image")
76+
77+
78+
@pytest.mark.skipif(not PIL_AVAILABLE, reason="PIL is not installed")
79+
def test_styledpilimage_embeded_parameters(dummy_image: NamedTemporaryFile) -> None:
80+
"""
81+
Using 'embeded_image_path' or 'embeded_image' parameters with StyledPilImage
82+
is deprecated and will raise a DeprecationWarning.
83+
84+
Removed in v9.0.
85+
"""
86+
from PIL import Image
87+
88+
from qrcode.image.styledpil import StyledPilImage
89+
90+
styled_kwargs = {
91+
"border": 4,
92+
"width": 21,
93+
"box_size": 10,
94+
"qrcode_modules": 1,
95+
}
96+
97+
try:
98+
# Test with embeded_image_path parameter
99+
with pytest.warns(
100+
DeprecationWarning, match="The 'embeded_\\*' parameters are deprecated."
101+
):
102+
StyledPilImage(embeded_image_path=dummy_image.name, **styled_kwargs)
103+
104+
# Test with embeded_image parameter
105+
embedded_img = Image.open(dummy_image.name)
106+
107+
with pytest.warns(
108+
DeprecationWarning, match="The 'embeded_\\*' parameters are deprecated."
109+
):
110+
StyledPilImage(embeded_image=embedded_img, **styled_kwargs)
111+
112+
# Make sure the temporary image is always deleted after the testrun.
113+
finally:
114+
Path(dummy_image.name).unlink(missing_ok=True)

0 commit comments

Comments
 (0)