diff --git a/.github/workflows/codespell-private.yml b/.github/workflows/codespell-private.yml index 350d2f8fc2..ff73f2560d 100644 --- a/.github/workflows/codespell-private.yml +++ b/.github/workflows/codespell-private.yml @@ -1,6 +1,7 @@ # GitHub Action to check our dictionary, this should only be used by the codespell project itself # For general usage in your repo, see the example in codespell.yml # https://github.com/codespell-project/codespell +# Concurrency cancels an action on a given PR once a new commit is pushed name: codespell Private Actions concurrency: group: ${{ github.workflow }}-${{ github.event.number }}-${{ github.event.ref }} diff --git a/README.rst b/README.rst index 058e565f5c..fb5702e262 100644 --- a/README.rst +++ b/README.rst @@ -103,19 +103,24 @@ be specified in this file (without the preceding dashes), for example:: count = quiet-level = 3 -This is equivalent to running:: - - codespell --quiet-level 3 --count --skip "*.po,*.ts,./src/3rdParty,./src/Test" - -Now codespell also support ``pyproject.toml``:: +Codespell will also check in the current directory for a ``pyproject.toml`` +(or a path can be specified via ``--toml ``) file, and the +``[tool.codespell]`` entry will be used as long as the tomli_ package +is installed, for example:: [tool.codespell] skip = '*.po,*.ts,./src/3rdParty,./src/Test' count = '' quiet-level = 3 +These are both equivalent to running:: + + codespell --quiet-level 3 --count --skip "*.po,*.ts,./src/3rdParty,./src/Test" + Any options specified in the command line will *override* options from the -config file. +config files. + +.. _tomli: https://pypi.org/project/tomli/ Dictionary format ----------------- diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index e2c73d3bcd..005e911043 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -407,14 +407,22 @@ def parse_options(args): config = configparser.ConfigParser() # Read toml before other config files. + toml_files_errors = list() + if os.path.isfile('pyproject.toml'): + toml_files_errors.append(('pyproject.toml', False)) if options.toml: + toml_files_errors.append((options.toml, True)) + for toml_file, raise_error in toml_files_errors: try: import tomli except Exception as exc: - raise ImportError( - f'tomli is required to read pyproject.toml but could not be ' - f'imported, got: {exc}') from None - with open(options.toml, 'rb') as f: + if raise_error: + raise ImportError( + f'tomli is required to read pyproject.toml but could not ' + f'be imported, got: {exc}') from None + else: + continue + with open(toml_file, 'rb') as f: data = tomli.load(f).get('tool', {}) config.read_dict(data) config.read(cfg_files) diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 5679f7fa32..282a748309 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -808,7 +808,7 @@ def test_config_toml(tmp_path, capsys, kind): assert 'bad.txt' in stdout if kind == 'cfg': - conffile = str(tmp_path / 'config.cfg') + conffile = str(tmp_path / 'setup.cfg') args = ('--config', conffile) with open(conffile, 'w') as f: f.write("""\ @@ -833,6 +833,16 @@ def test_config_toml(tmp_path, capsys, kind): assert code == 0 assert 'bad.txt' not in stdout + # And both should automatically work if they're in cwd + cwd = os.getcwd() + try: + os.chdir(tmp_path) + code, stdout, _ = cs.main(str(d), count=True, std=True) + finally: + os.chdir(cwd) + assert code == 0 + assert 'bad.txt' not in stdout + @contextlib.contextmanager def FakeStdin(text):