Fix type hint inconsistencies in transforms/functional.py#9516
Fix type hint inconsistencies in transforms/functional.py#9516Nueramarcos wants to merge 2 commits into
Conversation
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
🔗 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 SEVsThere 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. |
|
Hi @Nueramarcos! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 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
Fixes #9257
Problem
27 public functions in
torchvision/transforms/functional.pyaccept bothTensorandPILImageat runtime (branching internally onisinstance(img, torch.Tensor)), but their signatures were annotated only asimg: Tensor. This causes false positives in type checkers (PyRight, mypy) and suppresses IDE autocomplete for PIL users.Additionally,
pil_to_tensorwas typed aspic: Anywhen it only acceptsPILImage.Changes
30 signature lines updated across 27 functions — both
imgparameter types and return types:get_dimensions,get_image_size,get_image_num_channelspil_to_tensor(Any → PILImage)resize,pad,crop,hflip,vflip,perspective,rotate,affineadjust_brightness,adjust_contrast,adjust_saturation,adjust_hue,adjust_gamma,adjust_sharpnessrgb_to_grayscale,gaussian_blur,invert,posterize,solarize,autocontrast,equalizeelastic_transformNot changed:
erase()— explicitly documented as Tensor-only and raisesTypeErrorfor PIL input.Notes
Union[Tensor, PILImage]is a superset ofTensorUnionandPILImageare already imported at the top of the file — no new imports needed