ruff: exclude pattern with `*` in file name incorrectly matches entire file path

ruff --exclude foo*.py

this incorrectly excludes the file foo/bar/baz.py.

since excludes takes globs (as mentioned here), i would expect it to only match python files with names starting with foo.

i’m on windows (which uses \ as path separators instead of /), that may be related

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 6
  • Comments: 15 (11 by maintainers)

Most upvoted comments

We have kind of a weird thing going on, which I think I followed from Flake8 or Black. When given a pattern like foo*.py, we match against both the basename and the absolute path (when testing to exclude a given file or directory).

So it may make sense to turn on literal_separator, since we’d still match *.py on the cases described above. Or we may want to reconsider our behavior here more holistically.

Came here because I was having an issue with the globs unexpectedly including subdirectories.

In my case, I am trying to exclude tests/*.py (which contains an old codebase which will be deprecated) while including tests/v3/*.py. Following the documentation:

  • Relative patterns, like directory/foo.py (to exclude that specific file) or directory/*.py (to exclude any Python files in directory). Note that these paths are relative to the project root (e.g., the directory containing your pyproject.toml).

I added:

[tool.ruff]
extend-exclude = ["tests/*.py"]

I was surprised to find out that this excluded all Python files in tests/ and subdirectories thereof. As pointed out above, it brings into question the purpose of having both * and **. Being a glob pattern, Ruff’s behaviour also doesn’t match a standard shell glob (e.g., ls tests/*.py does not list files in tests/v3/)

On mobile so apologies for short reply.

Globbing is pretty tricky because it depends on the context in which it is run. For example, at a shell, you might expect *.py to only match Python files in the current directory, but in a .gitignore file you would expect (and is what happens) *.py to exclude both foo.py and bar/foo.py. In the gitignore world, you would restrict your glob to the current directory (relative to the .gitignore file) by prefixing the glob with a /. So, /*.py would match foo.py but not bar/foo.py.

So whether *.py is supposed to match a path separator or not depends a lot on the context in which a glob is applied. But generally speaking, for exclude rules I would try to follow what gitignore does. (With that said, I haven’t looked at in detail the context here, so take what I’m saying with a grain of salt.)

Our plan for red knot is to have either an include and exclude (or both?) that support git patterns. Whether we bring this behavior to ruff depends on how it proves in practice and the timeline for using Red Knot’s infrastructure for Ruff.

I think @konstin and @MichaReiser have also been thinking about this recently

For visibility, I think my comment to my (closed) PR addressing this is quite relevant here:

While I am open to suggestions for this PR, I do think that it would be good to follow a widely accepted convention such as the gitignore specification. Specifically the following from the specification:

  • An asterisk * matches anything except a slash. The character ? matches any one character except /. The range notation, e.g. [a-zA-Z], can be used to match one of the characters in a range. See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description.

Two consecutive asterisks (**) in patterns matched against full pathname may have special meaning:

  • A leading ** followed by a slash means match in all directories. For example, **/foo matches file or directory foo anywhere, the same as pattern foo. **/foo/bar matches file or directory bar anywhere that is directly under directory foo.
  • A trailing /** matches everything inside. For example, abc/** matches all files inside directory abc, relative to the location of the .gitignore file, with infinite depth.
  • A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, a/**/b matches a/b, a/x/b, a/x/y/b and so on.
  • Other consecutive asterisks are considered regular asterisks and will match according to the previous rules.

Implementing the .gitignore logic as to the meaning of a leading forward slash, /, would be good as well to distinguish /*.py and *.py.

Is it currently possible to exclude python files in the root directory without excluding python files in every subdirectory?

The current approach seems different from both gitignore and standard globs, instead a third thing:

Globbing is pretty tricky because it depends on the context in which it is run. For example, at a shell, you might expect *.py to only match Python files in the current directory, but in a .gitignore file you would expect (and is what happens) *.py to exclude both foo.py and bar/foo.py. In the gitignore world, you would restrict your glob to the current directory (relative to the .gitignore file) by prefixing the glob with a /. So, /*.py would match foo.py but not bar/foo.py.

…in no case would we expect /*.py to match all python files — which IIUC is what ruff currently does?

Thanks! I’m impressed that you can manage code formatting on mobile.

I’m not sure about Android, but if you long-press the ' key on the standard iPhone keyboard it will give you a contextual menu of other apostrophe-like characters, including `.

On Android, the long press variations for ' do not include `, but you can instead do: ?123 -> =\< -> `

Thanks! I’m impressed that you can manage code formatting on mobile.