{"id":"4b26797bd9e49aad","repo":"pypa/pip","slug":"requested-ireq-has-invalid-metadata-requires-di","errorCode":null,"errorMessage":"Requested {ireq} has invalid metadata: Requires-Dist in {source}: {e}","messagePattern":"Requested (.+?) has invalid metadata: Requires-Dist in (.+?): (.+?)","errorType":"exception","errorClass":"MetadataInvalid","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/operations/prepare.py","lineNumber":273,"sourceCode":"\n\ndef _canonical_requires(\n    req: InstallRequirement, dist: BaseDistribution, source: str\n) -> frozenset[str]:\n    \"\"\"Return the canonicalized ``Requires-Dist`` entries of ``dist``.\n\n    ``source`` describes which metadata file ``dist`` was parsed from, for\n    use in error messages.\n    \"\"\"\n    canonical: set[str] = set()\n    for raw in dist.iter_raw_dependencies():\n        try:\n            # strip() because a folded metadata header may be returned\n            # with a leading newline; iter_dependencies() strips for the\n            # same reason.\n            canonical.add(_canonicalize_requirement(raw.strip()))\n        except InvalidRequirement as e:\n            raise MetadataInvalid(req, f\"Requires-Dist in {source}: {e}\")\n    return frozenset(canonical)\n\n\ndef _check_sidecar_matches_wheel(\n    req: InstallRequirement,\n    sidecar_dist: BaseDistribution,\n    wheel_dist: BaseDistribution,\n) -> None:\n    \"\"\"Check that a .metadata-based distribution matches the wheel's METADATA.\n\n    Compare ``Name``, ``Version``, ``Requires-Dist``, ``Requires-Python``\n    and ``Provides-Extra`` between the two and abort the install on any\n    mismatch as PEP 658 requires the metadata files \"MUST be identical\".\n\n    While the PEP doesn't mandate that consumers enforce the identical\n    requirement, it's good nonetheless to check to prevent confusing\n    behaviour when an index misbehaves.\n","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_internal/operations/prepare.py#L255-L291","documentation":"Raised as MetadataInvalid when canonicalizing a Requires-Dist entry from a distribution's metadata fails to parse as a valid PEP 508 requirement. At prepare.py:266-273, _canonical_requires iterates raw dependency strings and calls _canonicalize_requirement, which uses get_requirement(raw.strip()); an InvalidRequirement propagates and is wrapped with the metadata source name.","triggerScenarios":"A distribution (wheel METADATA or a PEP 658 .metadata sidecar) contains a Requires-Dist line that packaging's Requirement parser rejects — e.g. malformed specifier, illegal characters, or a non-PEP-508 syntax. The 'source' in the message indicates whether it was the sidecar or the wheel METADATA.","commonSituations":"A package built with an old/non-compliant tool, hand-edited METADATA, a corrupted download, or a metadata field containing unsupported syntax (e.g. environment markers in an unexpected position).","solutions":["Open the offending *.dist-info/METADATA and review the Requires-Dist lines for syntax errors.","Fix the requirement string to conform to PEP 508 (e.g. 'dep>=1.0; python_version<\"3.12\"').","Rebuild the wheel with a current build backend and reinstall.","Pin to a version of the package whose metadata is valid."],"exampleFix":"# before (METADATA)\nRequires-Dist: numpy >=1.20, <2  # commas inside specifier are invalid in one entry\n\n# after\nRequires-Dist: numpy>=1.20,<2","handlingStrategy":"validation","validationCode":"from pip._vendor.packaging.requirements import InvalidRequirement, Requirement\n\ndef validate_requires_dist(metadata_text):\n    for line in metadata_text.splitlines():\n        if line.startswith(\"Requires-Dist:\"):\n            raw = line.split(\":\", 1)[1].strip()\n            try:\n                Requirement(raw)\n            except InvalidRequirement as e:\n                raise ValueError(f\"invalid Requires-Dist {raw!r}: {e}\")","typeGuard":"from pip._vendor.packaging.requirements import Requirement, InvalidRequirement\ndef is_valid_requirement_str(raw: str) -> bool:\n    try:\n        Requirement(raw)\n        return True\n    except InvalidRequirement:\n        return False","tryCatchPattern":null,"preventionTips":["Lint METADATA Requires-Dist entries in CI with packaging.Requirement.","Use a modern build backend that validates metadata on build.","Pin to package versions known to ship valid metadata."],"tags":["metadata","packaging","pep508","requires-dist","install"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}