Skip to content

Commit 416aec0

Browse files
committed
Add some additional type hints
1 parent 2735505 commit 416aec0

3 files changed

Lines changed: 32 additions & 24 deletions

File tree

barcode/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
from typing import BinaryIO
88
from typing import Dict
9+
from typing import Optional
910
from typing import Union
1011

1112
from barcode.codabar import CODABAR
@@ -56,16 +57,21 @@
5657
PROVIDED_BARCODES.sort()
5758

5859

59-
def get(name, code=None, writer=None, options=None):
60+
def get(
61+
name: str,
62+
code: Optional[str] = None,
63+
writer=None,
64+
options: Optional[dict] = None,
65+
):
6066
"""Helper method for getting a generator or even a generated code.
6167
62-
:param str name: The name of the type of barcode desired.
63-
:param str code: The actual information to encode. If this parameter is
68+
:param name: The name of the type of barcode desired.
69+
:param code: The actual information to encode. If this parameter is
6470
provided, a generated barcode is returned. Otherwise, the barcode class
6571
is returned.
6672
:param Writer writer: An alternative writer to use when generating the
6773
barcode.
68-
:param dict options: Additional options to be passed on to the barcode when
74+
:param options: Additional options to be passed on to the barcode when
6975
generating.
7076
"""
7177
options = options or {}

barcode/base.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"""barcode.base
22
33
"""
4+
from typing import List
5+
from typing import Optional
6+
7+
from barcode.writer import BaseWriter
48
from barcode.writer import SVGWriter
59

610

@@ -23,7 +27,9 @@ class Barcode:
2327
"text": "",
2428
}
2529

26-
def to_ascii(self):
30+
writer: BaseWriter
31+
32+
def to_ascii(self) -> str:
2733
code = self.build()
2834
for i, line in enumerate(code):
2935
code[i] = line.replace("1", "X").replace("0", " ")
@@ -32,7 +38,7 @@ def to_ascii(self):
3238
def __repr__(self):
3339
return f"<{self.__class__.__name__}({self.get_fullcode()!r})>"
3440

35-
def build(self):
41+
def build(self) -> List[str]:
3642
raise NotImplementedError
3743

3844
def get_fullcode(self):
@@ -43,20 +49,16 @@ def get_fullcode(self):
4349
"""
4450
raise NotImplementedError
4551

46-
def save(self, filename, options=None, text=None):
52+
def save(
53+
self, filename: str, options: Optional[dict] = None, text: Optional[str] = None
54+
) -> str:
4755
"""Renders the barcode and saves it in `filename`.
4856
49-
:parameters:
50-
filename : String
51-
Filename to save the barcode in (without filename
52-
extension).
53-
options : Dict
54-
The same as in `self.render`.
55-
text : str
56-
Text to render under the barcode.
57+
:param filename: Filename to save the barcode in (without filename extension).
58+
:param options: The same as in `self.render`.
59+
:param text: Text to render under the barcode.
5760
5861
:returns: The full filename with extension.
59-
:rtype: String
6062
"""
6163
output = self.render(options, text) if text else self.render(options)
6264

@@ -77,14 +79,11 @@ def write(self, fp, options=None, text=None):
7779
output = self.render(options, text)
7880
self.writer.write(output, fp)
7981

80-
def render(self, writer_options=None, text=None):
82+
def render(self, writer_options: Optional[dict] = None, text: Optional[str] = None):
8183
"""Renders the barcode using `self.writer`.
8284
83-
:parameters:
84-
writer_options : Dict
85-
Options for `self.writer`, see writer docs for details.
86-
text : str
87-
Text to render under the barcode.
85+
:param writer_options: Options for `self.writer`, see writer docs for details.
86+
:param text: Text to render under the barcode.
8887
8988
:returns: Output of the writers render method.
9089
"""

barcode/codex.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
:Provided barcodes: Code 39, Code 128, PZN
44
"""
5+
6+
from typing import Tuple
7+
58
from barcode.base import Barcode
69
from barcode.charsets import code39
710
from barcode.charsets import code128
@@ -16,7 +19,7 @@
1619
MIN_QUIET_ZONE = 2.54
1720

1821

19-
def check_code(code, name, allowed):
22+
def check_code(code: str, name: str, allowed: Tuple[str, ...]) -> None:
2023
wrong = []
2124
for char in code:
2225
if char not in allowed:
@@ -48,7 +51,7 @@ def __init__(self, code: str, writer=None, add_checksum: bool = True):
4851
self.writer = writer or self.default_writer()
4952
check_code(self.code, self.name, code39.REF)
5053

51-
def __str__(self):
54+
def __str__(self) -> str:
5255
return self.code
5356

5457
def get_fullcode(self) -> str:

0 commit comments

Comments
 (0)