Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

[tensor-shapes] torch.empty overloads omit requires_grad

Open Beginner friendly
#5,359 2 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 2 days

@Cha-Imaa is already working on this.

Since Oct 25, 2026.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
82/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, rust
Domain
tooling

Research direction

The issue points to both torch.empty overloads in tensor-shapes/pyrefly-torch-stubs/torch-stubs/__init__.pyi near line 2970. Read those signatures and find nearby tests for tensor shape stubs; use the supplied repro.py with pyrefly check repro.py to confirm the error. Done means both size forms accept requires_grad and retain their inferred shapes.

Written by the indexing model from the issue text.

Description

pytorch tensor-shapes
Describe the Bug

torch.empty accepts the requires_grad keyword for both tuple sizes and positional sizes.

The shape stubs reject the keyword in both forms. This also affects the explicit default value requires_grad=False.

Minimal reproduction

Save this code as repro.py. Create an empty pyrefly.toml next to it.

import torch

empty = torch.empty((0, 2), requires_grad=False)
trainable = torch.empty(2, 3, requires_grad=True)
assert empty.shape == (0, 2)
assert not empty.requires_grad
assert trainable.shape == (2, 3)
assert trainable.requires_grad

Run:

python repro.py
pyrefly check repro.py

The runtime assertions pass and the process exits with code 0. The type check reports the errors below.

Expected behavior

Accept requires_grad: bool = False in both overloads. Preserve the result shape (0, 2) or (2, 3).

Actual behavior
repro.py:3:20: No matching overload found for function `torch.empty` called with arguments: (tuple[Literal[0], Literal[2]], requires_grad=Literal[False])
  Possible overloads:
    (*size: *Shape, *, dtype: Any = None, device: Any = None) -> Tensor[Shape]
    (size: Shape, dtype: Any = None, device: Any = None) -> Tensor[Shape] [closest match]
  Unexpected keyword argument `requires_grad` in function `torch.empty` [no-matching-overload]

repro.py:4:24: No matching overload found for function `torch.empty` called with arguments: (Literal[2], Literal[3], requires_grad=Literal[True])
  Possible overloads:
    (*size: *Shape, *, dtype: Any = None, device: Any = None) -> Tensor[Shape] [closest match]
    (size: Shape, dtype: Any = None, device: Any = None) -> Tensor[Shape]
  Unexpected keyword argument `requires_grad` in function `torch.empty` [no-matching-overload]
Environment and current source
  • Python 3.13.13 on Linux.
  • Pyrefly 1.4.0.
  • pyrefly-torch-stubs==1.4.0.
  • PyTorch 2.13.0+cu126.

I also reproduced this with the stub sources from main at e5f077bcb2897dad3d168c45b9b64271fca94db6. The local Pyrefly binary reports pyrefly 1.4.0. I selected the checkout stub package directories with --site-packages-path. The diagnostics above use those current source files.

Current stub source. Both current empty overloads declare dtype and device, but omit requires_grad.

Dominant language
Rust
Stars
7.1k
Forks
564
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from facebook/pyrefly

All issues in facebook/pyrefly

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.