python / python/mypy

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

Abierto
#19,012 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

bug topic-configuration
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Merge medio
1 d 18 h
PR fusionados (30 d)
54

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza ejecutando el reproductor proporcionado de mypy 1.15.0 con --exclude '/build/' contra el árbol de directorios mostrado y, después, sigue el punto de entrada de coincidencia de rutas de --exclude. Confirma cómo se construyen las rutas antes de aplicar la expresión regular. Se considera completado cuando un patrón con una barra inicial excluye build/b.py y sigue comprobando los otros tres archivos, con cobertura del comportamiento documentado.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
cli
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.