Skip to content

Commit ae62038

Browse files
Add support for Python3.10 (#300)
* ADd support for Python3.10 Signed-off-by: Mihai Maruseac <[email protected]> * Run CI on py3.10 too Signed-off-by: Mihai Maruseac <[email protected]> * Reduce some indentation Signed-off-by: Mihai Maruseac <[email protected]> --------- Signed-off-by: Mihai Maruseac <[email protected]>
1 parent e1e36f0 commit ae62038

File tree

12 files changed

+87
-17
lines changed

12 files changed

+87
-17
lines changed

.github/workflows/unit_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
fail-fast: false # Don't cancel other jobs if one fails
3636
matrix:
3737
os: [ubuntu-latest, macos-latest, windows-latest]
38-
python-version: ['3.11', '3.12']
38+
python-version: ['3.10', '3.11', '3.12']
3939
steps:
4040
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
4141
- name: Set up Hatch

pyproject.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ classifiers = [
1515
"License :: OSI Approved :: Apache Software License",
1616
"Programming Language :: Python :: 3 :: Only",
1717
"Programming Language :: Python :: 3",
18+
"Programming Language :: Python :: 3.10",
1819
"Programming Language :: Python :: 3.11",
1920
"Programming Language :: Python :: 3.12",
2021
"Development Status :: 3 - Alpha",
@@ -30,7 +31,7 @@ dependencies = [
3031
"sigstore",
3132
"typing_extensions",
3233
]
33-
requires-python = ">=3.11"
34+
requires-python = ">=3.10"
3435
keywords = [
3536
"machine learning",
3637
"artificial intelligence",
@@ -61,7 +62,7 @@ parallel = true
6162
randomize = true
6263

6364
[[tool.hatch.envs.hatch-test.matrix]]
64-
python = ["3.11", "3.12"]
65+
python = ["3.10", "3.11", "3.12"]
6566

6667
[tool.hatch.envs.docs]
6768
description = """Custom environment for pdoc.

src/model_signing/hashing/file.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
import hashlib
5050
import pathlib
51+
import sys
5152
from typing import BinaryIO
5253

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

193194
@override
194195
def compute(self) -> hashing.Digest:
195-
# https://github.com/python/typeshed/issues/2166
196-
# pytype: disable=wrong-arg-types
197-
digest = hashlib.file_digest(self._fd, self._algorithm)
198-
# pytype: enable=wrong-arg-types
199-
return hashing.Digest(self.digest_name, digest.digest())
196+
if sys.version_info >= (3, 11):
197+
# https://github.com/python/typeshed/issues/2166
198+
# pytype: disable=wrong-arg-types
199+
digest = hashlib.file_digest(self._fd, self._algorithm)
200+
# pytype: enable=wrong-arg-types
201+
return hashing.Digest(self.digest_name, digest.digest())
202+
203+
# Polyfill for Python 3.10
204+
# https://github.com/python/cpython/blob/4deb32a992/Lib/hashlib.py#L195
205+
hasher = hashlib.new(self._algorithm)
206+
buffer = bytearray(2**18)
207+
view = memoryview(buffer)
208+
while True:
209+
size = self._fd.readinto(buffer)
210+
if size == 0:
211+
break
212+
hasher.update(view[:size])
213+
return hashing.Digest(self.digest_name, hasher.digest())
200214

201215
@property
202216
@override

src/model_signing/manifest/manifest.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,19 @@
5555
from collections.abc import Iterable, Iterator
5656
import dataclasses
5757
import pathlib
58-
from typing import Self
58+
import sys
5959

6060
from typing_extensions import override
6161

6262
from model_signing.hashing import hashing
6363

6464

65+
if sys.version_info >= (3, 11):
66+
from typing import Self
67+
else:
68+
from typing_extensions import Self
69+
70+
6571
@dataclasses.dataclass(frozen=True)
6672
class ResourceDescriptor:
6773
"""A description of any content from any `Manifest`.

src/model_signing/signing/as_bytes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@
1818
the hash computed from somewhere else and want to avoid the in-toto types.
1919
"""
2020

21-
from typing import Self
21+
import sys
2222

2323
from typing_extensions import override
2424

2525
from model_signing.manifest import manifest as manifest_module
2626
from model_signing.signing import signing
2727

2828

29+
if sys.version_info >= (3, 11):
30+
from typing import Self
31+
else:
32+
from typing_extensions import Self
33+
34+
2935
class BytesPayload(signing.SigningPayload):
3036
"""A payload that is a view into the bytes of a digest."""
3137

src/model_signing/signing/empty_signing.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,20 @@
2121
"""
2222

2323
import pathlib
24-
from typing import Self
24+
import sys
2525

2626
from typing_extensions import override
2727

2828
from model_signing.manifest import manifest
2929
from model_signing.signing import signing
3030

3131

32+
if sys.version_info >= (3, 11):
33+
from typing import Self
34+
else:
35+
from typing_extensions import Self
36+
37+
3238
class EmptySigningPayload(signing.SigningPayload):
3339
"""An empty signing payload, mostly just for testing."""
3440

src/model_signing/signing/in_toto.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"""
2121

2222
import pathlib
23-
from typing import Any, Final, Self
23+
import sys
24+
from typing import Any, Final
2425

2526
from in_toto_attestation.v1 import statement
2627
from typing_extensions import override
@@ -31,6 +32,12 @@
3132
from model_signing.signing import signing
3233

3334

35+
if sys.version_info >= (3, 11):
36+
from typing import Self
37+
else:
38+
from typing_extensions import Self
39+
40+
3441
class IntotoPayload(signing.SigningPayload):
3542
"""A generic payload in in-toto format.
3643

src/model_signing/signing/in_toto_signature.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import json
1818
import pathlib
19-
from typing import Self
19+
import sys
2020

2121
from sigstore_protobuf_specs.dev.sigstore.bundle import v1 as bundle_pb
2222
from typing_extensions import override
@@ -28,6 +28,12 @@
2828
from model_signing.signing import signing
2929

3030

31+
if sys.version_info >= (3, 11):
32+
from typing import Self
33+
else:
34+
from typing_extensions import Self
35+
36+
3137
class IntotoSignature(signing.Signature):
3238
def __init__(self, bundle: bundle_pb.Bundle):
3339
self._bundle = bundle

src/model_signing/signing/signing.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,17 @@
4343

4444
import abc
4545
import pathlib
46-
from typing import Self
46+
import sys
4747

4848
from model_signing.manifest import manifest
4949

5050

51+
if sys.version_info >= (3, 11):
52+
from typing import Self
53+
else:
54+
from typing_extensions import Self
55+
56+
5157
class SigningPayload(metaclass=abc.ABCMeta):
5258
"""Generic payload that we can sign."""
5359

src/model_signing/signing/sigstore.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import json
1818
import pathlib
19-
from typing import Self
19+
import sys
2020

2121
from google.protobuf import json_format
2222
from sigstore import dsse as sigstore_dsse
@@ -33,6 +33,12 @@
3333
from model_signing.signing import signing
3434

3535

36+
if sys.version_info >= (3, 11):
37+
from typing import Self
38+
else:
39+
from typing_extensions import Self
40+
41+
3642
_IN_TOTO_JSON_PAYLOAD_TYPE: str = "application/vnd.in-toto+json"
3743
_IN_TOTO_STATEMENT_TYPE: str = "https://in-toto.io/Statement/v1"
3844

0 commit comments

Comments
 (0)