From 556e9a930eb8823c6829b081797ed2f83dfcbcac Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:14:44 +0100 Subject: [PATCH 1/3] Require Python 3.11 Python 3.10 reachaed EOL last October. --- .github/workflows/release.yml | 2 +- .github/workflows/tests.yml | 12 ++--------- .pre-commit-config.yaml | 5 +---- AGENTS.md | 2 +- README.rst | 11 +++------- codespell_lib/_codespell.py | 38 +++++++++++------------------------ pyproject.toml | 6 +----- tests/test_basic.py | 14 ++----------- 8 files changed, 23 insertions(+), 67 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index a9887ae828..b7d50281d9 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e943cadacc..43b869a275 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -20,8 +20,6 @@ jobs: fail-fast: false matrix: python-version: - - "3.9" - - "3.10" - "3.11" - "3.12" - "3.13" @@ -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 }}" @@ -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" diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index f2d0a4b58b..1e04b41c0e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 @@ -85,7 +83,6 @@ repos: args: ["--config-file", "pyproject.toml"] additional_dependencies: - pytest - - tomli - types-chardet ci: diff --git a/AGENTS.md b/AGENTS.md index 295160d0e5..23000fec26 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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. diff --git a/README.rst b/README.rst index ab8fe227cb..4c10e4fb1a 100644 --- a/README.rst +++ b/README.rst @@ -20,7 +20,7 @@ Useful links Requirements ------------ -Python 3.9 or above. +Python 3.11 or above. Installation ------------ @@ -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 @@ -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`` @@ -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 --------------------------- @@ -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 ----------------- diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index f5d9c7595d..798004d2c9 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -742,7 +742,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: @@ -752,32 +751,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 = [] diff --git a/pyproject.toml b/pyproject.toml index 97f150d48d..de908db637 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"}, @@ -29,9 +29,6 @@ dynamic = ["version"] hard-encoding-detection = [ "chardet" ] -toml = [ - "tomli; python_version < '3.11'" -] [dependency-groups] dev = [ @@ -43,7 +40,6 @@ dev = [ "pytest-dependency", "Pygments", "ruff", - "tomli" ] types = [ "chardet>=5.1.0", diff --git a/tests/test_basic.py b/tests/test_basic.py index b0abdf9d5c..c123bedc2e 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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 @@ -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": @@ -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") @@ -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") From beaf06a1b3db02c04cf112a49bd4a7a4bd637105 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:20:43 +0100 Subject: [PATCH 2/3] Apply ruff/pyupgrade rule UP007 Use `X | Y` for type annotations --- tests/test_basic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_basic.py b/tests/test_basic.py index c123bedc2e..c086b96aa8 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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, Optional from unittest import mock import pytest @@ -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) From 170dee795a556c5d33f6bddbb92f30e5644644e8 Mon Sep 17 00:00:00 2001 From: Dimitri Papadopoulos <3234522+DimitriPapadopoulos@users.noreply.github.com> Date: Wed, 25 Nov 2026 21:21:15 +0100 Subject: [PATCH 3/3] Apply ruff/pyupgrade rule UP045 Use `X | None` for type annotations --- codespell_lib/_codespell.py | 25 ++++++++++++------------- tests/test_basic.py | 8 ++++---- tests/test_dictionary.py | 10 +++++----- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 798004d2c9..265e28d295 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -33,7 +33,6 @@ from re import Match, Pattern from typing import ( Any, - Optional, TextIO, ) @@ -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: @@ -967,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) @@ -977,7 +976,7 @@ 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)) @@ -985,7 +984,7 @@ def extract_words( 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))) @@ -994,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]]: @@ -1041,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]]]: @@ -1059,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() @@ -1229,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 diff --git a/tests/test_basic.py b/tests/test_basic.py index c086b96aa8..a6349b2825 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -10,7 +10,7 @@ from io import StringIO from pathlib import Path from shutil import copyfile -from typing import Any, Optional +from typing import Any from unittest import mock import pytest @@ -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) @@ -1708,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 @@ -1802,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) diff --git a/tests/test_dictionary.py b/tests/test_dictionary.py index e105507d51..c4f1f94f2e 100644 --- a/tests/test_dictionary.py +++ b/tests/test_dictionary.py @@ -4,7 +4,7 @@ import pathlib import re from collections.abc import Iterable -from typing import Any, Optional +from typing import Any import pytest @@ -102,7 +102,7 @@ def test_dictionary_sorting(filename: pathlib.Path) -> None: def _check_aspell( phrase: str, msg: str, - in_aspell: Optional[bool], + in_aspell: bool | None, fname: str, languages: Iterable[str], ) -> None: @@ -138,7 +138,7 @@ def _check_aspell( def _check_err_rep( err: str, rep: str, - in_aspell: tuple[Optional[bool], Optional[bool]], + in_aspell: tuple[bool | None, bool | None], fname: str, languages: tuple[Iterable[str], Iterable[str]], ) -> None: @@ -264,8 +264,8 @@ def test_error_checking(err: str, rep: str, match: str) -> None: def test_error_checking_in_aspell( err: str, rep: str, - err_aspell: Optional[bool], - rep_aspell: Optional[bool], + err_aspell: bool | None, + rep_aspell: bool | None, match: str, ) -> None: """Test that our error checking works with aspell."""