Lightning-AI / Lightning-AI/torchmetrics
PSNRB: `block_size` input validation is broken
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 2.5k
- Forks
- 526
- Avg merge
- 6d 11h
- Merged PRs (30d)
- 5
Description
## 🐛 Bug
Input validation for `block_size` in `PeakSignalNoiseRatioWithBlockedEffect` is broken: invalid values either slip through silently or raise `TypeError`, instead of the intended `ValueError`.
[`src/torchmetrics/image/psnrb.py:82`](https://github.com/Lightning-AI/torchmetrics/blob/master/src/torchmetrics/image/psnrb.py#L82) uses `and` where `or` is intended:
```python
if not isinstance(block_size, int) and block_size < 1:
raise ValueError("Argument ``block_size`` should be a positive integer")
```
With `and`, the first clause (`not isinstance(..., int)`) is False for every int, which short-circuits the expression and skips the `< 1` check, so invalid ints like `0` and `-5` slip through. For non-ints, the second comparison runs and either returns False (e.g. `1.5 < 1`) or raises `TypeError` (e.g. `"foo" < 1`). Neither path reaches the intended `ValueError`.
| Input | Observed | Expected |
|---|---|---|
| `0` | no error | `ValueError` |
| `-5` | no error | `ValueError` |
| `1.5` | no error | `ValueError` |
| `"foo"` | `TypeError: '<' not supported between instances of 'str' and 'int'` | `ValueError` |
### To Reproduce
Code sample
```python
from torchmetrics.image import PeakSignalNoiseRatioWithBlockedEffect
# All four should raise ValueError per the existing error message.
# Silently accepted (no error):
PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0, block_size=0)
PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0, block_size=-5)
PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0, block_size=1.5)
# Raises TypeError instead of ValueError:
PeakSignalNoiseRatioWithBlockedEffect(data_range=1.0, block_size="foo")
```
Environment
- TorchMetrics version: 1.9.0
- Python version: 3.12.13
- PyTorch version: 2.11.0
- OS: macOS 26.4.1
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read src/torchmetrics/image/psnrb.py at line 82 and run the reproduction cases from the issue for block_size values 0, -5, 1.5, and "foo". Done means each invalid value consistently raises ValueError with the intended message, without introducing regressions to PeakSignalNoiseRatioWithBlockedEffect.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- machine-learning
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100