Allow pyright to "see" core properly

Without this change, the following file raises errors (in strict mode)
and reveals the type as "Unknown". After this change, the type is
correctly revealed as "ImagingCore".

  # pyright: strict

  from PIL import Image

  img = Image.new('RGB', (10, 10))
  data = img.getdata()
  reveal_type(data)
This commit is contained in:
Joey Marianer 2025-08-19 22:01:07 -07:00
parent 34c651deb8
commit 9240ec96c0

View File

@ -86,38 +86,41 @@ WARN_POSSIBLE_FORMATS: bool = False
MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3) MAX_IMAGE_PIXELS: int | None = int(1024 * 1024 * 1024 // 4 // 3)
try: if TYPE_CHECKING:
# If the _imaging C module is not present, Pillow will not load.
# Note that other modules should not refer to _imaging directly;
# import Image and use the Image.core variable instead.
# Also note that Image.core is not a publicly documented interface,
# and should be considered private and subject to change.
from . import _imaging as core from . import _imaging as core
else:
try:
# If the _imaging C module is not present, Pillow will not load.
# Note that other modules should not refer to _imaging directly;
# import Image and use the Image.core variable instead.
# Also note that Image.core is not a publicly documented interface,
# and should be considered private and subject to change.
from . import _imaging as core
if __version__ != getattr(core, "PILLOW_VERSION", None): if __version__ != getattr(core, "PILLOW_VERSION", None):
msg = ( msg = (
"The _imaging extension was built for another version of Pillow or PIL:\n" "The _imaging extension was built for another version of Pillow or PIL:\n"
f"Core version: {getattr(core, 'PILLOW_VERSION', None)}\n" f"Core version: {getattr(core, 'PILLOW_VERSION', None)}\n"
f"Pillow version: {__version__}" f"Pillow version: {__version__}"
) )
raise ImportError(msg) raise ImportError(msg)
except ImportError as v: except ImportError as v:
core = DeferredError.new(ImportError("The _imaging C module is not installed.")) core = DeferredError.new(ImportError("The _imaging C module is not installed."))
# Explanations for ways that we know we might have an import error # Explanations for ways that we know we might have an import error
if str(v).startswith("Module use of python"): if str(v).startswith("Module use of python"):
# The _imaging C module is present, but not compiled for # The _imaging C module is present, but not compiled for
# the right version (windows only). Print a warning, if # the right version (windows only). Print a warning, if
# possible. # possible.
warnings.warn( warnings.warn(
"The _imaging extension was built for another version of Python.", "The _imaging extension was built for another version of Python.",
RuntimeWarning, RuntimeWarning,
) )
elif str(v).startswith("The _imaging extension"): elif str(v).startswith("The _imaging extension"):
warnings.warn(str(v), RuntimeWarning) warnings.warn(str(v), RuntimeWarning)
# Fail here anyway. Don't let people run with a mostly broken Pillow. # Fail here anyway. Don't let people run with a mostly broken Pillow.
# see docs/porting.rst # see docs/porting.rst
raise raise
# #