Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

service/interface: handle fractional seconds in RFC3339 timestamps #417

Merged
merged 4 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ All versions prior to 0.0.9 are untracked.

## [Unreleased]

### Fixed

* Fixed a timestamp parsing bug that occurred with some vulnerability
reports provided by the OSV service
([#416](https://github.com/pypa/pip-audit/issues/416))

## [2.4.6]

### Fixed
Expand Down
10 changes: 9 additions & 1 deletion pip_audit/_service/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,15 @@ def query_all(
def _parse_rfc3339(dt: str | None) -> datetime | None:
if dt is None:
return None
return datetime.strptime(dt, "%Y-%m-%dT%H:%M:%SZ")

# NOTE: OSV's schema says timestamps are RFC3339 but strptime
# has no way to indicate an optional field (like `%f`), so
# we have to try-and-retry with the two different expected formats.
# See: https://github.com/google/osv.dev/issues/857
try:
return datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S.%fZ")
except ValueError:
return datetime.strptime(dt, "%Y-%m-%dT%H:%M:%SZ")


class ServiceError(Exception):
Expand Down
16 changes: 16 additions & 0 deletions test/service/test_interface.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime

import pytest
from packaging.version import Version

Expand All @@ -6,6 +8,7 @@
ResolvedDependency,
SkippedDependency,
VulnerabilityResult,
VulnerabilityService,
)


Expand Down Expand Up @@ -73,3 +76,16 @@ def test_vulnerability_result_has_any_id():
assert result.has_any_id({"ham", "eggs", "BAZ"})
assert not result.has_any_id({"zilch"})
assert not result.has_any_id(set())


class TestVulnerabilityService:
@pytest.mark.parametrize(
["timestamp", "result"],
[
(None, None),
("2019-08-24T14:15:22Z", datetime.datetime(2019, 8, 24, 14, 15, 22)),
("2022-10-22T00:00:27.668938Z", datetime.datetime(2022, 10, 22, 0, 0, 27, 668938)),
],
)
def test_parse_rfc3339(self, timestamp, result):
assert VulnerabilityService._parse_rfc3339(timestamp) == result