summaryrefslogtreecommitdiffstats
path: root/pre_commit_hooks
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pre_commit_hooks/fix_encoding_pragma.py8
-rw-r--r--pre_commit_hooks/requirements_txt_fixer.py11
2 files changed, 18 insertions, 1 deletions
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)