Skip to content

Commit

Permalink
Add support for Python3.10 (#300)
Browse files Browse the repository at this point in the history
* ADd support for Python3.10

Signed-off-by: Mihai Maruseac <mihaimaruseac@google.com>

* Run CI on py3.10 too

Signed-off-by: Mihai Maruseac <mihaimaruseac@google.com>

* Reduce some indentation

Signed-off-by: Mihai Maruseac <mihaimaruseac@google.com>

---------

Signed-off-by: Mihai Maruseac <mihaimaruseac@google.com>
  • Loading branch information
mihaimaruseac authored Aug 27, 2024
1 parent e1e36f0 commit ae62038
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
fail-fast: false # Don't cancel other jobs if one fails
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
python-version: ['3.11', '3.12']
python-version: ['3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Set up Hatch
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ classifiers = [
"License :: OSI Approved :: Apache Software License",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Development Status :: 3 - Alpha",
Expand All @@ -30,7 +31,7 @@ dependencies = [
"sigstore",
"typing_extensions",
]
requires-python = ">=3.11"
requires-python = ">=3.10"
keywords = [
"machine learning",
"artificial intelligence",
Expand Down Expand Up @@ -61,7 +62,7 @@ parallel = true
randomize = true

[[tool.hatch.envs.hatch-test.matrix]]
python = ["3.11", "3.12"]
python = ["3.10", "3.11", "3.12"]

[tool.hatch.envs.docs]
description = """Custom environment for pdoc.
Expand Down
24 changes: 19 additions & 5 deletions src/model_signing/hashing/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import hashlib
import pathlib
import sys
from typing import BinaryIO

from typing_extensions import override
Expand Down Expand Up @@ -192,11 +193,24 @@ def digest_name(self) -> str:

@override
def compute(self) -> hashing.Digest:
# https://github.com/python/typeshed/issues/2166
# pytype: disable=wrong-arg-types
digest = hashlib.file_digest(self._fd, self._algorithm)
# pytype: enable=wrong-arg-types
return hashing.Digest(self.digest_name, digest.digest())
if sys.version_info >= (3, 11):
# https://github.com/python/typeshed/issues/2166
# pytype: disable=wrong-arg-types
digest = hashlib.file_digest(self._fd, self._algorithm)
# pytype: enable=wrong-arg-types
return hashing.Digest(self.digest_name, digest.digest())

# Polyfill for Python 3.10
# https://github.com/python/cpython/blob/4deb32a992/Lib/hashlib.py#L195
hasher = hashlib.new(self._algorithm)
buffer = bytearray(2**18)
view = memoryview(buffer)
while True:
size = self._fd.readinto(buffer)
if size == 0:
break
hasher.update(view[:size])
return hashing.Digest(self.digest_name, hasher.digest())

@property
@override
Expand Down
8 changes: 7 additions & 1 deletion src/model_signing/manifest/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,19 @@
from collections.abc import Iterable, Iterator
import dataclasses
import pathlib
from typing import Self
import sys

from typing_extensions import override

from model_signing.hashing import hashing


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


@dataclasses.dataclass(frozen=True)
class ResourceDescriptor:
"""A description of any content from any `Manifest`.
Expand Down
8 changes: 7 additions & 1 deletion src/model_signing/signing/as_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@
the hash computed from somewhere else and want to avoid the in-toto types.
"""

from typing import Self
import sys

from typing_extensions import override

from model_signing.manifest import manifest as manifest_module
from model_signing.signing import signing


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


class BytesPayload(signing.SigningPayload):
"""A payload that is a view into the bytes of a digest."""

Expand Down
8 changes: 7 additions & 1 deletion src/model_signing/signing/empty_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@
"""

import pathlib
from typing import Self
import sys

from typing_extensions import override

from model_signing.manifest import manifest
from model_signing.signing import signing


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


class EmptySigningPayload(signing.SigningPayload):
"""An empty signing payload, mostly just for testing."""

Expand Down
9 changes: 8 additions & 1 deletion src/model_signing/signing/in_toto.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"""

import pathlib
from typing import Any, Final, Self
import sys
from typing import Any, Final

from in_toto_attestation.v1 import statement
from typing_extensions import override
Expand All @@ -31,6 +32,12 @@
from model_signing.signing import signing


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


class IntotoPayload(signing.SigningPayload):
"""A generic payload in in-toto format.
Expand Down
8 changes: 7 additions & 1 deletion src/model_signing/signing/in_toto_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import json
import pathlib
from typing import Self
import sys

from sigstore_protobuf_specs.dev.sigstore.bundle import v1 as bundle_pb
from typing_extensions import override
Expand All @@ -28,6 +28,12 @@
from model_signing.signing import signing


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


class IntotoSignature(signing.Signature):
def __init__(self, bundle: bundle_pb.Bundle):
self._bundle = bundle
Expand Down
8 changes: 7 additions & 1 deletion src/model_signing/signing/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,17 @@

import abc
import pathlib
from typing import Self
import sys

from model_signing.manifest import manifest


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


class SigningPayload(metaclass=abc.ABCMeta):
"""Generic payload that we can sign."""

Expand Down
8 changes: 7 additions & 1 deletion src/model_signing/signing/sigstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import json
import pathlib
from typing import Self
import sys

from google.protobuf import json_format
from sigstore import dsse as sigstore_dsse
Expand All @@ -33,6 +33,12 @@
from model_signing.signing import signing


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


_IN_TOTO_JSON_PAYLOAD_TYPE: str = "application/vnd.in-toto+json"
_IN_TOTO_STATEMENT_TYPE: str = "https://in-toto.io/Statement/v1"

Expand Down
8 changes: 7 additions & 1 deletion tests/signing/empty_signing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

import pathlib
from typing import Self
import sys

import pytest
from typing_extensions import override
Expand All @@ -25,6 +25,12 @@
from tests import test_support


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


class TestEmptySigningPayload:
def test_build_from_digest_manifest(self):
digest = hashing.Digest("test", b"test_digest")
Expand Down
8 changes: 7 additions & 1 deletion tests/signing/sigstore_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import json
import pathlib
from typing import Self
import sys
from unittest import mock

import pytest
Expand All @@ -31,6 +31,12 @@
from model_signing.signing import sigstore


if sys.version_info >= (3, 11):
from typing import Self
else:
from typing_extensions import Self


class MockedSigstoreBundle:
"""Mocked SigstoreBundle that just records the signed payload."""

Expand Down

0 comments on commit ae62038

Please sign in to comment.