pytorch - 💡(How to fix) Fix nn.Sequential type hints too general for nested lists [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#181861Fetched 2026-04-30 06:18:10
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Participants

Error Message

mypy test.py test.py:4: error: "Tensor" not callable [operator] Found 1 error in 1 file (checked 1 source file) ty check test.py error[invalid-assignment]: Cannot assign to a subscript on an object of type Module --> test.py:4:1 | 4 | model[0][0] = nn.Conv2d(6, 3, 1) | ^^^^^^^^^^^ | info: Module does not have a __setitem__ method.

Found 1 diagnostic

Code Example

from torch import nn

model = nn.Sequential(nn.Sequential(nn.Conv2d(3, 3, 1)))

---

from torch import nn

model = nn.Sequential(nn.Sequential(nn.Conv2d(3, 3, 1)))
model[0][0] = nn.Conv2d(6, 3, 1)

---

> mypy test.py
test.py:4: error: "Tensor" not callable  [operator]
Found 1 error in 1 file (checked 1 source file)
> ty check test.py
error[invalid-assignment]: Cannot assign to a subscript on an object of type `Module`
 --> test.py:4:1
  |
4 | model[0][0] = nn.Conv2d(6, 3, 1)
  | ^^^^^^^^^^^
  |
info: `Module` does not have a `__setitem__` method.

Found 1 diagnostic
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

PyTorch models, including torchvision's SwinTransformer, often use nested nn.Sequential expressions like:

from torch import nn

model = nn.Sequential(nn.Sequential(nn.Conv2d(3, 3, 1)))

A user may reasonably want to later replace the first (for multispectral imagery) or last (for different # classes) layer of this model like so:

from torch import nn

model = nn.Sequential(nn.Sequential(nn.Conv2d(3, 3, 1)))
model[0][0] = nn.Conv2d(6, 3, 1)

While this code runs fine, type checkers like mypy and ty complain about it:

> mypy test.py
test.py:4: error: "Tensor" not callable  [operator]
Found 1 error in 1 file (checked 1 source file)
> ty check test.py
error[invalid-assignment]: Cannot assign to a subscript on an object of type `Module`
 --> test.py:4:1
  |
4 | model[0][0] = nn.Conv2d(6, 3, 1)
  | ^^^^^^^^^^^
  |
info: `Module` does not have a `__setitem__` method.

Found 1 diagnostic

While nn.Sequential is indeed an nn.Module, it is a specific nn.Module that allows subscripting.

Not 100% sure what the fix is. Perhaps containers like nn.Sequential should become generics?

Versions

Collecting environment information... PyTorch version: 2.11.0 Is debug build: False CUDA used to build PyTorch: None ROCM used to build PyTorch: N/A

OS: macOS 26.4.1 (arm64) GCC version: Could not collect Clang version: 18.1.8 CMake version: version 3.31.11 Libc version: N/A

Python version: 3.13.7 (main, Sep 18 2025, 22:52:34) [Clang 20.1.4 ] (64-bit runtime) Python platform: macOS-26.4.1-arm64-arm-64bit-Mach-O Is CUDA available: False CUDA runtime version: No CUDA CUDA_MODULE_LOADING set to: N/A GPU models and configuration: No CUDA Nvidia driver version: No CUDA cuDNN version: No CUDA Is XPU available: False HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True Caching allocator config: N/A

CPU: Apple M2 Pro

Versions of relevant libraries: [pip3] Could not collect [conda] Could not collect

extent analysis

TL;DR

The issue can be addressed by making nn.Sequential a generic class to support subscripting and type checking.

Guidance

  • The error messages from mypy and ty suggest that the issue is related to the type checker's inability to recognize nn.Sequential as a subscriptable object.
  • To resolve this, consider making nn.Sequential a generic class that can support subscripting and type checking.
  • Investigate the PyTorch documentation and source code to see if there are any existing solutions or workarounds for this issue.
  • If possible, test the code with a different version of PyTorch or mypy to see if the issue persists.

Example

No code example is provided as the issue is related to the internal implementation of nn.Sequential and requires changes to the PyTorch library.

Notes

The solution to this issue may require changes to the PyTorch library, which could be complex and time-consuming. It may be necessary to wait for an update to PyTorch or to use a workaround in the meantime.

Recommendation

Apply workaround: The issue is related to the type checker's inability to recognize nn.Sequential as a subscriptable object, and making changes to the PyTorch library may not be feasible. A workaround, such as using a different data structure or disabling type checking for specific lines of code, may be necessary until a permanent solution is available.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING