Skip to content

Add torch.nn.functional as F to flake8-import-conventions aliases #18310

Open
@IlanCosman

Description

@IlanCosman

Summary

Import style Search regex Number of files (links are slightly borked)
from torch.nn import functional /from torch\.nn import functional$/ 1.6k
from torch.nn import functional as <SOMETHING> /from torch\.nn import functional as .*$/ 162k
from torch.nn import functional as F /from torch\.nn import functional as F$/ 129k
import torch.nn.functional as <SOMETHING> /import torch\.nn\.functional as .*$/ 1.4M
import torch.nn.functional as F /import torch\.nn\.functional as F$/ 1.2M

In total 1.329 / 1.562 ~= 85% of the torch.nn.functional imports are as F.

Considerations

The F alias violates rule N812, lowercase-imported-as-non-lowercase.

  • This is unfortunate, but I think it's case where practicality beats purity, since the F alias is so ubiquitous in the PyTorch ecosystem.
  • I think it's worth making some sort of special case for this in rule N812
    • Quick and dirty option: add functional to the default ignore-names in [tool.ruff.lint.pep8-naming]
      • This has the slight disadvantage of allowing all import of functional, regardless of whether they are from PyTorch or not, to be importable with uppercase names.
    • Cleaner option: some sort of particular exception for torch.nn.functional? Not sure tbh

Metadata

Metadata

Assignees

No one assigned

    Labels

    configurationRelated to settings and configurationneeds-decisionAwaiting a decision from a maintainer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions