Skip to main content

Linter for PyTorch Tensor operations

Project description


Pylint plugin to check for PyTorch tensor type annotations

Tested on Python 3.6+

Branch Status


pip install pylint pylint_tta

# Use pylint defaults with the plugin
pylint --load-plugins=pylint_tta <directory>

# Only use the plugin
pylint --load-plugins=pylint_tta --disable=all --enable=C9001,C9002,C9003,C9004,C9005,C9006 <directory>

What it checks for

Given file,

import astroid
import torch

def fail_func():
    a = torch.randn(5, 5, 5)

    b_fail = a.view(5, 25, -1)
    c_fail = a[:, : 1]
    c_fail: str = a[:, : 1]
    d_fail: str = a.unsqueeze(1)
    e_fail = a.unsqueeze_(1)

    return None

def pass_func():
    a = torch.randn(5, 5, 5)

    c_pass: ('w', 'h', 'b') = a[:, :, 1]
    d_pass: ('width', 'batch', 'height') = a.unsqueeze(1)

    return None

pylint-tta will yield:

************* Module example1 C9005: In-place operation detected (disallow-inplace-tensor-operations) C9001: Missing type annotations on tensor operation (missing-tensor-annotations) C9003: Missing type annotations on slicing (missing-slicing-annotations) C9004: Invalid type annotations on slicing (invalid-slicing-annotations) C9002: Non tuple type annotations on tensor operation (invalid-tensor-annotations) C9006: Assignment on in-place operation detected (assignment-on-inplace-operations)


Code Name
C9001 missing-tensor-annotations
C9002 invalid-tensor-annotations
C9003 missing-slicing-annotations
C9004 invalid-slicing-annotations
C9005 disallow-inplace-tensor-operations
C9006 assignment-on-inplace-operations


Run Example

export PYTHONPATH=`pwd`; pylint --load-plugins=pylint_tta --disable=all --enable=C9001,C9002,C9003,C9004


pytest -s

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pylint-tta, version 0.0.4a0
Filename, size File type Python version Upload date Hashes
Filename, size pylint_tta-0.0.4a0.tar.gz (3.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page