Skip to content

Validate CelebA target_type in __init__ using verify_str_arg#9449

Open
sunnycho100 wants to merge 2 commits intopytorch:mainfrom
sunnycho100:celeba-verify-str-arg-target-type
Open

Validate CelebA target_type in __init__ using verify_str_arg#9449
sunnycho100 wants to merge 2 commits intopytorch:mainfrom
sunnycho100:celeba-verify-str-arg-target-type

Conversation

@sunnycho100
Copy link

Motivation

There's a long-standing TODO comment in CelebA.__getitem__() (line ~189):

# TODO: refactor with utils.verify_str_arg

Currently, invalid target_type values are only caught at __getitem__ time via a raw ValueError. Meanwhile, the split parameter is already validated in __init__() using verify_str_arg. This inconsistency means users don't get an error until they try to access an item, even though the problem is known at construction time.

Changes

torchvision/datasets/celeba.py

  • Added verify_str_arg validation for each entry in target_type inside __init__(), right after the list normalization — same pattern already used for split
  • Removed the now-unreachable else branch (and the TODO) from __getitem__()

Before / After

# Before — error only at item access time
ds = CelebA(root, target_type="typo")  # no error
ds[0]  # ValueError: Target type "typo" is not recognized.

# After — error immediately at construction
ds = CelebA(root, target_type="typo")
# ValueError: Unknown value 'typo' for argument target_type.
#   Valid values are {'attr', 'identity', 'bbox', 'landmarks'}.

Notes

  • Fully backward-compatible — only changes when the error is raised, not what is accepted
  • No new dependencies — verify_str_arg is already imported in the file

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 18, 2026

🔗 Helpful Links

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

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

✅ No Failures

As of commit 875de32 with merge base a025873 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@meta-cla meta-cla bot added the cla signed label Mar 18, 2026
@sunnycho100 sunnycho100 marked this pull request as ready for review March 18, 2026 16:39
@zy1git
Copy link
Contributor

zy1git commented Mar 19, 2026

@sunnycho100 Thanks a lot for this PR! After discussion, we won’t be merging it as-is since it isn’t a current priority for TorchVision.
If you update this PR to only remove the # TODO: refactor with utils.verify_str_arg comment and adjust the PR description accordingly, I’m happy to approve and merge it.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants