Skip to content

Fix type hint inconsistencies in transforms/functional.py#9516

Open
Nueramarcos wants to merge 2 commits into
pytorch:mainfrom
Nueramarcos:fix/9257-type-hint-inconsistencies
Open

Fix type hint inconsistencies in transforms/functional.py#9516
Nueramarcos wants to merge 2 commits into
pytorch:mainfrom
Nueramarcos:fix/9257-type-hint-inconsistencies

Conversation

@Nueramarcos

@Nueramarcos Nueramarcos commented Jun 14, 2026

Copy link
Copy Markdown

Fixes #9257

Problem

27 public functions in torchvision/transforms/functional.py accept both Tensor and PILImage at runtime (branching internally on isinstance(img, torch.Tensor)), but their signatures were annotated only as img: Tensor. This causes false positives in type checkers (PyRight, mypy) and suppresses IDE autocomplete for PIL users.

Additionally, pil_to_tensor was typed as pic: Any when it only accepts PILImage.

Changes

30 signature lines updated across 27 functions — both img parameter types and return types:

Category Functions
Getters get_dimensions, get_image_size, get_image_num_channels
Conversions pil_to_tensor (Any → PILImage)
Geometric resize, pad, crop, hflip, vflip, perspective, rotate, affine
Color adjust adjust_brightness, adjust_contrast, adjust_saturation, adjust_hue, adjust_gamma, adjust_sharpness
Color ops rgb_to_grayscale, gaussian_blur, invert, posterize, solarize, autocontrast, equalize
Advanced elastic_transform

Not changed: erase() — explicitly documented as Tensor-only and raises TypeError for PIL input.

Notes

  • Zero functional changes — purely annotation updates
  • Backwards compatible: Union[Tensor, PILImage] is a superset of Tensor
  • Union and PILImage are already imported at the top of the file — no new imports needed
  • Return types updated to match: functions that accept both types also return the matching type
  • Identified via static analysis of all 847+ function signatures in the file

27 public functions accepted both Tensor and PILImage at runtime
but were only annotated as img: Tensor. Update all 29 affected
parameter annotations to img: Union[Tensor, PILImage] for accurate
type checking and better IDE support.

Note: erase() intentionally kept as img: Tensor — it explicitly
does not support PIL Image and raises TypeError if given one.

Fixes pytorch#9257
@pytorch-bot

pytorch-bot Bot commented Jun 14, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9516

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Jun 14, 2026

Copy link
Copy Markdown

Hi @Nueramarcos!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

27 public functions accepted both Tensor and PILImage at runtime but
were annotated only as img: Tensor. Update all affected parameter and
return type annotations to use Union[Tensor, PILImage].

Also fixes pil_to_tensor(pic: Any) -> pic: PILImage since that function
only accepts PIL images.

erase() intentionally unchanged — explicitly PIL-incompatible (raises
TypeError for non-Tensor input).

Fixes pytorch#9257
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type hints of torchvision are not constistent with implementation

1 participant