Skip to content

Commit

Permalink
why does mypy hate me.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tansy Arron committed Apr 24, 2024
1 parent c322515 commit f528375
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/python/pants/backend/experimental/audit/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ async def audit(
)
requests = tuple(
request_type(
request_type.field_set_type.create(target)
request_type.field_set_type.create(target) # type: ignore[misc]
for target in targets
if (
request_type.tool_id in specified_ids
and request_type.field_set_type.is_applicable(target)
and request_type.field_set_type.is_applicable(target) # type: ignore[misc]
)
)
for request_type in request_types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from itertools import zip_longest
from typing import Any, Iterable

from pants.backend.experimental.audit.pip_audit import VulnerabilityData


def tabulate(rows: Iterable[Iterable[Any]]) -> tuple[list[str], list[int]]:
"""Return a list of formatted rows and a list of column sizes. For example::
Expand All @@ -30,7 +32,7 @@ def generate_header(sizes: Iterable[int]) -> str:


def format_results(
result: dict[str, list[dict[str:Any]]],
result: dict[str, list[VulnerabilityData]],
) -> str:
"""Returns a column formatted string for a given mapping of dependencies to vulnerability
results."""
Expand Down
18 changes: 10 additions & 8 deletions src/python/pants/backend/experimental/audit/pip_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Licensed under the Apache License, Version 2.0 (see LICENSE).
import logging
from dataclasses import dataclass
from typing import Dict, List, Optional, Set
from typing import Dict, List, Optional

import requests
from packaging.requirements import Requirement
Expand All @@ -19,10 +19,10 @@ class VulnerabilityData:
fixed_in: List[
str
] # A list of versions that can be upgraded to that resolve the vulnerability.
aliases: Set[str] # A set of aliases (alternative identifiers) for this result.
aliases: List[str] # A set of aliases (alternative identifiers) for this result.
link: str # A link to the vulnerability info.
summary: str # An optional short form human readable description.
withdrawn: bool # Represents whether the vulnerability has been withdrawn.
summary: Optional[str] # An optional short form human readable description.
withdrawn: Optional[str] # Represents whether the vulnerability has been withdrawn.

@classmethod
def from_raw_data(self, data):
Expand All @@ -37,7 +37,9 @@ def from_raw_data(self, data):
)


def audit_constraints_strings(constraints_strings, session, excludes_ids) -> Dict[str, str]:
def audit_constraints_strings(
constraints_strings, session, excludes_ids
) -> Dict[str, List[VulnerabilityData]]:
"""Retrieve security warnings for the given constraints from the Pypi json API."""
vulnerabilities = {}
for constraint_string in constraints_strings:
Expand All @@ -53,7 +55,7 @@ def audit_constraints_strings(constraints_strings, session, excludes_ids) -> Dic
version=specifier.version,
session=session,
)
if results is None:
if not results:
continue
vulnerabilities[str(requirement)] = [
result for result in results if result.vuln_id not in excludes_ids
Expand All @@ -63,13 +65,13 @@ def audit_constraints_strings(constraints_strings, session, excludes_ids) -> Dic

def audit_constraints_string(
package_name: str, version: str, session: requests.Session
) -> Optional[str]:
) -> List[VulnerabilityData]:
url = f"https://pypi.org/pypi/{package_name}/{str(version)}/json"
response = session.get(url=url)
response.raise_for_status()
response_json = response.json()
vulnerabilities = response_json.get("vulnerabilities")
if vulnerabilities:
vulns = [VulnerabilityData.from_raw_data(vuln_data) for vuln_data in vulnerabilities]
print(vulns)
return vulns
return []
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PypiAuditSubsystem(Subsystem):
options_scope = "pypi-audit"
help = "Configuration for the pypi audit rule."

lockfile_vulnerability_excludes = DictOption(
lockfile_vulnerability_excludes = DictOption[str](
help=softwrap(
"""
A mapping of logical names of Python lockfiles to a list of excluded vulnerability IDs.
Expand Down

0 comments on commit f528375

Please sign in to comment.