Skip to content

Commit

Permalink
black the generated protocol messages code (Chia-Network#11786)
Browse files Browse the repository at this point in the history
* black the generated protocol messages code

* Update tests/util/build_network_protocol_files.py

Co-authored-by: Arvid Norberg <arvid@libtorrent.org>

* more focused noqa

Co-authored-by: Arvid Norberg <arvid@libtorrent.org>
  • Loading branch information
altendky and arvidn authored Jun 8, 2022
1 parent 61fa0ed commit a87e8e3
Show file tree
Hide file tree
Showing 7 changed files with 2,363 additions and 212 deletions.
5 changes: 5 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
max-line-length = 120
exclude = ./typings/**/*
ignore = E203,W503
per-file-ignores =
tests/util/build_network_protocol_files.py:F405
tests/util/test_network_protocol_files.py:F405
tests/util/test_network_protocol_json.py:F405
tests/util/protocol_messages_json.py:E501
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ repos:
rev: 21.12b0
hooks:
- id: black
exclude: "tests/util/(protocol_messages_json|test_network_protocol_json).py"
additional_dependencies: ['click<8.1']
- repo: https://gitlab.com/pycqa/flake8
rev: 3.9.2
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ include = '''
| (benchmarks|build_scripts|chia|tests|tools)/.*\.pyi?
)$
'''
exclude = 'tests/util/(protocol_messages_json|test_network_protocol_json).py'
exclude = ''
53 changes: 29 additions & 24 deletions tests/util/build_network_protocol_files.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# flake8: noqa

import os
import subprocess
import sysconfig
from typing import Callable, Any
from pathlib import Path
from chia.util.streamable import Streamable, streamable
from tests.util.network_protocol_data import *
from tests.util.network_protocol_data import * # noqa: F403
from chia.util.ints import uint32

version = "1.0"


tests_dir = Path(__file__).resolve().parent


def get_network_protocol_filename() -> Path:
tests_dir = Path(os.path.dirname(os.path.abspath(__file__)))
return tests_dir / Path("protocol_messages_bytes-v" + version)


Expand Down Expand Up @@ -159,11 +160,10 @@ def build_protocol_test() -> str:

result = """# this file is generated by build_network_protocol_files.py
# flake8: noqa
from typing import Tuple
from pathlib import Path
from tests.util.network_protocol_data import *
from tests.util.protocol_messages_json import *
from tests.util.network_protocol_data import * # noqa: F403
from tests.util.protocol_messages_json import * # noqa: F403
from tests.util.build_network_protocol_files import get_network_protocol_filename
Expand Down Expand Up @@ -203,7 +203,6 @@ def visitor(obj: Any, name: str) -> None:
def get_protocol_json() -> str:

result = """# this file is generated by build_network_protocol_files.py
# flake8: noqa
from typing import Any, Dict
"""
counter = 0
Expand All @@ -223,9 +222,8 @@ def build_json_test() -> str:

result = """# this file is generated by build_network_protocol_files.py
# flake8: noqa
from tests.util.network_protocol_data import *
from tests.util.protocol_messages_json import *
from tests.util.network_protocol_data import * # noqa: F403
from tests.util.protocol_messages_json import * # noqa: F403
def test_protocol_json() -> None:
Expand All @@ -245,15 +243,22 @@ def visitor(obj: Any, name: str) -> None:


if __name__ == "__main__":
tests_dir = Path(os.path.dirname(os.path.abspath(__file__)))
with open(get_network_protocol_filename(), "wb") as f:
f.write(get_protocol_bytes())

with open(tests_dir / Path("test_network_protocol_files.py"), "w") as f2:
f2.write(build_protocol_test())

with open(tests_dir / Path("protocol_messages_json.py"), "w") as f2:
f2.write(get_protocol_json())

with open(tests_dir / Path("test_network_protocol_json.py"), "w") as f2:
f2.write(build_json_test())
get_network_protocol_filename().write_bytes(get_protocol_bytes())

name_to_function = {
"test_network_protocol_files.py": build_protocol_test,
"protocol_messages_json.py": get_protocol_json,
"test_network_protocol_json.py": build_json_test,
}

scripts_path = Path(sysconfig.get_path("scripts"))

for name, function in name_to_function.items():
path = tests_dir.joinpath(name)
path.write_text(function())
# black seems to have trouble when run as a module so not using `python -m black`
subprocess.run(
[scripts_path.joinpath("black"), os.fspath(path.relative_to(tests_dir))],
check=True,
cwd=tests_dir,
)
2,461 changes: 2,290 additions & 171 deletions tests/util/protocol_messages_json.py

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions tests/util/test_network_protocol_files.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# this file is generated by build_network_protocol_files.py

# flake8: noqa
from typing import Tuple
from pathlib import Path
from tests.util.network_protocol_data import *
from tests.util.protocol_messages_json import *
from tests.util.network_protocol_data import * # noqa: F403
from tests.util.protocol_messages_json import * # noqa: F403
from tests.util.build_network_protocol_files import get_network_protocol_filename


Expand Down
48 changes: 36 additions & 12 deletions tests/util/test_network_protocol_json.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# this file is generated by build_network_protocol_files.py

# flake8: noqa
from tests.util.network_protocol_data import *
from tests.util.protocol_messages_json import *
from tests.util.network_protocol_data import * # noqa: F403
from tests.util.protocol_messages_json import * # noqa: F403


def test_protocol_json() -> None:
Expand Down Expand Up @@ -47,15 +46,26 @@ def test_protocol_json() -> None:
assert str(respond_unfinished_block_json) == str(respond_unfinished_block.to_json_dict())
assert type(respond_unfinished_block).from_json_dict(respond_unfinished_block_json) == respond_unfinished_block
assert str(new_signage_point_or_end_of_subslot_json) == str(new_signage_point_or_end_of_subslot.to_json_dict())
assert type(new_signage_point_or_end_of_subslot).from_json_dict(new_signage_point_or_end_of_subslot_json) == new_signage_point_or_end_of_subslot
assert str(request_signage_point_or_end_of_subslot_json) == str(request_signage_point_or_end_of_subslot.to_json_dict())
assert type(request_signage_point_or_end_of_subslot).from_json_dict(request_signage_point_or_end_of_subslot_json) == request_signage_point_or_end_of_subslot
assert (
type(new_signage_point_or_end_of_subslot).from_json_dict(new_signage_point_or_end_of_subslot_json)
== new_signage_point_or_end_of_subslot
)
assert str(request_signage_point_or_end_of_subslot_json) == str(
request_signage_point_or_end_of_subslot.to_json_dict()
)
assert (
type(request_signage_point_or_end_of_subslot).from_json_dict(request_signage_point_or_end_of_subslot_json)
== request_signage_point_or_end_of_subslot
)
assert str(respond_signage_point_json) == str(respond_signage_point.to_json_dict())
assert type(respond_signage_point).from_json_dict(respond_signage_point_json) == respond_signage_point
assert str(respond_end_of_subslot_json) == str(respond_end_of_subslot.to_json_dict())
assert type(respond_end_of_subslot).from_json_dict(respond_end_of_subslot_json) == respond_end_of_subslot
assert str(request_mempool_transaction_json) == str(request_mempool_transaction.to_json_dict())
assert type(request_mempool_transaction).from_json_dict(request_mempool_transaction_json) == request_mempool_transaction
assert (
type(request_mempool_transaction).from_json_dict(request_mempool_transaction_json)
== request_mempool_transaction
)
assert str(new_compact_vdf_json) == str(new_compact_vdf.to_json_dict())
assert type(new_compact_vdf).from_json_dict(new_compact_vdf_json) == new_compact_vdf
assert str(request_compact_vdf_json) == str(request_compact_vdf.to_json_dict())
Expand Down Expand Up @@ -129,7 +139,10 @@ def test_protocol_json() -> None:
assert str(harvester_handhsake_json) == str(harvester_handhsake.to_json_dict())
assert type(harvester_handhsake).from_json_dict(harvester_handhsake_json) == harvester_handhsake
assert str(new_signage_point_harvester_json) == str(new_signage_point_harvester.to_json_dict())
assert type(new_signage_point_harvester).from_json_dict(new_signage_point_harvester_json) == new_signage_point_harvester
assert (
type(new_signage_point_harvester).from_json_dict(new_signage_point_harvester_json)
== new_signage_point_harvester
)
assert str(new_proof_of_space_json) == str(new_proof_of_space.to_json_dict())
assert type(new_proof_of_space).from_json_dict(new_proof_of_space_json) == new_proof_of_space
assert str(request_signatures_json) == str(request_signatures.to_json_dict())
Expand Down Expand Up @@ -175,14 +188,25 @@ def test_protocol_json() -> None:
assert str(new_peak_timelord_json) == str(new_peak_timelord.to_json_dict())
assert type(new_peak_timelord).from_json_dict(new_peak_timelord_json) == new_peak_timelord
assert str(new_unfinished_block_timelord_json) == str(new_unfinished_block_timelord.to_json_dict())
assert type(new_unfinished_block_timelord).from_json_dict(new_unfinished_block_timelord_json) == new_unfinished_block_timelord
assert (
type(new_unfinished_block_timelord).from_json_dict(new_unfinished_block_timelord_json)
== new_unfinished_block_timelord
)
assert str(new_infusion_point_vdf_json) == str(new_infusion_point_vdf.to_json_dict())
assert type(new_infusion_point_vdf).from_json_dict(new_infusion_point_vdf_json) == new_infusion_point_vdf
assert str(new_signage_point_vdf_json) == str(new_signage_point_vdf.to_json_dict())
assert type(new_signage_point_vdf).from_json_dict(new_signage_point_vdf_json) == new_signage_point_vdf
assert str(new_end_of_sub_slot_bundle_json) == str(new_end_of_sub_slot_bundle.to_json_dict())
assert type(new_end_of_sub_slot_bundle).from_json_dict(new_end_of_sub_slot_bundle_json) == new_end_of_sub_slot_bundle
assert (
type(new_end_of_sub_slot_bundle).from_json_dict(new_end_of_sub_slot_bundle_json) == new_end_of_sub_slot_bundle
)
assert str(request_compact_proof_of_time_json) == str(request_compact_proof_of_time.to_json_dict())
assert type(request_compact_proof_of_time).from_json_dict(request_compact_proof_of_time_json) == request_compact_proof_of_time
assert (
type(request_compact_proof_of_time).from_json_dict(request_compact_proof_of_time_json)
== request_compact_proof_of_time
)
assert str(respond_compact_proof_of_time_json) == str(respond_compact_proof_of_time.to_json_dict())
assert type(respond_compact_proof_of_time).from_json_dict(respond_compact_proof_of_time_json) == respond_compact_proof_of_time
assert (
type(respond_compact_proof_of_time).from_json_dict(respond_compact_proof_of_time_json)
== respond_compact_proof_of_time
)

0 comments on commit a87e8e3

Please sign in to comment.