[tensor-shapes] Missing scalar divisor overloads for torch.fmod and Tensor.fmod

Aperta Adatta ai principianti
#5,711 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
70/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
devtools

Direzione di ricerca

Riproduci prima con il repro.py fornito e il comando di controllo di pyrefly; il problema è in pyrefly-torch-stubs, non in pyrefly stesso, quindi individua dove sono dichiarati torch.fmod e Tensor.fmod e la firma che accetta solo un divisore tensore. Aggiungi un overload scalare per ciascuno che restituisca la forma dell'input S, poi riesegui il repro per confermare che entrambe le chiamate passano mentre il comportamento con divisore tensore resta invariato. È fatto quando i due errori segnalati scompaiono e le chiamate esistenti con broadcasting continuano a passare il controllo dei tipi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

pytorch tensor-shapes typechecking
Describe the Bug

With pyrefly-torch-stubs==1.5.0, passing a Python float divisor to either torch.fmod(x, divisor) or x.fmod(divisor) produces a false positive (bad-argument-type). PyTorch documents a scalar divisor and gives an example with a Python float. Both calls should type-check and preserve the input tensor shape S. PyTorch fmod documentation

Reproduction

Save this as repro.py in an empty directory.

from __future__ import annotations

from typing import TYPE_CHECKING

import torch

if TYPE_CHECKING:
    from shape_extensions import IntTuple
    from torch import Tensor


def functional[S: IntTuple](x: Tensor[S], divisor: float) -> Tensor[S]:
    return torch.fmod(x, divisor)


def method[S: IntTuple](x: Tensor[S], divisor: float) -> Tensor[S]:
    return x.fmod(divisor)

An empty Pyrefly configuration is sufficient, then run thuis.

touch pyrefly.toml
uv venv --python 3.13.18 .venv
uv pip install --python .venv/bin/python --index https://download.pytorch.org/whl/cpu 'torch==2.14.0+cpu'
uv pip install --python .venv/bin/python pyrefly==1.5.0 pyrefly-torch-stubs==1.5.0
.venv/bin/pyrefly check --python-interpreter-path .venv/bin/python --output-format min-text repro.py

Pyrefly exits with status 1 and underlines the divisor in both calls.

ERROR repro.py:13:26-33: Argument `float` is not assignable to parameter `other` with type `Tensor[@_]` in function `torch.fmod` [bad-argument-type]
ERROR repro.py:17:19-26: Argument `float` is not assignable to parameter `other` with type `Tensor[@_]` in function `torch.Tensor.fmod` [bad-argument-type]

Both functions run with the same float argument.

.venv/bin/python - <<'PY'
import torch
from repro import functional, method

x = torch.tensor([-0.25, 1.25])
print(functional(x, 0.5).tolist())
print(method(x, 0.5).tolist())
PY

Both print [-0.25, 0.25]. Changing just the two divisor annotations to Tensor[[]] makes the type check pass. The current shape stubs declare a tensor divisor in both places. A scalar overload returning the input shape would cover these calls while preserving the existing broadcasting behavior for tensor divisors.

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.zVbfi9QwEP5XgvfSQs0p4ktBEZZDFzk93H2RWko2TbfFbrc0qe4p_u_OJGnS9FbvBB-Oe7huMpmf33wzPz1lDqIfjjPCLIpqBARgRabGRteYbWRLDSGtbj_fXBWrd1er9-sPb2eUAPzCa1vnQMYmVKuSNetFIU5KdJhARzXrTm1H6NiZqNbnjIK8gV_YQKzNNql7nUen1IpmmzwhZfOtge-UVECjpjnd7VR70wHaGK0O0NQn9y729kzD_x9bJ2PHG3kozF5YVBhn-9sGJC_I6giPm32tSMRjci0UIzctU9BBByDudccpFLUkrAIUNAwwQ8n1egusycE9Qb90F-STKEcuSpv3WpB-3AGr1nD0nL6kz0zZoCX4V7aH-YDdqWpgIYlQ4UQYuqUWL_cVOYFGhbGGQw7zi1ONFBtt4tUMCYbKpyTiderFzEc-Y3hMavYRvB8C2TwKSR3GKsq42vkXuRGcl875GWkJmP5OOs5TQil1gETr1rCPc-6OB45l864fVRrEl__VuX91DJCyrMTljEMeK3qoWz9ki0xdwH8_2acs2hjDQxmUxASvBtZJDCfb5lHVQWpNHp3om4WcebwfkWIKn2fZC653j8Qolr6iUmt0npnyQq_C3-p4gCoLnZbjqODbZqMUkg_NDnK0uyWM7HEXZW3zAw60ZSJhWDDkZmo0TeuHJRHMCi3Oejk5aOnrfHz-SSsqNcesRoM_OBcbWtGkFMVJFD99DTP7ib3RlrE_0T-XoEhbsbrjOIjkvhjOwRiVP14E_2Ezn_bxJc6nLGmwTz82vBYHlniMBbfSXN9tCxktZeJgCe7Ed9iBeSvtCowotm5lC9u5rvxSXdBhxUO6ZKkhIZeh5kDxnU3GrSy_fgM

(Only applicable for extension issues) IDE Information

No response

Lingua principale
Rust
Stelle
7.2k
Fork
578
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di facebook/pyrefly

Tutte le issue di facebook/pyrefly

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.