Type annotations: Switch to type alias for Mode & add self.im annotation.

This commit is contained in:
neiljp (Neil Pilgrim) 2017-08-17 21:54:51 -07:00 committed by Eric Soroos
parent 97c2312c07
commit 43b704b7df

View File

@ -34,6 +34,7 @@ if False:
Size = XY # NOTE: All XY aliases will be interchangeable Size = XY # NOTE: All XY aliases will be interchangeable
Matrix4 = Tuple[float, float, float, float] Matrix4 = Tuple[float, float, float, float]
Matrix12 = Tuple[float, float, float, float, float, float, float, float, float, float, float, float] Matrix12 = Tuple[float, float, float, float, float, float, float, float, float, float, float, float]
Mode = str
# Just required for typing, or gradual module inclusion while adding annotation? # Just required for typing, or gradual module inclusion while adding annotation?
from io import BytesIO from io import BytesIO
@ -320,7 +321,7 @@ _MAPMODES = ("L", "P", "RGBX", "RGBA", "CMYK", "I;16", "I;16L", "I;16B")
def getmodebase(mode): def getmodebase(mode):
# type: (Text) -> Text # type: (Mode) -> Mode
""" """
Gets the "base" mode for given mode. This function returns "L" for Gets the "base" mode for given mode. This function returns "L" for
images that contain grayscale data, and "RGB" for images that images that contain grayscale data, and "RGB" for images that
@ -334,7 +335,7 @@ def getmodebase(mode):
def getmodetype(mode): def getmodetype(mode):
# type: (Text) -> Text # type: (Mode) -> Mode
""" """
Gets the storage type mode. Given a mode, this function returns a Gets the storage type mode. Given a mode, this function returns a
single-layer mode suitable for storing individual bands. single-layer mode suitable for storing individual bands.
@ -347,7 +348,7 @@ def getmodetype(mode):
def getmodebandnames(mode): def getmodebandnames(mode):
# type: (Text) -> Tuple # type: (Mode) -> Tuple
""" """
Gets a list of individual band names. Given a mode, this function returns Gets a list of individual band names. Given a mode, this function returns
a tuple containing the names of individual bands (use a tuple containing the names of individual bands (use
@ -363,7 +364,7 @@ def getmodebandnames(mode):
def getmodebands(mode): def getmodebands(mode):
# type: (Text) -> int # type: (Mode) -> int
""" """
Gets the number of individual bands for this mode. Gets the number of individual bands for this mode.
@ -445,7 +446,7 @@ def init():
# Codec factories (used by tobytes/frombytes and ImageFile.load) # Codec factories (used by tobytes/frombytes and ImageFile.load)
def _getdecoder(mode, decoder_name, args, extra=()): def _getdecoder(mode, decoder_name, args, extra=()):
# type: (Text, Text, Optional[Tuple], Tuple) -> PyDecoder # type: (Mode, Text, Optional[Tuple], Tuple) -> PyDecoder
# tweak arguments # tweak arguments
if args is None: if args is None:
@ -468,7 +469,7 @@ def _getdecoder(mode, decoder_name, args, extra=()):
def _getencoder(mode, encoder_name, args, extra=()): def _getencoder(mode, encoder_name, args, extra=()):
# type: (Text, Text, Optional[Tuple], Tuple) -> PyEncoder # type: (Mode, Text, Optional[Tuple], Tuple) -> PyEncoder
# tweak arguments # tweak arguments
if args is None: if args is None:
@ -551,7 +552,7 @@ class Image(object):
# type: () -> None # type: () -> None
# FIXME: take "new" parameters / other image? # FIXME: take "new" parameters / other image?
# FIXME: turn mode and size into delegating properties? # FIXME: turn mode and size into delegating properties?
self.im = None self.im = None # type: Optional[Any]
self.mode = "" self.mode = ""
self.size = (0, 0) self.size = (0, 0)
self.palette = None # type: Optional[ImagePalette.ImagePalette] self.palette = None # type: Optional[ImagePalette.ImagePalette]
@ -893,7 +894,7 @@ class Image(object):
def convert(self, mode=None, matrix=None, dither=None, def convert(self, mode=None, matrix=None, dither=None,
palette=WEB, colors=256): palette=WEB, colors=256):
# type: (Optional[Text], Optional[Union[Matrix4, Matrix12]], Optional[int], Optional[int], int) -> Image # type: (Optional[Mode], Optional[Union[Matrix4, Matrix12]], Optional[int], Optional[int], int) -> Image
""" """
Returns a converted copy of this image. For the "P" mode, this Returns a converted copy of this image. For the "P" mode, this
method translates pixels through the palette. If mode is method translates pixels through the palette. If mode is
@ -1531,7 +1532,7 @@ class Image(object):
self.paste(result, box) self.paste(result, box)
def point(self, lut, mode=None): def point(self, lut, mode=None):
# type: (Union[List, Callable[[Any], Any]], Optional[Text]) -> Image # type: (Union[List, Callable[[Any], Any]], Optional[Mode]) -> Image
""" """
Maps this image through a lookup table or function. Maps this image through a lookup table or function.
@ -1644,7 +1645,7 @@ class Image(object):
self.im.putdata(data, scale, offset) self.im.putdata(data, scale, offset)
def putpalette(self, data, rawmode="RGB"): def putpalette(self, data, rawmode="RGB"):
# type: (Sequence[Union[int, bytes]], Text) -> None # type: (Sequence[Union[int, bytes]], Mode) -> None
""" """
Attaches a palette to this image. The image must be a "P" or Attaches a palette to this image. The image must be a "P" or
"L" image, and the palette sequence must contain 768 integer "L" image, and the palette sequence must contain 768 integer
@ -2365,7 +2366,7 @@ def _check_size(size):
def new(mode, size, color=0): def new(mode, size, color=0):
# type: (Text, Size, Optional[Union[int, float, Tuple, Text]]) -> Image # type: (Mode, Size, Optional[Union[int, float, Tuple, Text]]) -> Image
""" """
Creates a new image with the given mode and size. Creates a new image with the given mode and size.
@ -2397,7 +2398,7 @@ def new(mode, size, color=0):
def frombytes(mode, size, data, decoder_name="raw", *args): def frombytes(mode, size, data, decoder_name="raw", *args):
# type: (Text, Size, Sequence[bytes], Text, *Any) -> Image # type: (Mode, Size, Sequence[bytes], Text, *Any) -> Image
""" """
Creates a copy of an image memory from pixel data in a buffer. Creates a copy of an image memory from pixel data in a buffer.
@ -2442,7 +2443,7 @@ def fromstring(*args, **kw):
def frombuffer(mode, size, data, decoder_name="raw", *args): def frombuffer(mode, size, data, decoder_name="raw", *args):
# type: (Text, Size, Sequence[bytes], Text, *Any) -> Image # type: (Mode, Size, Sequence[bytes], Text, *Any) -> Image
""" """
Creates an image memory referencing pixel data in a byte buffer. Creates an image memory referencing pixel data in a byte buffer.
@ -2504,7 +2505,7 @@ def frombuffer(mode, size, data, decoder_name="raw", *args):
def fromarray(obj, mode=None): def fromarray(obj, mode=None):
# type: (object, Optional[Text]) -> Image # type: (object, Optional[Mode]) -> Image
""" """
Creates an image memory from an object exporting the array interface Creates an image memory from an object exporting the array interface
(using the buffer protocol). (using the buffer protocol).
@ -2775,7 +2776,7 @@ def eval(image, *args):
def merge(mode, bands): def merge(mode, bands):
# type: (Text, Sequence[Image]) -> Image # type: (Mode, Sequence[Image]) -> Image
""" """
Merge a set of single band images into a new multiband image. Merge a set of single band images into a new multiband image.
@ -2961,7 +2962,7 @@ def effect_noise(size, sigma):
def linear_gradient(mode): def linear_gradient(mode):
# type: (Text) -> Image # type: (Mode) -> Image
""" """
Generate 256x256 linear gradient from black to white, top to bottom. Generate 256x256 linear gradient from black to white, top to bottom.
@ -2971,7 +2972,7 @@ def linear_gradient(mode):
def radial_gradient(mode): def radial_gradient(mode):
# type: (Text) -> Image # type: (Mode) -> Image
""" """
Generate 256x256 radial gradient from black to white, centre to edge. Generate 256x256 radial gradient from black to white, centre to edge.