Skip to content

Commit

Permalink
Fix compatibility with typing_extensions>=4.6.0 and importlib_resources
Browse files Browse the repository at this point in the history
Fixes #10, #11, and #14
  • Loading branch information
sg495 committed Dec 18, 2023
1 parent 4a06e7c commit 9806e2c
Show file tree
Hide file tree
Showing 29 changed files with 143 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3
Expand Down
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"esbonio.sphinx.confDir": "${workspaceFolder}\\docs",
"workbench.colorTheme": "Default Dark+"
}
4 changes: 3 additions & 1 deletion multiformats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
while :class:`~multiformats.cid.CID` is a class for Content IDentifiers.
"""

__version__ = "0.2.1"
from __future__ import annotations

__version__ = "0.3.0"

from . import varint
from . import multicodec
Expand Down
2 changes: 2 additions & 0 deletions multiformats/cid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
>>> from multiformats import CID
"""

from __future__ import annotations

from typing import Any, cast, FrozenSet, Tuple, Type, TypeVar, Union
from typing_extensions import Literal, Final
from typing_validation import validate
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multiaddr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
>>> from multiformats import multiaddr
"""

from __future__ import annotations

from itertools import islice, chain
from typing import Any, cast, ClassVar, Dict, Iterator, List, Optional, overload, Sequence, Tuple, Type, Union
from weakref import WeakValueDictionary
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multiaddr/err.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Errors for the :mod:`~multiformats.multiaddr` module.
"""

from __future__ import annotations

import builtins

class MultiaddrKeyError(builtins.KeyError): # pylint: disable = redefined-builtin
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multiaddr/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
For expected address bytestring sizes, see the `multiaddr table <https://github.com/multiformats/multiaddr/blob/master/protocols.csv>`_.
"""

from __future__ import annotations

from ipaddress import IPv4Address, IPv6Address, AddressValueError
from typing import Callable, Dict, Optional, Tuple
from typing_validation import validate
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multibase/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
>>> from multiformats import multibase
"""

from __future__ import annotations

from abc import ABC, abstractmethod
import binascii
import importlib.resources as importlib_resources
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multibase/err.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Errors for the :mod:`~multiformats.multibase` module.
"""

from __future__ import annotations

import builtins

class MultibaseKeyError(builtins.KeyError): # pylint: disable = redefined-builtin
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multibase/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"""

from __future__ import annotations

import binascii
from itertools import product
from types import MappingProxyType
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multicodec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
>>> from multiformats import multicodec
"""

from __future__ import annotations

import importlib.resources as importlib_resources
from io import BufferedIOBase
import json
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multicodec/err.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Errors for the :mod:`~multiformats.multicodec` module.
"""

from __future__ import annotations

import builtins

class MulticodecKeyError(builtins.KeyError): # pylint: disable = redefined-builtin
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
>>> from multiformats import multihash
"""

from __future__ import annotations

from io import BytesIO, BufferedIOBase
from typing import AbstractSet, Any, cast, ClassVar, Dict, Iterator, Mapping, Optional, overload, Union, Sequence, Tuple, Type, TypeVar
from weakref import WeakValueDictionary
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/_hashfuns/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
""" Implementations for specific hash functions. """

from __future__ import annotations

from .utils import Hashfun, validate_hashfun_args, repeat_hashfun

__all__ = ["Hashfun", "validate_hashfun_args", "repeat_hashfun"]
10 changes: 7 additions & 3 deletions multiformats/multihash/_hashfuns/blake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Implementation for the ``blake2`` and ``blake3`` hash functions, using the optional dependency `blake3 <https://github.com/oconnor663/blake3-py>`_.
"""

# pylint: disable = no-member, not-callable

from __future__ import annotations

import hashlib
from typing import Optional

Expand All @@ -12,7 +16,7 @@ def _hashlib_blake2(version: str, digest_bits: int) -> Hashfun:
h = getattr(hashlib, f"blake2{version}")
def hashfun(data: BytesLike, size: Optional[int] = None) -> bytes:
validate_hashfun_args(data, size, digest_bits//8)
m: hashlib._Hash = h(digest_size=digest_bits//8) # pylint: disable = no-member
m: hashlib._Hash = h(digest_size=digest_bits//8)
m.update(data)
d = m.digest()
return d if size is None else d[:size]
Expand All @@ -28,13 +32,13 @@ def _jit_register_blake2(m, register) -> bool: # type: ignore

def _blake3() -> Hashfun:
try:
from blake3 import blake3 # type: ignore # pylint: disable = import-outside-toplevel
from blake3 import blake3 # pylint: disable = import-outside-toplevel
except ImportError as e:
raise ImportError("Module 'blake3' must be installed to use 'blake3' hash function. Consider running 'pip install blake3'.") from e
def hashfun(data: BytesLike, size: Optional[int] = None) -> bytes:
validate_hashfun_args(data, size, None, size_required=True, name="blake3")
assert size is not None
m = blake3() # pylint: disable = not-callable
m = blake3()
m.update(data)
d: bytes = m.digest(size)
return d
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/_hashfuns/filecoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- https://github.com/filecoin-project/neptune
"""

from __future__ import annotations

import hashlib
from hashlib import sha256
from typing import Optional
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/_hashfuns/kangarootwelve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Implementation for the ``kangarootwelve`` hash function, using `pycryptodomex <https://github.com/Legrandin/pycryptodome/>`_.
"""

from __future__ import annotations

from typing import Optional

from multiformats.varint import BytesLike
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/_hashfuns/keccak.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Implementation for the ``keccak`` hash functions, using the optional dependency `pysha3 <https://github.com/tiran/pysha3>`_.
"""

from __future__ import annotations

import hashlib
from typing import Optional

Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/_hashfuns/md.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using `hashlib <https://docs.python.org/3/library/hashlib.html>`_ and `pycryptodomex <https://github.com/Legrandin/pycryptodome/>`_.
"""

from __future__ import annotations

import hashlib
from typing import Optional

Expand Down
4 changes: 3 additions & 1 deletion multiformats/multihash/_hashfuns/murmur3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
Implementation for the ``murmur3`` hash functions, using the optional dependency `mmh3 <https://github.com/hajimes/mmh3>`_.
"""

from __future__ import annotations

from typing import Optional

from multiformats.varint import BytesLike
from .utils import Hashfun, validate_hashfun_args

def _murmur3(version: str, digest_bits: int) -> Hashfun:
try:
import mmh3 # type: ignore # pylint: disable = import-outside-toplevel
import mmh3 # pylint: disable = import-outside-toplevel
except ImportError as e:
raise ImportError("Module 'mmh3' must be installed to use 'murmur3' hash functions. Consider running 'pip install mmh3'.") from e
if version == "32":
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/_hashfuns/sha.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using `hashlib <https://docs.python.org/3/library/hashlib.html>`_ and `pycryptodomex <https://github.com/Legrandin/pycryptodome/>`_.
"""

from __future__ import annotations

import hashlib
from typing import Optional

Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/_hashfuns/skein.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Implementation for the ``skein`` hash functions, using the optional dependency `pyskein <https://pythonhosted.org/pyskein/>`_.
"""

from __future__ import annotations

import hashlib
from typing import Optional

Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/_hashfuns/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Utilities for hash function implementation.
"""

from __future__ import annotations

from typing import Optional
from typing_extensions import Literal, Protocol, runtime_checkable
from typing_validation import validate
Expand Down
2 changes: 2 additions & 0 deletions multiformats/multihash/err.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Errors for the :mod:`~multiformats.multihash` module.
"""

from __future__ import annotations

import builtins

class MultihashKeyError(builtins.KeyError): # pylint: disable = redefined-builtin
Expand Down
6 changes: 4 additions & 2 deletions multiformats/multihash/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
The max digest sizes (if not :obj:`None`) are used to sense-check hash digests passed to :func:`~multiformats.multihash.wrap`
and/or obtained from :func:`~multiformats.multihash.unwrap`: telling whether a digest has been generated by a hash function
is deemed to be computationally unfeasible in general, but hash digests of length greater than the max digest size
can always be discounted as invalid.
runt can always be discounted as invalid.
"""

from __future__ import annotations

import re
from typing import Dict, Optional, Tuple
from typing_validation import validate
Expand Down Expand Up @@ -274,7 +276,7 @@ def _jit_register_hashfun_dbl(name: str, check_only: bool = False) -> bool:
_jit_register_hashfun_dir = {
"sha": _jit_register_hashfun_sha,
"bla": _jit_register_hashfun_bla,
"kec": _jit_register_hashfun_kec,
# "kec": _jit_register_hashfun_kec, # kec is currently unavailable
"ske": _jit_register_hashfun_ske,
"mur": _jit_register_hashfun_mur,
"md5": _jit_register_hashfun_md5,
Expand Down
2 changes: 2 additions & 0 deletions multiformats/varint/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
>>> from multiformats import varint
"""

from __future__ import annotations

from io import BufferedIOBase
from typing import BinaryIO, cast, List, Optional, overload, Tuple, Union, TypeVar
from typing_extensions import Final
Expand Down
16 changes: 10 additions & 6 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ project_urls =
# see https://pypi.org/classifiers/
classifiers =
Development Status :: 3 - Alpha
Programming Language :: Python :: 3.12
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.8
Expand All @@ -27,10 +29,10 @@ classifiers =
packages = find:
python_requires = >=3.7
install_requires =
typing-extensions
typing-validation
bases
multiformats-config
typing-extensions>=4.6.0
typing-validation>=1.1.0
bases>=0.3.0
multiformats-config>=0.3.0

[options.package_data]
* = py.typed, *.json
Expand All @@ -44,14 +46,16 @@ dev =
pylint
pytest
pytest-cov
pysha3
; pysha3
blake3
pyskein
mmh3
pycryptodomex
rich # optional dependency of typing_validation
full =
pysha3
; pysha3
blake3
pyskein
mmh3
pycryptodomex
rich # optional dependency of typing_validation
Loading

0 comments on commit 9806e2c

Please sign in to comment.