From c6c85ebe8e5b1cd200636d30f94c630adf32c6fb Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 24 Jul 2024 05:10:32 +0200 Subject: Merging upstream version 4.6.0+dfsg. Signed-off-by: Daniel Baumann --- .pre-commit-config.yaml | 10 +++++----- .pre-commit-hooks.yaml | 2 +- CHANGELOG.md | 14 ++++++++++++++ README.md | 5 ++++- pre_commit_hooks/fix_encoding_pragma.py | 8 ++++++++ pre_commit_hooks/requirements_txt_fixer.py | 11 ++++++++++- setup.cfg | 2 +- tests/requirements_txt_fixer_test.py | 6 ++++++ 8 files changed, 49 insertions(+), 9 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d9ffea2..937696b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v4.6.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -23,20 +23,20 @@ repos: hooks: - id: add-trailing-comma - repo: https://github.com/asottile/pyupgrade - rev: v3.14.0 + rev: v3.15.2 hooks: - id: pyupgrade args: [--py38-plus] - repo: https://github.com/hhatto/autopep8 - rev: v2.0.4 + rev: v2.1.0 hooks: - id: autopep8 - repo: https://github.com/PyCQA/flake8 - rev: 6.1.0 + rev: 7.0.0 hooks: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.5.1 + rev: v1.9.0 hooks: - id: mypy additional_dependencies: [types-all] diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index c0d811c..4b4d0cf 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -145,7 +145,7 @@ language: python types: [text] - id: fix-encoding-pragma - name: fix python encoding pragma + name: fix python encoding pragma (deprecated) description: 'adds # -*- coding: utf-8 -*- to the top of python files.' language: python entry: fix-encoding-pragma diff --git a/CHANGELOG.md b/CHANGELOG.md index c1daaba..bf99d9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ +4.6.0 - 2024-04-06 +================== + +### Features +- `requirements-txt-fixer`: remove duplicate packages. + - #1014 PR by @vhoulbreque-withings. + - #960 issue @csibe17. + +### Migrating +- `fix-encoding-pragma`: deprecated -- will be removed in 5.0.0. use + [pyupgrade](https://github.com/asottile/pyupgrade) or some other tool. + - #1033 PR by @mxr. + - #1032 issue by @mxr. + 4.5.0 - 2023-10-07 ================== diff --git a/README.md b/README.md index 9ae7ec5..4992baf 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Add this to your `.pre-commit-config.yaml` ```yaml - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 # Use the ref you want to point at + rev: v4.6.0 # Use the ref you want to point at hooks: - id: trailing-whitespace # - id: ... @@ -127,6 +127,9 @@ The following arguments are available: removes UTF-8 byte order marker #### `fix-encoding-pragma` + +_Deprecated since py2 is EOL - use [pyupgrade](https://github.com/asottile/pyupgrade) instead._ + Add `# -*- coding: utf-8 -*-` to the top of python files. - To remove the coding pragma pass `--remove` (useful in a python3-only codebase) diff --git a/pre_commit_hooks/fix_encoding_pragma.py b/pre_commit_hooks/fix_encoding_pragma.py index 60c71ee..eee6705 100644 --- a/pre_commit_hooks/fix_encoding_pragma.py +++ b/pre_commit_hooks/fix_encoding_pragma.py @@ -1,6 +1,7 @@ from __future__ import annotations import argparse +import sys from typing import IO from typing import NamedTuple from typing import Sequence @@ -107,6 +108,13 @@ def _normalize_pragma(pragma: str) -> bytes: def main(argv: Sequence[str] | None = None) -> int: + print( + 'warning: this hook is deprecated and will be removed in a future ' + 'release because py2 is EOL. instead, use ' + 'https://github.com/asottile/pyupgrade', + file=sys.stderr, + ) + parser = argparse.ArgumentParser( 'Fixes the encoding pragma of python files', ) diff --git a/pre_commit_hooks/requirements_txt_fixer.py b/pre_commit_hooks/requirements_txt_fixer.py index 5884394..261acc9 100644 --- a/pre_commit_hooks/requirements_txt_fixer.py +++ b/pre_commit_hooks/requirements_txt_fixer.py @@ -45,6 +45,11 @@ class Requirement: elif requirement.value == b'\n': return False else: + # if 2 requirements have the same name, the one with comments + # needs to go first (so that when removing duplicates, the one + # with comments is kept) + if self.name == requirement.name: + return bool(self.comments) > bool(requirement.comments) return self.name < requirement.name def is_complete(self) -> bool: @@ -113,10 +118,14 @@ def fix_requirements(f: IO[bytes]) -> int: if req.value != b'pkg-resources==0.0.0\n' ] + # sort the requirements and remove duplicates + prev = None for requirement in sorted(requirements): after.extend(requirement.comments) assert requirement.value, requirement.value - after.append(requirement.value) + if prev is None or requirement.value != prev.value: + after.append(requirement.value) + prev = requirement after.extend(rest) after_string = b''.join(after) diff --git a/setup.cfg b/setup.cfg index 6a4c459..82a5457 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = pre_commit_hooks -version = 4.5.0 +version = 4.6.0 description = Some out-of-the-box hooks for pre-commit. long_description = file: README.md long_description_content_type = text/markdown diff --git a/tests/requirements_txt_fixer_test.py b/tests/requirements_txt_fixer_test.py index b725afa..c400be1 100644 --- a/tests/requirements_txt_fixer_test.py +++ b/tests/requirements_txt_fixer_test.py @@ -68,6 +68,12 @@ from pre_commit_hooks.requirements_txt_fixer import Requirement b'f<=2\n' b'g<2\n', ), + (b'a==1\nb==1\na==1\n', FAIL, b'a==1\nb==1\n'), + ( + b'a==1\nb==1\n#comment about a\na==1\n', + FAIL, + b'#comment about a\na==1\nb==1\n', + ), (b'ocflib\nDjango\nPyMySQL\n', FAIL, b'Django\nocflib\nPyMySQL\n'), ( b'-e git+ssh://git_url@tag#egg=ocflib\nDjango\nPyMySQL\n', -- cgit v1.2.3