Skip to content

Commit ba08a1e

Browse files
committed
Enforce lazy evaluation of annotations
They're only used for type hints in this code base.
1 parent af91e21 commit ba08a1e

24 files changed

Lines changed: 58 additions & 14 deletions

barcode/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
created as SVG objects. If Pillow is installed, the barcodes can also be
44
rendered as images (all formats supported by Pillow).
55
"""
6-
import os
6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
79
from typing import BinaryIO
8-
from typing import Dict
9-
from typing import Optional
10-
from typing import Union
1110

1211
from barcode.codabar import CODABAR
1312
from barcode.codex import PZN
@@ -28,6 +27,9 @@
2827
from barcode.upc import UPCA
2928
from barcode.version import version # noqa: F401
3029

30+
if TYPE_CHECKING:
31+
import os
32+
3133
__BARCODE_MAP = {
3234
"ean8": EAN8,
3335
"ean8-guard": EAN8_GUARD,
@@ -59,9 +61,9 @@
5961

6062
def get(
6163
name: str,
62-
code: Optional[str] = None,
64+
code: str | None = None,
6365
writer=None,
64-
options: Optional[dict] = None,
66+
options: dict | None = None,
6567
):
6668
"""Helper method for getting a generator or even a generated code.
6769
@@ -93,9 +95,9 @@ def generate(
9395
name: str,
9496
code: str,
9597
writer=None,
96-
output: Union[str, os.PathLike, BinaryIO, None] = None,
97-
writer_options: Union[Dict, None] = None,
98-
text: Union[str, None] = None,
98+
output: str | (os.PathLike | (BinaryIO | None)) = None,
99+
writer_options: dict | None = None,
100+
text: str | None = None,
99101
):
100102
"""Shortcut to generate a barcode in one line.
101103

barcode/base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""barcode.base
22
33
"""
4+
from __future__ import annotations
5+
46
from typing import ClassVar
5-
from typing import List
6-
from typing import Optional
77

88
from barcode.writer import BaseWriter
99
from barcode.writer import SVGWriter
@@ -39,7 +39,7 @@ def to_ascii(self) -> str:
3939
def __repr__(self) -> str:
4040
return f"<{self.__class__.__name__}({self.get_fullcode()!r})>"
4141

42-
def build(self) -> List[str]:
42+
def build(self) -> list[str]:
4343
raise NotImplementedError
4444

4545
def get_fullcode(self):
@@ -51,7 +51,7 @@ def get_fullcode(self):
5151
raise NotImplementedError
5252

5353
def save(
54-
self, filename: str, options: Optional[dict] = None, text: Optional[str] = None
54+
self, filename: str, options: dict | None = None, text: str | None = None
5555
) -> str:
5656
"""Renders the barcode and saves it in `filename`.
5757
@@ -80,7 +80,7 @@ def write(self, fp, options=None, text=None):
8080
output = self.render(options, text)
8181
self.writer.write(output, fp)
8282

83-
def render(self, writer_options: Optional[dict] = None, text: Optional[str] = None):
83+
def render(self, writer_options: dict | None = None, text: str | None = None):
8484
"""Renders the barcode using `self.writer`.
8585
8686
:param writer_options: Options for `self.writer`, see writer docs for details.

barcode/charsets/codabar.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
# W = Wide bar
24
# w = wide space
35
# N = Narrow bar

barcode/charsets/code128.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import string
24

35
# Charsets for code 128

barcode/charsets/code39.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import string
24

35
# Charsets for code 39

barcode/charsets/ean.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
EDGE = "101"
24
MIDDLE = "01010"
35
CODES = {

barcode/charsets/itf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
# W = Wide bar
24
# w = wide space
35
# N = Narrow bar

barcode/charsets/upc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
EDGE = "101"
24
MIDDLE = "01010"
35
CODES = {

barcode/codabar.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
33
:Provided barcodes: Codabar (NW-7)
44
"""
5+
from __future__ import annotations
6+
57
__docformat__ = "restructuredtext en"
68

79
from barcode.base import Barcode

barcode/codex.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
:Provided barcodes: Code 39, Code 128, PZN
44
"""
5+
from __future__ import annotations
56

67
from typing import Collection
78

0 commit comments

Comments
 (0)