8000 Typing: Incorrect overload for boolean operators · Issue #155701 · pytorch/pytorch · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Typing: Incorrect overload for boolean operators #155701
Closed
@randolf-scholz

Description

@randolf-scholz

🐛 Describe the bug

torch.Tensor has some faulty type annotations with respect to boolean operations.

import torch
from typing import assert_type

x = torch.tensor([0b0011])
assert_type(0b0101 & x, torch.Tensor)  # false positive
assert_type(x | (0+1j), torch.Tensor)  # false negative
$ mypy tmp.py
tmp.py:5:1: error: Expression is of type "Any", not "Tensor"  [assert-type]
tmp.py:5:22: error: Unsupported operand types for & ("int" and "Tensor")  [operator]
Found 2 errors in 1 file (checked 1 source file)

The current overloads are:

@overload
def __or__(self, other: Tensor) -> Tensor: ...
@overload
def __or__(self, other: Number | _complex) -> Tensor: ...
@overload
def __or__(self, other: Tensor | _bool) -> Tensor: ...

def __ror__(self, other: Tensor | _bool) -> Tensor: ...

They probably should be simply:

def __or__(self, other: Tensor | int) -> Tensor: ...
def __ror__(self, other: Tensor | int) -> Tensor: ...

Versions

Collecting environment information...
PyTorch version: 2.8.0a0+git013cf1e
Is debug build: False
CUDA used to build PyTorch: 12.9
ROCM used to build PyTorch: N/A

OS: Ubuntu 24.04.2 LTS (x86_64)
GCC version: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
Clang version: 18.1.3 (1ubuntu1)
CMake version: version 4.0.2
Libc version: glibc-2.39

Python version: 3.9.23 (main, Jun  4 2025, 08:55:39)  [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.11.0-26-generic-x86_64-with-glibc2.39
Is CUDA available: True
CUDA runtime version: 12.9.86
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090
Nvidia driver version: 575.57.08
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.9.10.2
/usr/lib/x86_64-linux-gnu/libcudnn_adv.so.9.10.2
/usr/lib/x86_64-linux-gnu/libcudnn_cnn.so.9.10.2
/usr/lib/x86_64-linux-gnu/libcudnn_engines_precompiled.so.9.10.2
/usr/lib/x86_64-linux-gnu/libcudnn_engines_runtime_compiled.so.9.10.2
/usr/lib/x86_64-linux-gnu/libcudnn_graph.so.9.10.2
/usr/lib/x86_64-linux-gnu/libcudnn_heuristic.so.9.10.2
/usr/lib/x86_64-linux-gnu/libcudnn_ops.so.9.10.2
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                         x86_64
CPU op-mode(s):                       32-bit, 64-bit
Address sizes:                        46 bits physical, 48 bits virtual
Byte Order:                           Little Endian
CPU(s):                               20
On-line CPU(s) list:                  0-19
Vendor ID:                            GenuineIntel
Model name:                           Intel(R) Core(TM) Ultra 7 265K
CPU family:                           6
Model:                                198
Thread(s) per core:                   1
Core(s) per socket:                   20
Socket(s):                            1
Stepping:                             2
CPU(s) scaling MHz:                   32%
CPU max MHz:                          7000,0000
CPU min MHz:                          800,0000
BogoMIPS:                             7756,80
Flags:                                fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault intel_ppin ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdt_a rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect user_shstk avx_vnni lam wbnoinvd dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi vnmi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid bus_lock_detect movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
Virtualization:                       VT-x
L1d cache:                            704 KiB (18 instances)
L1i cache:                            1,1 MiB (18 instances)
L2 cache:                             36 MiB (11 instances)
L3 cache:                             30 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-19
Vulnerability Gather data sampling:   Not affected
Vulnerability Itlb multihit:          Not affected
Vulnerability L1tf:                   Not affected
Vulnerability Mds:                    Not affected
Vulnerability Meltdown:               Not affected
Vulnerability Mmio stale data:        Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   Not affected
Vulnerability Spec store bypass:      Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:             Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:             Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] flake8==6.1.0
[pip3] flake8-bugbear==23.3.23
[pip3] flake8-comprehensions==3.15.0
[pip3] flake8-executable==2.1.3
[pip3] flake8-logging-format==0.9.0
[pip3] flake8-pyi==23.3.1
[pip3] flake8-simplify==0.19.3
[pip3] mypy==1.15.0
[pip3] mypy_extensions==1.1.0
[pip3] numpy==1.26.4
[pip3] optree==0.13.0
[pip3] pytorch_sphinx_theme2==0.1.0
[pip3] torch==2.8.0a0+git013cf1e
[conda] Could not collect

cc @ezyang @malfet @xuzhao9 @gramster

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: typingRelated to mypy type annotationstriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0