diff --git a/news/11935.feature.rst b/news/11935.feature.rst new file mode 100644 index 00000000000..b170ca1d8cd --- /dev/null +++ b/news/11935.feature.rst @@ -0,0 +1 @@ +Warn if ``--hash`` is used on a line without requirement in a requirements file. diff --git a/src/pip/_internal/req/req_file.py b/src/pip/_internal/req/req_file.py index aced95e64c4..8428888d932 100644 --- a/src/pip/_internal/req/req_file.py +++ b/src/pip/_internal/req/req_file.py @@ -2,6 +2,7 @@ Requirements file parsing """ +import logging import optparse import os import re @@ -76,6 +77,8 @@ # the 'dest' string values SUPPORTED_OPTIONS_REQ_DEST = [str(o().dest) for o in SUPPORTED_OPTIONS_REQ] +logger = logging.getLogger(__name__) + class ParsedRequirement: def __init__( @@ -209,6 +212,13 @@ def handle_option_line( options: Optional[optparse.Values] = None, session: Optional[PipSession] = None, ) -> None: + if opts.hashes: + logger.warning( + "%s line %s has --hash but no requirement, and will be ignored.", + filename, + lineno, + ) + if options: # percolate options upward if opts.require_hashes: