Skip to content

Commit 85ed18d

Browse files
authored
refactor: finish typing hints (#21)
* Add stubs for is_ipfs dependencies : py-cid, py-multibase * refactor: finish typing hints for py-is_ipfs: * add stubs to setup * add py.typed * update dev dependcies * add type testing to tox.ini * add basic mypy.ini
1 parent c8ac622 commit 85ed18d

File tree

11 files changed

+136
-4
lines changed

11 files changed

+136
-4
lines changed

cid-stubs/__init__.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .cid import (
2+
CIDv0 as CIDv0,
3+
CIDv1 as CIDv1,
4+
from_bytes as from_bytes,
5+
from_string as from_string,
6+
is_cid as is_cid,
7+
make_cid as make_cid,
8+
)

cid-stubs/cid.pyi

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from _typeshed import Incomplete
2+
from typing import Union, Callable
3+
4+
class BaseCID:
5+
__hash__: Callable[[object], int]
6+
def __init__(self, version: int, codec: str, multihash: str) -> None: ...
7+
@property
8+
def version(self) -> int: ...
9+
@property
10+
def codec(self) -> str: ...
11+
@property
12+
def multihash(self) -> bytes: ...
13+
@property
14+
def buffer(self) -> None: ...
15+
def encode(self, *args, **kwargs) -> None: ...
16+
def __eq__(self, other): ...
17+
18+
class CIDv0(BaseCID):
19+
CODEC: str
20+
def __init__(self, multihash: bytes) -> None: ...
21+
@property
22+
def buffer(self): ...
23+
def encode(self): ...
24+
def to_v1(self) -> CIDv1: ...
25+
26+
class CIDv1(BaseCID):
27+
def __init__(self, codec: str, multihash: Union[str, bytes]) -> None: ...
28+
@property
29+
def buffer(self): ...
30+
def encode(self, encoding=...): ...
31+
def to_v0(self) -> CIDv0: ...
32+
33+
def make_cid(*args) -> Union[CIDv0, CIDv1]: ...
34+
def is_cid(cidstr: Union[str, bytes]) -> bool: ...
35+
def from_string(cidstr: str) -> Union[CIDv0, CIDv1]: ...
36+
def from_bytes(cidbytes: bytes) -> Union[CIDv0, CIDv1]: ...

is_ipfs/is_ipfs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def _id_is_explicit_tld(self, input_string: str) -> bool:
248248
r"(?=^.{4,253}\.?$)(^((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$)"
249249
)
250250
hostname = urlparse(f"http://{input_string}").hostname
251-
return bool(re.search(fqdn_with_tld, hostname))
251+
return bool(re.search(fqdn_with_tld, hostname)) if hostname else False
252252

253253
def _is_native_ipfs_url(self) -> bool:
254254
"""

is_ipfs/py.typed

Whitespace-only changes.

multibase-stubs/__init__.pyi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .multibase import (
2+
ENCODINGS as ENCODINGS,
3+
Encoding as Encoding,
4+
decode as decode,
5+
encode as encode,
6+
get_codec as get_codec,
7+
is_encoded as is_encoded,
8+
)

multibase-stubs/converters.pyi

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from _typeshed import Incomplete
2+
from baseconv import BaseConverter # type: ignore
3+
4+
class BaseStringConverter(BaseConverter):
5+
def encode(self, bytes: bytes) -> bytes: ...
6+
def bytes_to_int(self, bytes: bytes) -> int: ...
7+
def decode(self, bytes: bytes) -> bytes: ...
8+
9+
class Base16StringConverter(BaseStringConverter):
10+
def encode(self, bytes: bytes) -> bytes: ...
11+
12+
class BaseByteStringConverter:
13+
ENCODE_GROUP_BYTES: int
14+
ENCODING_BITS: int
15+
DECODING_BITS: int
16+
digits: Incomplete
17+
def __init__(self, digits): ...
18+
def encode(self, bytes): ...
19+
def decode(self, bytes): ...
20+
21+
class Base64StringConverter(BaseByteStringConverter):
22+
def encode(self, bytes: bytes) -> bytes: ...
23+
def decode(self, bytes: bytes) -> bytes: ...
24+
25+
class Base32StringConverter(BaseByteStringConverter):
26+
def encode(self, bytes: bytes) -> bytes: ...
27+
def decode(self, bytes: bytes) -> bytes: ...
28+
29+
class IdentityConverter:
30+
def encode(self, x): ...
31+
def decode(self, x): ...

multibase-stubs/multibase.pyi

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from .converters import (
2+
Base16StringConverter as Base16StringConverter,
3+
Base32StringConverter as Base32StringConverter,
4+
Base64StringConverter as Base64StringConverter,
5+
BaseStringConverter as BaseStringConverter,
6+
IdentityConverter as IdentityConverter,
7+
)
8+
from typing import List, NamedTuple, Union, Dict
9+
10+
class Encoding(NamedTuple):
11+
encoding: str
12+
code: bytes
13+
converter: Union[
14+
IdentityConverter,
15+
BaseStringConverter,
16+
Base16StringConverter,
17+
Base32StringConverter,
18+
Base64StringConverter,
19+
]
20+
21+
CODE_LENGTH: int
22+
ENCODINGS: List[Encoding]
23+
ENCODINGS_LOOKUP: Dict[Union[str, bytes], Encoding]
24+
25+
def encode(encoding: str, data: Union[str, bytes]) -> bytes: ...
26+
def get_codec(data: Union[str, bytes]) -> Encoding: ...
27+
def is_encoded(data: Union[str, bytes]) -> bool: ...
28+
def decode(data: Union[str, bytes]) -> str: ...

mypy.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Global options:
2+
3+
[mypy]
4+
warn_return_any = True
5+
warn_unused_configs = True

requirements-dev.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
pre-commit==2.17.0
22
black==22.3.0
3-
tox
3+
tox
4+
coverage
5+
mypy

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66

77
setuptools.setup(
88
name="py-is_ipfs",
9-
version="0.4.0",
9+
version="0.5.0",
1010
description="Python library to identify valid IPFS resources",
1111
long_description=long_description,
1212
long_description_content_type="text/markdown",
1313
url="https://github.com/Barabazs/py-is_ipfs",
1414
author="Barabazs",
1515
packages=setuptools.find_packages(exclude=["tests", "tests.*"]),
16+
package_data={
17+
"cid-stubs": ["__init__.pyi", "cid.pyi"],
18+
"multibase-stubs": ["__init__.pyi", "converters.pyi", "multibase.pyi"],
19+
},
1620
license="MIT",
1721
classifiers=[
1822
"Development Status :: 4 - Beta",

0 commit comments

Comments
 (0)