mdformat icon indicating copy to clipboard operation
mdformat copied to clipboard

`--exclude` on `python < 3.13`

Open jorenham opened this issue 1 year ago • 4 comments

Context

#451 introduced the --exclude option to specify which files to exclude from formatting using globs. This is an essential feature for anyone that uses things like submodules, vendors 3rd party libs, or uses in-project virtual environments, which includes e.g. every uv user. However, this can only be used on python >= 3.13. Attempting to do so on other Python versions will result in a error, and will prevent using mdformat altogether. In practice, many projects support multiple python versions, and for reasons of compatibility use the lowest supported version for development and testing, and can therefore not use --exclude.

Proposal

Make --exclude available on python<3.13 as well. Something like glob.translate could be used for this.

jorenham avatar Dec 08 '24 11:12 jorenham

Thanks for the issue!

glob.translate was added in Python 3.13, so isn't useful here.

A PR is welcome, if there is a pathlib.PurePath.full_match backport on PyPI, or other library that can mimic its behavior.

hukkin avatar Dec 09 '24 08:12 hukkin

Not sure if it works for you, but using a tool like pre-commit will allow excluding files on older Python versions (and comes with other benefits too).

hukkin avatar Dec 09 '24 08:12 hukkin

yea in scipy-stubs we're also running in the pre-commit, but I'm not the biggest fan of it, and don't want to make the CI fully dependent on it

jorenham avatar Dec 09 '24 11:12 jorenham

I had the same issue today, so I had a quick look at what it would take to support --exclude on python < 3.13. Using glob.translate isn't possible since it was also added in 3.13. So it seems like Path.full_match would need to be re-implemented to some extent. It would probably take around 100 lines of code, judging from the cpython glob module or this backport of full_match.

hoel-bagard avatar May 20 '25 12:05 hoel-bagard