Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

# flake8: noqa inside a string literal skips the whole file

Chiusa
#2,061 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
tooling

Direzione di ricerca

Start in FileProcessor.should_ignore_file and inspect how defaults.NOQA_FILE is matched against raw source lines; compare this with tokenize.COMMENT handling and the related Violation.is_inline_ignored path. Reproduce the fn.py example, then verify that a directive inside a docstring no longer skips the file while real F401, E201, and E225 violations are still reported.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

# flake8: noqa inside a string literal causes the whole file to be skipped, so a file with real violations is reported clean and flake8 exits 0.

Steps to reproduce

Save as fn.py:

"""Our lint configuration for this module.

Reproduce with:
    flake8 --isolated
Suppress with:
    # flake8: noqa
"""

import os


def f( ):
    x=1
    return os, x
$ flake8 fn.py; echo "exit=$?"
exit=0
$ flake8 --disable-noqa fn.py
fn.py:12:1: F401 'os' imported but unused
...
exit=1

The real F401, E201 and E225 are all there — a # noqa: F401 on a code line, or a comment elsewhere in the file, does not suppress them. Only this one, which is string content.

It also happens with a docstring on a nested function, and with any triple-quoted string containing a line that starts with the directive.

Why this matters more than a normal false positive

The file reports clean and the exit status is 0, so CI passes on a file that has violations. The E/W/F/C checks never get a chance to run — the decision is made before any plugin sees the file.

Cause

FileProcessor.should_ignore_file regex-matches defaults.NOQA_FILE against the raw source lines, so it cannot tell a comment from string content. The docstring above is the most natural place to write down a file's own lint configuration, and the directive ends up on a line by itself inside it.

This is a residual of #873 ("Only skip a file if # flake8: noqa is on a line by itself"). That change switched .search to .match, which fixed the trailing-comment case but not a standalone line inside a docstring.

Suggested fix

Match only lines that carry a tokenize.COMMENT token, falling back to all lines when the file cannot be tokenized. This is the same remedy asottile described for the inline directive in #1321 — "I think the regex is still relevant but it should only be tested against COMMENT tokens" — applied to the file-level directive, which is a different function (FileProcessor.should_ignore_file, not Violation.is_inline_ignored).

If you would rather do the tokenization rework for both at once, this is happy to be folded into that.

Related, separate

defaults.NOQA_FILE is re.compile(r"\s*# flake8[:=]\s*noqa", re.I) with no end anchor, so a line reading # flake8: noqaX also skips the whole file. That is a different question and not claimed here.

Lingua principale
Python
Stelle
3.8k
Fork
363
Merge medio
3h 59m
PR unite (30g)
11

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di PyCQA/flake8

Tutte le issue di PyCQA/flake8

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.