11import importlib .metadata
2+ import typing
23import warnings
34
45from PIL import Image
56
6- from . import compat
7+ from ._zbarlight import Symbologies
8+ from ._zbarlight import zbar_code_scanner
79
8- # FIXME: pylint is capable of reading .pyi stub files, which we'll
9- # create when we set up a type checker. Remove the following pragmas
10- # once it's done.
11- from ._zbarlight import Symbologies # pylint: disable=no-name-in-module
12- from ._zbarlight import zbar_code_scanner # pylint: disable=no-name-in-module
1310
1411__version__ = importlib .metadata .version ("zbarlight" )
1512__ALL__ = [
@@ -29,7 +26,7 @@ class UnknownSymbologieError(Exception):
2926 pass
3027
3128
32- def scan_codes (code_types , image ) :
29+ def scan_codes (code_types : str | list [ str ] , image : Image . Image ) -> list [ bytes ] | None :
3330 """
3431 Get *code_type* codes from a PIL Image.
3532
@@ -69,9 +66,10 @@ def scan_codes(code_types, image):
6966 if code_type .upper () not in Symbologies
7067 ]
7168 raise UnknownSymbologieError ("Unknown Symbologies: %s" % bad_code_types )
69+ # mypy cannot narrow the type, despite our membership check above.
70+ if not _has_only_integers (symbologies ):
71+ raise RuntimeError ("should not happen" )
7272
73- if not compat .is_image (image ):
74- raise RuntimeError ("Bad or unknown image format" )
7573 # Convert image to gray scale (8 bits per pixel).
7674 converted_image = image .convert ("L" )
7775 raw = converted_image .tobytes ()
@@ -80,7 +78,9 @@ def scan_codes(code_types, image):
8078 return zbar_code_scanner (symbologies , raw , width , height )
8179
8280
83- def copy_image_on_background (image , color = WHITE ):
81+ def copy_image_on_background (
82+ image : Image .Image , color : tuple [int , int , int ] = WHITE
83+ ) -> Image .Image :
8484 """
8585 Create a new image by copying the image on a *color* background.
8686
@@ -95,3 +95,7 @@ def copy_image_on_background(image, color=WHITE):
9595 background = Image .new ("RGB" , image .size , color )
9696 background .paste (image , mask = image .split ()[3 ])
9797 return background
98+
99+
100+ def _has_only_integers (values : list [int | None ]) -> typing .TypeGuard [list [int ]]:
101+ return None not in values
0 commit comments