hhatto / autopep8

A tool that automatically formats Python code to conform to the PEP 8 style guide.
https://pypi.org/project/autopep8/
MIT License
4.56k stars 290 forks source link

Add --exclude-base-dir, defaults to the pyproject.toml directory #631

Closed ideasman42 closed 1 year ago

ideasman42 commented 2 years ago

Add --exclude-base-dir which defaults to the location of pyproject.toml so that exclude paths are always made relative to this path.


For some context:

Currently there is a problem where the exclude directories listed in pyproject.toml are compared against paths which depend on the command line input given.

I ran into this when attempting to use autopep8 to format all source files in a project and noticed that:

autopep8 .

Worked, while:

autopep8 $PWD

Failed to exclude directories.

This isn't a problem when executing from the command line however IDE's may pass in full paths which then fail to match the paths in the exclude list. Checking vscode, it's passing full paths to autopep8 by default (for example).

The problem is that when the full directory is passed in, this then expands to an absolute path that doesn't match the paths in the pyproject.toml which are relative to that file

This PR sets exclude-base-dir to the base directory of the pyproject.toml, so exclude checks first make paths relative to this directory before matching. The "." is added at the beginning of paths not to break compatibility with anyone already depending on the current behavior of autopep8 ..

Note that I'm not so keen on exposing --exclude-base-dir as a user visible argument, this could be a global instead.