Skip to content

Commit af91e21

Browse files
committed
Add some type hints for BaseWriter
1 parent 177e130 commit af91e21

1 file changed

Lines changed: 40 additions & 25 deletions

File tree

barcode/writer.py

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
from __future__ import annotations
2+
13
import gzip
24
import os
35
import xml.dom
6+
from typing import TYPE_CHECKING
47
from typing import BinaryIO
8+
from typing import Callable
9+
from typing import TypedDict
510

611
from barcode.version import version
712

13+
if TYPE_CHECKING:
14+
15+
class InternalText(TypedDict):
16+
start: list
17+
end: list
18+
xpos: list
19+
was_guard: bool
20+
21+
class Callbacks(TypedDict):
22+
initialize: Callable | None
23+
paint_module: Callable
24+
paint_text: Callable | None
25+
finish: Callable
26+
27+
828
try:
929
import Image
1030
import ImageDraw
@@ -61,30 +81,27 @@ class BaseWriter:
6181
attributes and can set them directly or using
6282
`self.set_options(option=value)`.
6383
64-
:parameters:
65-
initialize : Function
66-
Callback for initializing the inheriting writer.
67-
Is called: `callback_initialize(raw_code)`
68-
paint_module : Function
69-
Callback for painting one barcode module.
70-
Is called: `callback_paint_module(xpos, ypos, width, color)`
71-
paint_text : Function
72-
Callback for painting the text under the barcode.
73-
Is called: `callback_paint_text(xpos, ypos)` using `self.text`
74-
as text.
75-
finish : Function
76-
Callback for doing something with the completely rendered
77-
output.
78-
Is called: `return callback_finish()` and must return the
79-
rendered output.
84+
:param initialize: Callback for initializing the inheriting writer.
85+
Is called: `callback_initialize(raw_code)`
86+
:param paint_module:
87+
Callback for painting one barcode module.
88+
Is called: `callback_paint_module(xpos, ypos, width, color)`
89+
:param paint_text: Callback for painting the text under the barcode.
90+
Is called: `callback_paint_text(xpos, ypos)` using `self.text`
91+
as text.
92+
:param finish: Callback for doing something with the completely rendered
93+
output. Is called: `return callback_finish()` and must return the
94+
rendered output.
8095
"""
8196

97+
_callbacks: Callbacks
98+
8299
def __init__(
83100
self,
84-
initialize=None,
85-
paint_module=None,
86-
paint_text=None,
87-
finish=None,
101+
initialize: Callable | None,
102+
paint_module: Callable,
103+
paint_text: Callable | None,
104+
finish: Callable,
88105
) -> None:
89106
self._callbacks = {
90107
"initialize": initialize,
@@ -218,7 +235,7 @@ def render(self, code):
218235
# Left quiet zone is x startposition
219236
xpos = self.quiet_zone
220237
bxs = xpos # x start of barcode
221-
text = {
238+
text: InternalText = {
222239
"start": [], # The x start of a guard
223240
"end": [], # The x end of a guard
224241
"xpos": [], # The x position where to write a text block
@@ -281,12 +298,10 @@ def render(self, code):
281298
# The last text block is always put after the last guard end
282299
text["xpos"].append(text["end"][-1] + 4 * self.module_width)
283300

284-
# Split the ean into its blocks
285-
self.text = self.text.split(" ")
286-
287301
ypos += pt2mm(self.font_size)
288302

289-
blocks = self.text
303+
# Split the ean into its blocks
304+
blocks = self.text.split(" ")
290305
for text_, xpos in zip(blocks, text["xpos"]):
291306
self.text = text_
292307
self._callbacks["paint_text"](xpos, ypos)

0 commit comments

Comments
 (0)