Skip to content

Commit fb4b742

Browse files
authored
Merge pull request #65 from acul71/fix/typecheck
Fix/typecheck - Great efforts by Arush (arcinston) and Luca (acul71)
2 parents e01dbd3 + 0c7acb4 commit fb4b742

32 files changed

+1243
-659
lines changed

.bumpversion.cfg

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[bumpversion]
2+
current_version = 0.0.9
3+
commit = True
4+
tag = True
5+
6+
[bumpversion:file:pyproject.toml]
7+
search = version = "{current_version}"
8+
replace = version = "{new_version}"
9+
10+
[bumpversion:file:multiaddr/__init__.py]
11+
search = __version__ = '{current_version}'
12+
replace = __version__ = '{new_version}'

.editorconfig

Lines changed: 0 additions & 21 deletions
This file was deleted.

.flake8

Lines changed: 0 additions & 2 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var/
2323
*.egg-info/
2424
.installed.cfg
2525
*.egg
26+
poetry.lock
27+
uv.lock
2628

2729
# PyInstaller
2830
# Usually these files are written by a python script from a template

.pre-commit-config.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
exclude: '.project-template|docs/conf.py|.*pb2\..*'
2+
repos:
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v5.0.0
5+
hooks:
6+
- id: check-yaml
7+
- id: check-toml
8+
- id: end-of-file-fixer
9+
- id: trailing-whitespace
10+
- repo: https://github.com/asottile/pyupgrade
11+
rev: v3.15.0
12+
hooks:
13+
- id: pyupgrade
14+
args: [--py39-plus]
15+
- repo: https://github.com/astral-sh/ruff-pre-commit
16+
rev: v0.11.10
17+
hooks:
18+
- id: ruff
19+
args: [--fix, --exit-non-zero-on-fix]
20+
- id: ruff-format
21+
- repo: https://github.com/northisup/pyright-pretty
22+
rev: v0.1.0
23+
hooks:
24+
- id: pyright-pretty
25+
args: [--show-summary]

Makefile

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ help:
1616
@echo "clean-build - remove build artifacts"
1717
@echo "clean-pyc - remove Python file artifacts"
1818
@echo "clean-test - remove test and coverage artifacts"
19-
@echo "lint - check style with flake8"
19+
@echo "lint - check style with ruff"
2020
@echo "test - run tests quickly with the default Python"
2121
@echo "test-all - run tests on every Python version with tox"
2222
@echo "coverage - check code coverage quickly with the default Python"
@@ -49,13 +49,20 @@ setup:
4949
pip install -r requirements_dev.txt
5050

5151
lint:
52-
flake8 multiaddr/* tests/*
52+
python -m ruff check --fix
53+
54+
fix:
55+
python -m ruff check --fix
56+
57+
typecheck:
58+
pre-commit run pyright-pretty --all-files
5359

5460
test:
55-
TOXENV=py27 tox
61+
python -m pytest tests
5662

5763
test-all:
58-
tox
64+
python3.11 -m pytest tests
65+
python3.13 -m pytest tests
5966

6067
coverage:
6168
coverage run --source multiaddr setup.py test

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ Simple
4040
m1 = Multiaddr("/ip4/127.0.0.1/udp/1234")
4141
4242
# construct from bytes
43-
m2 = Multiaddr(bytes_addr=m1.to_bytes())
43+
#m2 = Multiaddr(bytes_addr=m1.to_bytes()) # deprecated
44+
m2 = Multiaddr(m1.to_bytes())
4445
4546
assert str(m1) == "/ip4/127.0.0.1/udp/1234"
4647
assert str(m1) == str(m2)

multiaddr/codecs/__init__.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
import importlib
2-
2+
from typing import Any, Dict, Union
33

44
# These are special sizes
55
LENGTH_PREFIXED_VAR_SIZE = -1
66

77

8-
class NoneCodec:
9-
SIZE = 0
10-
IS_PATH = False
8+
class CodecBase:
9+
SIZE: int
10+
IS_PATH: bool
11+
12+
def to_string(self, proto: Any, buf: bytes) -> str:
13+
raise NotImplementedError
14+
15+
def to_bytes(self, proto: Any, string: str) -> bytes:
16+
raise NotImplementedError
17+
18+
19+
class NoneCodec(CodecBase):
20+
SIZE: int = 0
21+
IS_PATH: bool = False
22+
23+
def to_string(self, proto: Any, buf: bytes) -> str:
24+
return ""
25+
26+
def to_bytes(self, proto: Any, string: str) -> bytes:
27+
return b""
1128

1229

13-
CODEC_CACHE = {}
30+
CODEC_CACHE: Dict[str, CodecBase] = {}
1431

1532

16-
def codec_by_name(name):
17-
if name is None: # Special do nothing expect nothing pseudo-codec
18-
return NoneCodec
33+
def codec_by_name(name: Union[str, None]) -> CodecBase:
34+
if name is None: # Special "do nothing - expect nothing" pseudo-codec
35+
return NoneCodec()
1936
codec = CODEC_CACHE.get(name)
20-
if not codec:
21-
codec = CODEC_CACHE[name] = importlib.import_module(".{0}".format(name), __name__)
37+
if codec is None:
38+
module = importlib.import_module(f".{name}", __name__)
39+
codec_class = getattr(module, "Codec")
40+
assert codec_class is not None, f"Codec {name} not found"
41+
codec = codec_class()
42+
CODEC_CACHE[name] = codec
2243
return codec

0 commit comments

Comments
 (0)