Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v7
with:
python-version: "3.10"
python-version: "3.12"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
Expand Down
12 changes: 2 additions & 10 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ jobs:
fail-fast: false
matrix:
python-version:
- "3.9"
- "3.10"
- "3.11"
- "3.12"
- "3.13"
Expand All @@ -32,13 +30,10 @@ jobs:
os:
- "ubuntu-24.04-arm"
include:
- python-version: "3.10"
- python-version: "3.11"
kind: "old"
os: "ubuntu-24.04-arm"
- python-version: "3.10"
kind: "no-toml"
os: "ubuntu-24.04-arm"
- python-version: "3.13"
- python-version: "3.14"
kind: "standard"
os: "windows-latest"
name: "${{ matrix.python-version }} ${{ matrix.kind }} ${{ matrix.os }}"
Expand Down Expand Up @@ -72,9 +67,6 @@ jobs:
- uses: codecov/codecov-action@v7
with:
token: ${{ secrets.CODECOV_TOKEN }}
# tomli should not be required for the next two steps (and make sure it's not)
- run: pip uninstall -yq tomli
if: ${{ matrix.kind == 'no-toml' }}
- run: codespell --check-filenames --skip="./.*,./build/*,./codespell_lib/data/*,./tests/test_basic.py,./example/code.c,./junit-results.xml,*.egg-info/*,*.pyc,*.sig,pyproject-codespell.precommit-toml,README.rst,"
# this file has an error
- run: "! codespell tests/test_basic.py"
Expand Down
5 changes: 1 addition & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ repos:
hooks:
- id: codespell
args: [--toml, pyproject-codespell.precommit-toml]
additional_dependencies:
- tomli
- repo: https://github.com/abravalheri/validate-pyproject
rev: "v0.27"
rev: v0.27
hooks:
- id: validate-pyproject
- repo: https://github.com/pre-commit/mirrors-mypy
Expand All @@ -85,7 +83,6 @@ repos:
args: ["--config-file", "pyproject.toml"]
additional_dependencies:
- pytest
- tomli
- types-chardet

ci:
Expand Down
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ make check # everything CI checks: dictionaries, build, pytest, ru

## Project layout

Pure Python (>= 3.9), no runtime dependencies.
Pure Python (>= 3.11), no runtime dependencies.

- `codespell_lib/_codespell.py`: the CLI (`main()`), option parsing, and
file checking/fixing.
Expand Down
11 changes: 3 additions & 8 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Useful links
Requirements
------------

Python 3.9 or above.
Python 3.11 or above.

Installation
------------
Expand Down Expand Up @@ -227,8 +227,7 @@ comments are possible using ``;`` or ``#`` as the first character.

Codespell will also check in the current directory for a ``pyproject.toml``
file (or a file specified via ``--toml``), and the ``[tool.codespell]``
entry will be used. For versions of Python prior to 3.11, this requires
the tomli_ package. For example, here is the TOML equivalent of the
entry will be used. For example, here is the TOML equivalent of the
previous config file:

.. code-block:: toml
Expand All @@ -246,7 +245,7 @@ The above INI and TOML files are equivalent to running:

If several config files are present, they are read in the following order:

#. ``pyproject.toml`` (only if the ``tomli`` library is available for Python < 3.11)
#. ``pyproject.toml``
#. ``setup.cfg``
#. ``.codespellrc``
#. any additional file supplied via ``--config``
Expand Down Expand Up @@ -275,8 +274,6 @@ instead of these invalid entries:
dictionary = -,mydict
ignore-words-list = -foo,bar

.. _tomli: https://pypi.org/project/tomli/

Reading arguments from file
---------------------------

Expand Down Expand Up @@ -304,8 +301,6 @@ If one configures codespell using the `pyproject.toml` file instead use:
rev: v2.4.3
hooks:
- id: codespell
additional_dependencies:
- tomli

Dictionary format
-----------------
Expand Down
63 changes: 24 additions & 39 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
from re import Match, Pattern
from typing import (
Any,
Optional,
TextIO,
)

Expand Down Expand Up @@ -258,7 +257,7 @@ def __init__(
self,
use_chardet: bool,
quiet_level: int,
ignore_multiline_regex: Optional[Pattern[str]],
ignore_multiline_regex: Pattern[str] | None,
) -> None:
self.use_chardet = use_chardet
if use_chardet:
Expand Down Expand Up @@ -742,7 +741,6 @@ def convert_arg_line_to_args(self, arg_line: str) -> list[str]:
# Read toml before other config files.
toml_files = []
used_toml_files = []
tomllib_raise_error = False
if os.path.isfile("pyproject.toml"):
toml_files.append("pyproject.toml")
if options.toml:
Expand All @@ -752,32 +750,19 @@ def convert_arg_line_to_args(self, arg_line: str) -> list[str]:
f"ERROR: cannot find toml file: {options.toml}",
)
toml_files.append(options.toml)
tomllib_raise_error = True
if toml_files:
if sys.version_info >= (3, 11):
import tomllib
else:
try:
import tomli as tomllib # type: ignore[no-redef]
except ImportError as e:
if tomllib_raise_error:
msg = (
f"tomllib or tomli are required to read pyproject.toml "
f"but could not be imported, got: {e}"
)
raise ImportError(msg) from None
tomllib = None # type: ignore[assignment]
if tomllib is not None:
for toml_file in toml_files:
with open(toml_file, "rb") as f:
data = tomllib.load(f).get("tool", {}).get("codespell")
if data is not None:
if not isinstance(data, dict):
msg = f"{toml_file}: [tool.codespell] must be a table"
raise configparser.Error(msg)
config.read_dict({"codespell": _toml_to_parseconfig(data)})
if toml_file not in used_toml_files:
used_toml_files.append(toml_file)
import tomllib

for toml_file in toml_files:
with open(toml_file, "rb") as f:
data = tomllib.load(f).get("tool", {}).get("codespell")
if data is not None:
if not isinstance(data, dict):
msg = f"{toml_file}: [tool.codespell] must be a table"
raise configparser.Error(msg)
config.read_dict({"codespell": _toml_to_parseconfig(data)})
if toml_file not in used_toml_files:
used_toml_files.append(toml_file)

# Collect which config files are going to be used
used_ini_files = []
Expand Down Expand Up @@ -981,7 +966,7 @@ def print_context(

def _ignore_word_sub(
text: str,
ignore_word_regex: Optional[Pattern[str]],
ignore_word_regex: Pattern[str] | None,
) -> str:
if ignore_word_regex:
text = ignore_word_regex.sub(" ", text)
Expand All @@ -991,15 +976,15 @@ def _ignore_word_sub(
def extract_words(
text: str,
word_regex: Pattern[str],
ignore_word_regex: Optional[Pattern[str]],
ignore_word_regex: Pattern[str] | None,
) -> list[str]:
return word_regex.findall(_ignore_word_sub(text, ignore_word_regex))


def extract_words_iter(
text: str,
word_regex: Pattern[str],
ignore_word_regex: Optional[Pattern[str]],
ignore_word_regex: Pattern[str] | None,
) -> list[Match[str]]:
return list(word_regex.finditer(_ignore_word_sub(text, ignore_word_regex)))

Expand All @@ -1008,7 +993,7 @@ def apply_uri_ignore_words(
check_matches: list[Match[str]],
line: str,
word_regex: Pattern[str],
ignore_word_regex: Optional[Pattern[str]],
ignore_word_regex: Pattern[str] | None,
uri_regex: Pattern[str],
uri_ignore_words: set[str],
) -> list[Match[str]]:
Expand Down Expand Up @@ -1055,15 +1040,15 @@ def parse_lines(
fragment: tuple[bool, int, list[str]],
filename: str,
colors: TermColors,
summary: Optional[Summary],
summary: Summary | None,
misspellings: dict[str, Misspelling],
ignore_words_cased: set[str],
exclude_lines: set[str],
word_regex: Pattern[str],
ignore_word_regex: Optional[Pattern[str]],
ignore_word_regex: Pattern[str] | None,
uri_regex: Pattern[str],
uri_ignore_words: set[str],
context: Optional[tuple[int, int]],
context: tuple[int, int] | None,
options: argparse.Namespace,
asked_for: dict[str, tuple[bool, str]],
) -> tuple[int, bool, list[tuple[int, str, str]]]:
Expand All @@ -1073,7 +1058,7 @@ def parse_lines(

_, fragment_line_number, lines = fragment

next_line_ignore_words: Optional[set[str]] = None
next_line_ignore_words: set[str] | None = None

for i, line in enumerate(lines):
line = line.rstrip()
Expand Down Expand Up @@ -1243,16 +1228,16 @@ def parse_lines(
def parse_file(
filename: str,
colors: TermColors,
summary: Optional[Summary],
summary: Summary | None,
misspellings: dict[str, Misspelling],
ignore_words_cased: set[str],
exclude_lines: set[str],
file_opener: FileOpener,
word_regex: Pattern[str],
ignore_word_regex: Optional[Pattern[str]],
ignore_word_regex: Pattern[str] | None,
uri_regex: Pattern[str],
uri_ignore_words: set[str],
context: Optional[tuple[int, int]],
context: tuple[int, int] | None,
options: argparse.Namespace,
) -> tuple[int, bool]:
bad_count = 0
Expand Down
6 changes: 1 addition & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ build-backend = "flit_scm:buildapi"
name = "codespell"
description = "Fix common misspellings in text files"
readme = { file = "README.rst", content-type = "text/x-rst" }
requires-python = ">=3.9"
requires-python = ">=3.11"
license = "GPL-2.0-only"
authors = [
{name = "Lucas De Marchi", email = "lucas.de.marchi@gmail.com"},
Expand All @@ -29,9 +29,6 @@ dynamic = ["version"]
hard-encoding-detection = [
"chardet"
]
toml = [
"tomli; python_version < '3.11'"
]

[dependency-groups]
dev = [
Expand All @@ -43,7 +40,6 @@ dev = [
"pytest-dependency",
"Pygments",
"ruff",
"tomli"
]
types = [
"chardet>=5.1.0",
Expand Down
24 changes: 7 additions & 17 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from io import StringIO
from pathlib import Path
from shutil import copyfile
from typing import Any, Optional, Union
from typing import Any
from unittest import mock

import pytest
Expand Down Expand Up @@ -42,7 +42,7 @@ def main(
*args: Any,
count: bool = True,
std: bool = False,
) -> Union[int, tuple[int, str, str]]:
) -> int | tuple[int, str, str]:
args = tuple(str(arg) for arg in args)
if count:
args = ("--count", *args)
Expand All @@ -69,7 +69,7 @@ def main(

def run_codespell(
args: tuple[Any, ...] = (),
cwd: Optional[Path] = None,
cwd: Path | None = None,
) -> int:
"""Run codespell."""
args = tuple(str(arg) for arg in args)
Expand Down Expand Up @@ -302,12 +302,8 @@ def test_bad_glob(
# bad glob is invalid
result = cs.main("--skip", "[b-a].txt", g, std=True)
assert isinstance(result, tuple)
code, _, stderr = result
if sys.hexversion < 0x030A05F0: # Python < 3.10.5 raises re.error
assert code == EX_USAGE, "invalid glob"
assert "invalid glob" in stderr
else: # Python >= 3.10.5 does not match
assert code == 1
code, _, _ = result
assert code == 1
# properly escaped glob is valid, and matches glob-like file name
assert cs.main("--skip", "[[]b-a[]].txt", g) == 0

Expand Down Expand Up @@ -1595,8 +1591,6 @@ def test_config_toml(
"""
conffile.write_text(text)
else:
if sys.version_info < (3, 11):
pytest.importorskip("tomli")
tomlfile = tmp_path / "pyproject.toml"
args = ("--toml", tomlfile)
if kind == "toml":
Expand Down Expand Up @@ -1652,8 +1646,6 @@ def test_config_toml_codespell_array(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
if sys.version_info < (3, 11):
pytest.importorskip("tomli")
tomlfile = tmp_path / "pyproject.toml"
tomlfile.write_text("[[tool.codespell]]\nskip = 'bad.txt'\n")

Expand All @@ -1668,8 +1660,6 @@ def test_warn_both_pyproject_and_codespellrc(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
if sys.version_info < (3, 11):
pytest.importorskip("tomli")
(tmp_path / "pyproject.toml").write_text("[tool.codespell]\nquiet-level = 2\n")
(tmp_path / ".codespellrc").write_text("[codespell]\nquiet-level = 2\n")
(tmp_path / "ok.txt").write_text("ok\n")
Expand Down Expand Up @@ -1718,7 +1708,7 @@ def FakeStdin(text: str) -> Generator[None, None, None]:
def run_codespell_stdin(
text: str,
args: tuple[Any, ...],
cwd: Optional[Path] = None,
cwd: Path | None = None,
) -> int:
"""Run codespell in stdin mode and return number of lines in output."""
proc = subprocess.run( # noqa: S603
Expand Down Expand Up @@ -1812,7 +1802,7 @@ def test_args_from_file(
def run_codespell_interactive(
args: tuple[Any, ...],
answers: str,
cwd: Optional[Path] = None,
cwd: Path | None = None,
) -> "subprocess.CompletedProcess[str]":
"""Run codespell feeding interactive answers on stdin."""
args = tuple(str(arg) for arg in args)
Expand Down
Loading
Loading