Skip to content

Incorrect/inconsistent handling of PIL.Image.Image when using PEP 695 Type Parameters #19303

Closed as not planned
@PaarthShah

Description

@PaarthShah

Bug Report

When using PEP 695 type parameters to annotate a function returning an Image from pillow, mypy produces an error where normally it would pass without issue.
Link to pillow: https://github.com/python-pillow/Pillow

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.13&gist=bde516a7d23bba4bd1bbf29d0490c882
https://gist.github.com/mypy-play/bde516a7d23bba4bd1bbf29d0490c882

Full example code

from PIL import Image
from typing import reveal_type

def no_error1(image: Image.Image) -> Image.Image:
    reveal_type(image)
    return image

def no_error2[T: Image.Image](image: T) -> T:
    reveal_type(image)
    return image

def no_error3(image: Image.Image) -> Image.Image:
    reveal_type(image)
    return Image.new('RGBA', image.size, (0, 0, 0, 0))

type T = Image.Image

def no_error4(image: T) -> T:
    reveal_type(image)
    return Image.new('RGBA', image.size, (0, 0, 0, 0))

def error[T: Image.Image](image: T) -> T:
    reveal_type(image)
    return Image.new('RGBA', image.size, (0, 0, 0, 0))

Expected Behavior

No errors

Actual Behavior

scratch_4.py:5: note: Revealed type is "PIL.Image.Image"
scratch_4.py:9: note: Revealed type is "T`-1"
scratch_4.py:13: note: Revealed type is "PIL.Image.Image"
scratch_4.py:19: note: Revealed type is "PIL.Image.Image"
scratch_4.py:23: note: Revealed type is "T`-1"
scratch_4.py:24: error: Incompatible return value type (got "Image", expected "T")  [return-value]

Your Environment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions