Fix DTD partition validation using and instead of or - #9561
Open
Osamaali313 wants to merge 1 commit into
Open
Conversation
`DTD.__init__` validates the `partition` argument with:
if not isinstance(partition, int) and not (1 <= partition <= 10):
raise ValueError(...)
The intent is to reject any value that is not an int in [1, 10]. The correct
predicate is `not (isinstance(partition, int) and 1 <= partition <= 10)`,
which by De Morgan is `not isinstance(...) or not (1 <= ... <= 10)`. Using
`and` means the error is raised only when the value is simultaneously a
non-int and out of range, so a normal out-of-range int like `11` (where
`not isinstance(11, int)` is False) short-circuits and slips through.
`DTD(root, partition=11)` then fails later at
`open(... f"{self._split}{self._partition}.txt")` with an opaque
`FileNotFoundError: ...train11.txt` instead of the documented ValueError.
A str partition like "1" raises `TypeError` from `1 <= "1"` for the same
reason; the `or` fix short-circuits before the comparison and handles that
too. The error message in this same statement documents the intended
contract ("integer with 1 <= partition <= 10"). Use `or`.
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9561
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
DTD.__init__(torchvision/datasets/dtd.py) validates thepartitionargument withand:The intent is to reject any value that isn't an int in
[1, 10]. The correct predicate isnot (isinstance(partition, int) and 1 <= partition <= 10), which by De Morgan isnot isinstance(...) or not (1 <= ... <= 10). Withand, the error fires only when the value is both a non-int and out of range — so a normal out-of-range int like11(not isinstance(11, int)→False) short-circuits theandtoFalseand passes validation.Impact
DTD(root, partition=11)(or0,-3) slips past the check and then dies later at:with an opaque
FileNotFoundError: ...train11.txtinstead of the documentedValueError. Astrpartition like"1"raisesTypeErrorfrom1 <= "1"for the same reason (theorfix short-circuits before the comparison and handles that too).torchvision.datasets.DTDis public andpartitionis a documented constructor arg; the error message in this very statement documents the contract the guard fails to enforce.Reproduction
and)or)FileNotFoundError)ValueError✅"1"TypeErrorValueError✅Fix