1414from barcode .errors import BarcodeError
1515from barcode .errors import IllegalCharacterError
1616from barcode .errors import NumberOfDigitsError
17+ from barcode .writer import T_Output
1718
1819if TYPE_CHECKING :
1920 from collections .abc import Collection
@@ -40,12 +41,17 @@ def check_code(code: str, name: str, allowed: Collection[str]) -> None:
4041 )
4142
4243
43- class Code39 (Barcode ):
44+ class Code39 (Barcode [ T_Output ] ):
4445 """A Code39 barcode implementation"""
4546
4647 name = "Code 39"
4748
48- def __init__ (self , code : str , writer = None , add_checksum : bool = True ) -> None :
49+ def __init__ (
50+ self ,
51+ code : str ,
52+ writer : BaseWriter [T_Output ] | None = None ,
53+ add_checksum : bool = True ,
54+ ) -> None :
4955 r"""
5056 :param code: Code 39 string without \* and without checksum.
5157 :param writer: A ``barcode.writer`` instance used to render the barcode
@@ -56,7 +62,7 @@ def __init__(self, code: str, writer=None, add_checksum: bool = True) -> None:
5662 self .code = code .upper ()
5763 if add_checksum :
5864 self .code += self .calculate_checksum ()
59- self .writer = writer or self .default_writer ( )
65+ self .writer = self ._resolve_writer ( writer )
6066 check_code (self .code , self .name , code39 .REF )
6167
6268 def __str__ (self ) -> str :
@@ -83,13 +89,17 @@ def build(self) -> list[str]:
8389 result = code39 .MIDDLE .join (chars )
8490 return [result ]
8591
86- def render (self , writer_options = None , text = None ):
92+ def render (
93+ self ,
94+ writer_options : dict | None = None ,
95+ text : str | None = None ,
96+ ) -> T_Output :
8797 options = {"module_width" : MIN_SIZE , "quiet_zone" : MIN_QUIET_ZONE }
8898 options .update (writer_options or {})
8999 return super ().render (options , text )
90100
91101
92- class PZN7 (Code39 ):
102+ class PZN7 (Code39 [ T_Output ] ):
93103 """Initializes new German number for pharmaceutical products.
94104
95105 :param pzn: Code to render.
@@ -100,7 +110,7 @@ class PZN7(Code39):
100110
101111 digits = 6
102112
103- def __init__ (self , pzn , writer = None ) -> None :
113+ def __init__ (self , pzn , writer : BaseWriter [ T_Output ] | None = None ) -> None :
104114 pzn = pzn [: self .digits ]
105115 if not pzn .isdigit ():
106116 raise IllegalCharacterError ("PZN can only contain numbers." )
@@ -124,13 +134,13 @@ def calculate_checksum(self):
124134 return checksum
125135
126136
127- class PZN8 (PZN7 ):
137+ class PZN8 (PZN7 [ T_Output ] ):
128138 """Will be fully added in v0.9."""
129139
130140 digits = 7
131141
132142
133- class Code128 (Barcode ):
143+ class Code128 (Barcode [ T_Output ] ):
134144 """Initializes a new Code128 instance. The checksum is added automatically
135145 when building the bars.
136146
@@ -141,12 +151,11 @@ class Code128(Barcode):
141151 name = "Code 128"
142152 _charset : Literal ["A" , "B" , "C" ]
143153 code : str
144- writer : BaseWriter
145154 buffer : str
146155
147- def __init__ (self , code : str , writer = None ) -> None :
156+ def __init__ (self , code : str , writer : BaseWriter [ T_Output ] | None = None ) -> None :
148157 self .code = code
149- self .writer = writer or self .default_writer ( )
158+ self .writer = self ._resolve_writer ( writer )
150159 self ._charset = "C"
151160 self ._digit_buffer = "" # Accumulate pairs of digits for charset C
152161 check_code (self .code , self .name , code128 .ALL )
@@ -307,13 +316,17 @@ def build(self) -> list[str]:
307316 code += "11"
308317 return [code ]
309318
310- def render (self , writer_options = None , text = None ):
319+ def render (
320+ self ,
321+ writer_options : dict | None = None ,
322+ text : str | None = None ,
323+ ) -> T_Output :
311324 options = {"module_width" : MIN_SIZE , "quiet_zone" : MIN_QUIET_ZONE }
312325 options .update (writer_options or {})
313326 return super ().render (options , text )
314327
315328
316- class Gs1_128 (Code128 ): # noqa: N801
329+ class Gs1_128 (Code128 [ T_Output ] ): # noqa: N801
317330 """
318331 following the norm, a gs1-128 barcode is a subset of code 128 barcode,
319332 it can be generated by prepending the code with the FNC1 character
@@ -325,7 +338,7 @@ class Gs1_128(Code128): # noqa: N801
325338
326339 FNC1_CHAR = "\xf1 "
327340
328- def __init__ (self , code , writer = None ) -> None :
341+ def __init__ (self , code , writer : BaseWriter [ T_Output ] | None = None ) -> None :
329342 code = self .FNC1_CHAR + code
330343 super ().__init__ (code , writer )
331344
0 commit comments