python / python/mypy

`--exclude` regular expression leading "/" does not match in current directory

Aperta
#19,012 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug topic-configuration
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

This might be a documentation error, but I expect that the documentation does reflect intended behavior so I am reporting it here.

Bug Report

The documentation includes the following example for --exclude

Similarly, you can ignore discovering directories with a given name by e.g. --exclude /build/ or those matching a subpath with --exclude /project/vendor/.

The implicature from the documentation (and just typical user expectation) is that --exclude /build/ would exclude ./build/b.py and ./some_dir/build/e.py, while all others would be checked. However ./build/b.py is not excluded. But /build/ and /project/ will not match (and therefore will not exclude) a build or project directory directly in the current directory.

Some examples will better illustrate.

To Reproduce

I created a directory structure

% tree .
.
├── a.py
├── build
│   └── b.py
├── extra_build
│   └── d.py
└── some_dir
    ├── build
    │   └── e.py
    └── c.py

The Python files are all identical and contain

from typing import TYPE_CHECKING

def main() -> None:
    v: str = "a literal string"
    if TYPE_CHECKING:
        reveal_type(v)
    else:
        print(f"Not type checking {__file__}")

if __name__ == "__main__":
    main()

Then run

 mypy --exclude '/build/' --config-file= .

in the directory that contains a.py.

Expected Behavior

a.py:7: note: Revealed type is "builtins.str"
some_dir/c.py:7: note: Revealed type is "builtins.str"
extra_build/d.py:7: note: Revealed type is "builtins.str"
Success: no issues found in 3 source files

That is, I would expect that build/b.py would not be type checked.

Actual Behavior

a.py:7: note: Revealed type is "builtins.str"
some_dir/c.py:7: note: Revealed type is "builtins.str"
extra_build/d.py:7: note: Revealed type is "builtins.str"
build/b.py:7: note: Revealed type is "builtins.str"
Success: no issues found in 4 source files

Note that build/b.py is erroneously type checked.

Your Environment

  • Mypy version used: mypy 1.14.1 (compiled: yes)
  • Mypy command-line flags: --exclude '/build/' --config-file= .
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.12.5
  • OS: macOS 15.4
  • Shell: zsh

Update: Now tested with mypy 1.15.0 and Python 3.13.3. The behavior remains as described above.

Obvious explanation

It appears that when the documentation was written, the expectation was that regular expression would have been tested against "./build/b.py'", which would match as expected. But instead it is being tested against "build/b.py`" which does not match the regular expression

Addition samples show what happens with different expressions

--exclude 'build' correctly matches (excludes) build/b.py, but incorrectly matches (excludes) extra_build/d.py

% mypy --exclude 'build/' --config-file= . 
a.py:7: note: Revealed type is "builtins.str"
some_dir/c.py:7: note: Revealed type is "builtins.str"

--exclude '\bbuild/' does match what is expected for the structure I gave,

mypy --exclude '\bbuild/' --config-file= .
a.py:7: note: Revealed type is "builtins.str"
some_dir/c.py:7: note: Revealed type is "builtins.str"
extra_build/d.py:7: note: Revealed type is "builtins.str"

But that would not help if I constructed a subdirectory with a name like foo+build.

Although it definitely would be possible to construct a regular expression that does the right thing, it is going to be far from obvious how to do so, and unless there is some RE magic that I don't know or don't recall, it is not going to be pretty.

I have not looked at the source, but I expect that it will be easier to just prepend the OS specific version of "`./'" to what is being matched.

Meta

I am sure that I am not the first person to wish that we could go back in time and use some other form of file globbing here instead of regular expressions. But we have the history we have.

Also, I do recognize that it is much better and easier to manage exclude patterns in configuration files instead of on the command line, but I did have reason to use the command line, and so I encountered this issue.

Guida per i contributori

Apri la guida per i contributori

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.

Direzione di ricerca

Inizia eseguendo il reproducer fornito di mypy 1.15.0 con --exclude '/build/' sull’albero delle directory mostrato, quindi traccia il punto di ingresso per il confronto dei percorsi di --exclude. Conferma come vengono costruiti i percorsi prima che venga applicata l’espressione regolare. Il lavoro è completato quando un pattern con una barra iniziale esclude build/b.py continuando a controllare gli altri tre file, con copertura del comportamento documentato.

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

Valutazione

Stack tecnologico
python
Ambito
cli
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.