Skip to content

Commit eb73ec6

Browse files
committed
Make an import from 'moduledrawers' directly raise a deprecation warning
1 parent 7528208 commit eb73ec6

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

qrcode/image/styledpil.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import qrcode.image.base
1010
from qrcode.image.styles.colormasks import QRColorMask, SolidFillColorMask
11-
from qrcode.image.styles.moduledrawers import SquareModuleDrawer
11+
from qrcode.image.styles.moduledrawers.pil import SquareModuleDrawer
1212

1313

1414
class StyledPilImage(qrcode.image.base.BaseImageWithDrawer):
Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,47 @@
1-
# For backwards compatibility, importing the PIL drawers here.
2-
import contextlib
3-
4-
with contextlib.suppress(ImportError):
5-
from .pil import (
6-
CircleModuleDrawer, # noqa: F401
7-
GappedCircleModuleDrawer, # noqa: F401
8-
GappedSquareModuleDrawer, # noqa: F401
9-
HorizontalBarsDrawer, # noqa: F401
10-
RoundedModuleDrawer, # noqa: F401
11-
SquareModuleDrawer, # noqa: F401
12-
VerticalBarsDrawer, # noqa: F401
13-
)
1+
"""
2+
Module for lazy importing of PIL drawers with a deprecation warning.
3+
4+
Currently, importing a PIL drawer from this module is allowed for backwards
5+
compatibility but will raise a DeprecationWarning.
6+
7+
This will be removed in v9.0.
8+
"""
9+
10+
import warnings
11+
12+
from qrcode.constants import PIL_AVAILABLE
13+
14+
15+
def __getattr__(name):
16+
"""Lazy import with deprecation warning for PIL drawers."""
17+
# List of PIL drawer names that should trigger deprecation warnings
18+
pil_drawers = {
19+
"CircleModuleDrawer",
20+
"GappedCircleModuleDrawer",
21+
"GappedSquareModuleDrawer",
22+
"HorizontalBarsDrawer",
23+
"RoundedModuleDrawer",
24+
"SquareModuleDrawer",
25+
"VerticalBarsDrawer",
26+
}
27+
28+
if name in pil_drawers:
29+
# Only render a warning if PIL is actually installed. Otherwise it would
30+
# raise an ImportError directly, which is fine.
31+
if PIL_AVAILABLE:
32+
warnings.warn(
33+
f"Importing '{name}' directly from this module is deprecated."
34+
f"Please use 'from qrcode.image.styles.moduledrawers.pil import {name}' "
35+
f"instead. This backwards compatibility import will be removed in v9.0.",
36+
DeprecationWarning,
37+
stacklevel=2,
38+
)
39+
40+
# Import and return the drawer from the pil module
41+
from . import pil # noqa: PLC0415
42+
43+
return getattr(pil, name)
44+
45+
# For any other attribute, raise AttributeError
46+
msg = f"module {__name__!r} has no attribute {name!r}"
47+
raise AttributeError(msg)

qrcode/tests/test_qrcode_pil.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ def test_render_styled_with_embedded_image_path(tmp_path):
7171
@pytest.mark.parametrize(
7272
"drawer",
7373
[
74-
moduledrawers.CircleModuleDrawer,
75-
moduledrawers.GappedCircleModuleDrawer,
76-
moduledrawers.GappedSquareModuleDrawer,
77-
moduledrawers.HorizontalBarsDrawer,
78-
moduledrawers.RoundedModuleDrawer,
79-
moduledrawers.SquareModuleDrawer,
80-
moduledrawers.VerticalBarsDrawer,
74+
moduledrawers.pil.CircleModuleDrawer,
75+
moduledrawers.pil.GappedCircleModuleDrawer,
76+
moduledrawers.pil.GappedSquareModuleDrawer,
77+
moduledrawers.pil.HorizontalBarsDrawer,
78+
moduledrawers.pil.RoundedModuleDrawer,
79+
moduledrawers.pil.SquareModuleDrawer,
80+
moduledrawers.pil.VerticalBarsDrawer,
8181
],
8282
)
8383
def test_render_styled_with_drawer(drawer):

0 commit comments

Comments
 (0)