Skip to content

Commit a31d1bd

Browse files
committed
Add a few type hints
1 parent 2e765bd commit a31d1bd

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

barcode/ean.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ class EuropeanArticleNumber13(Barcode):
4242
digits = 12
4343

4444
def __init__(
45-
self, ean: str, writer=None, no_checksum=False, guardbar=False
45+
self,
46+
ean: str,
47+
writer=None,
48+
no_checksum: bool = False,
49+
guardbar: bool = False,
4650
) -> None:
4751
if not ean[: self.digits].isdigit():
4852
raise IllegalCharacterError(f"EAN code can only contain numbers {ean}.")
@@ -121,7 +125,7 @@ def to_ascii(self) -> str:
121125
code = code_list[0]
122126
return code.replace("G", "|").replace("1", "|").replace("0", " ")
123127

124-
def render(self, writer_options=None, text=None):
128+
def render(self, writer_options: dict | None = None, text: str | None = None):
125129
options = {"module_width": SIZES["SC2"]}
126130
options.update(writer_options or {})
127131
return super().render(options, text)
@@ -161,11 +165,8 @@ def __init__(self, jan, *args, **kwargs) -> None:
161165
class EuropeanArticleNumber8(EuropeanArticleNumber13):
162166
"""Represents an EAN-8 barcode. See EAN13's __init__ for details.
163167
164-
:parameters:
165-
ean : String
166-
The ean number as string.
167-
writer : barcode.writer Instance
168-
The writer to render the barcode (default: SVGWriter).
168+
:param ean: The ean number as string.
169+
:param writer: The writer to render the barcode (default: SVGWriter).
169170
"""
170171

171172
name = "EAN-8"
@@ -197,7 +198,13 @@ class EuropeanArticleNumber8WithGuard(EuropeanArticleNumber8):
197198

198199
name = "EAN-8 with guards"
199200

200-
def __init__(self, ean, writer=None, no_checksum=False, guardbar=True) -> None:
201+
def __init__(
202+
self,
203+
ean: str,
204+
writer=None,
205+
no_checksum: bool = False,
206+
guardbar: bool = True,
207+
) -> None:
201208
super().__init__(ean, writer, no_checksum, guardbar)
202209

203210

barcode/writer.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,15 @@ def pt2mm(pt: float) -> float:
5050
return pt * 0.352777778
5151

5252

53-
def _set_attributes(element, **attributes):
53+
def _set_attributes(
54+
element: xml.dom.minidom.Element,
55+
**attributes: str,
56+
) -> None:
5457
for key, value in attributes.items():
5558
element.setAttribute(key, value)
5659

5760

58-
def create_svg_object(with_doctype=False) -> xml.dom.minidom.Document:
61+
def create_svg_object(with_doctype: bool = False) -> xml.dom.minidom.Document:
5962
imp = xml.dom.minidom.getDOMImplementation()
6063
assert imp is not None
6164
doctype = imp.createDocumentType(
@@ -64,6 +67,7 @@ def create_svg_object(with_doctype=False) -> xml.dom.minidom.Document:
6467
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd",
6568
)
6669
document = imp.createDocument(None, "svg", doctype if with_doctype else None)
70+
assert document.documentElement is not None
6771
_set_attributes(
6872
document.documentElement, version="1.1", xmlns="http://www.w3.org/2000/svg"
6973
)
@@ -385,7 +389,7 @@ def _create_text(self, xpos, ypos):
385389
self._group.appendChild(element)
386390
ypos += pt2mm(self.font_size) + self.text_line_distance
387391

388-
def _finish(self):
392+
def _finish(self) -> bytes:
389393
if self.compress:
390394
return self._document.toxml(encoding="UTF-8")
391395

@@ -422,7 +426,12 @@ class ImageWriter(BaseWriter): # type: ignore[no-redef]
422426
mode: str
423427
dpi: int
424428

425-
def __init__(self, format="PNG", mode="RGB", dpi=300) -> None:
429+
def __init__(
430+
self,
431+
format: str = "PNG",
432+
mode: str = "RGB",
433+
dpi: int = 300,
434+
) -> None:
426435
"""Initialise a new write instance.
427436
428437
:params format: The file format for the generated image. This parameter can

0 commit comments

Comments
 (0)