Skip to content

Commit

Permalink
Switch from pylint to ruff (#5270)
Browse files Browse the repository at this point in the history
  • Loading branch information
achamayou authored May 17, 2023
1 parent 471e514 commit c93dd7a
Show file tree
Hide file tree
Showing 25 changed files with 84 additions and 72 deletions.
2 changes: 2 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
extend-exclude = ["*_pb2*.py"]
line-length = 2000
2 changes: 1 addition & 1 deletion python/ccf/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ def add_transaction(self, transaction):
elif self.service_status == "Open":
assert updated_status in ["Recovering"], updated_status
else:
assert self.service_status == None, self.service_status
assert self.service_status is None, self.service_status
self.service_status = updated_status

# Checks complete, add this transaction to tree
Expand Down
22 changes: 11 additions & 11 deletions python/ccf/ledger_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ def main():
else None,
)

l = DefaultLiner(args.write_views, args.split_views, args.split_services)
l.help()
liner = DefaultLiner(args.write_views, args.split_views, args.split_services)
liner.help()
current_service_identity = None
for chunk in ledger:
for tx in chunk:
Expand All @@ -163,33 +163,33 @@ def main():
seqno = tx.gcm_header.seqno
if not has_private:
if ccf.ledger.SIGNATURE_TX_TABLE_NAME in public:
l.entry("Signature", view, seqno)
liner.entry("Signature", view, seqno)
else:
if all(
table.startswith("public:ccf.internal.") for table in public
):
l.entry("Internal", view, seqno)
liner.entry("Internal", view, seqno)
elif any(table.startswith("public:ccf.gov.") for table in public):
service_info = try_get_service_info(public)
if service_info is None:
l.entry("Governance", view, seqno)
liner.entry("Governance", view, seqno)
elif service_info["status"] == "Opening":
l.entry("New Service", view, seqno)
liner.entry("New Service", view, seqno)
current_service_identity = service_info["cert"]
elif service_info["status"] == "Recovering":
l.entry("Recovering Service", view, seqno)
liner.entry("Recovering Service", view, seqno)
current_service_identity = service_info["cert"]
elif (
service_info["cert"] == current_service_identity
and service_info["status"] == "Open"
):
l.entry("Service Open", view, seqno)
liner.entry("Service Open", view, seqno)
else:
l.entry("User Public", view, seqno)
liner.entry("User Public", view, seqno)
else:
l.entry("User Private", view, seqno)
liner.entry("User Private", view, seqno)

l.flush()
liner.flush()


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions python/ccf/read_ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ def print_key(key, table_name, tables_format_rules, indent_s, is_removed=False):
LOG.info(f"{indent_s}{k}:")


def counted_string(l, name):
return f"{len(l)} {name}{'s' * bool(len(l) != 1)}"
def counted_string(string, name):
return f"{len(string)} {name}{'s' * bool(len(string) != 1)}"


def dump_entry(entry, table_filter, tables_format_rules):
Expand Down
3 changes: 1 addition & 2 deletions python/ledger_tutorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from loguru import logger as LOG
import json
import random
import ccf.ledger

# Change default log format
LOG.remove()
Expand All @@ -19,12 +20,10 @@

ledger_dirs = sys.argv[1:]


# Because all ledger files are closed and are no longer being
# written to, it is safe to read all of them, even those that may
# contain uncommitted transactions.
# SNIPPET_START: create_ledger
import ccf.ledger

ledger = ccf.ledger.Ledger(ledger_dirs, committed_only=False)
# SNIPPET_END: create_ledger
Expand Down
4 changes: 2 additions & 2 deletions scripts/ci-checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fi

source scripts/env/bin/activate
pip install -U pip
pip install -U wheel black pylint mypy 1>/dev/null
pip install -U wheel black pylint mypy ruff 1>/dev/null
endgroup

group "Python format"
Expand All @@ -119,7 +119,7 @@ pip install -U -r python/requirements.txt 1>/dev/null
endgroup

group "Python lint"
PYTHONPATH=./tests git ls-files tests/ python/ | grep -e '\.py$' | xargs python -m pylint --ignored-modules "*_pb2"
ruff check python/ tests/
endgroup

group "Python types"
Expand Down
2 changes: 1 addition & 1 deletion tests/ca_certs.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_cert_store(network, args):
network.get_ledger_public_state_at(remove_proposal.completed_seqno)[
"public:ccf.gov.tls.ca_cert_bundles"
][raw_cert_name]
== None
is None
), "CA bundle was not removed"

return network
18 changes: 9 additions & 9 deletions tests/e2e_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1126,15 +1126,15 @@ def test_forwarding_frontends(network, args):
ack = network.consortium.get_any_active_member().ack(backup)
check_commit(ack)
except AckException as e:
assert args.http2 == True
assert args.http2 is True
assert e.response.status_code == http.HTTPStatus.NOT_IMPLEMENTED
r = e.response.body.json()
assert (
r["error"]["message"]
== "Request cannot be forwarded to primary on HTTP/2 interface."
), r
else:
assert args.http2 == False
assert args.http2 is False

try:
msg = "forwarded_msg"
Expand All @@ -1148,15 +1148,15 @@ def test_forwarding_frontends(network, args):
msg=msg,
)
except infra.logging_app.LoggingTxsIssueException as e:
assert args.http2 == True
assert args.http2 is True
assert e.response.status_code == http.HTTPStatus.NOT_IMPLEMENTED
r = e.response.body.json()
assert (
r["error"]["message"]
== "Request cannot be forwarded to primary on HTTP/2 interface."
), r
else:
assert args.http2 == False
assert args.http2 is False

if args.package == "samples/apps/logging/liblogging" and not args.http2:
with backup.client("user0") as c:
Expand Down Expand Up @@ -1617,7 +1617,7 @@ def test_post_local_commit_failure(network, args):
"/app/log/private/anonymous/v2?fail=false", {"id": 100, "msg": "hello"}
)
assert r.status_code == http.HTTPStatus.OK.value, r.status_code
assert r.body.json()["success"] == True
assert r.body.json()["success"] is True
TxID.from_str(r.body.json()["tx_id"])

r = c.post(
Expand Down Expand Up @@ -1720,17 +1720,17 @@ def test_basic_constraints(network, args):
basic_constraints = ca_cert.extensions.get_extension_for_oid(
ObjectIdentifier("2.5.29.19")
)
assert basic_constraints.critical == True
assert basic_constraints.value.ca == True
assert basic_constraints.critical is True
assert basic_constraints.value.ca is True
assert basic_constraints.value.path_length == 0

node_pem = primary.get_tls_certificate_pem()
node_cert = load_pem_x509_certificate(node_pem.encode(), default_backend())
basic_constraints = node_cert.extensions.get_extension_for_oid(
ObjectIdentifier("2.5.29.19")
)
assert basic_constraints.critical == True
assert basic_constraints.value.ca == False
assert basic_constraints.critical is True
assert basic_constraints.value.ca is False


def run_udp_tests(args):
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def test_save_committed_ledger_files(network, args):
LOG.info(f"Moving committed ledger files to {args.common_read_only_ledger_dir}")
primary, _ = network.find_primary()
for ledger_dir in primary.remote.ledger_paths():
for l in os.listdir(ledger_dir):
if infra.node.is_file_committed(l):
for ledger_file_path in os.listdir(ledger_dir):
if infra.node.is_file_committed(ledger_file_path):
shutil.move(
os.path.join(ledger_dir, l),
os.path.join(args.common_read_only_ledger_dir, l),
os.path.join(ledger_dir, ledger_file_path),
os.path.join(args.common_read_only_ledger_dir, ledger_file_path),
)

network.txs.verify(network)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def do_historical(self, table, request, response):
response.body = e.details().encode()
return

if result.retry == True:
if result.retry is True:
response.status_code = HTTP.HttpStatusCode.ACCEPTED
response.body = "Historical transaction is not currently available. Please retry.".encode()
return
Expand Down
4 changes: 2 additions & 2 deletions tests/external_executor/executors/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(self, executor):
self.activated_event = None

def start(self):
assert self.thread == None, "Already started"
assert self.thread is None, "Already started"
LOG.info("Starting executor")
self.activated_event = threading.Event()
self.thread = threading.Thread(
Expand All @@ -25,7 +25,7 @@ def start(self):
), "Executor failed to activate after 3 seconds"

def terminate(self):
assert self.thread != None, "Already terminated"
assert self.thread is not None, "Already terminated"
LOG.info("Terminating executor")
self.executor.terminate()
self.thread.join()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def _execute_update_cache(self, kv_stub, request, response):
prefix = "/update_cache/"
title = request.uri[len(prefix) :]
description = self._get_description(title)
if description == None:
if description is None:
response.status_code = HTTP.HttpStatusCode.BAD_GATEWAY
response.body = f"Error when fetching article with title '{title}'".encode(
"utf-8"
Expand Down
7 changes: 4 additions & 3 deletions tests/governance.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,10 @@ def test_desc(s):

test_desc("Logging levels of governance operations")
consortium = network.consortium
rand_hex = lambda: md5(
random.getrandbits(32).to_bytes(4, "big")
).hexdigest()

def rand_hex():
return md5(random.getrandbits(32).to_bytes(4, "big")).hexdigest()

validate_info = f"Logged at info during validate: {rand_hex()}"
validate_warn = f"Logged at warn during validate: {rand_hex()}"
validate_js = (
Expand Down
2 changes: 1 addition & 1 deletion tests/governance_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def check_signatures(ledger):
sig_txid = TxID(tr.gcm_header.view, tr.gcm_header.seqno)

# Adjacent signatures only occur on a view change
if prev_sig_txid != None:
if prev_sig_txid is not None:
if prev_sig_txid.seqno + 1 == sig_txid.seqno:
# Reduced from assert while investigating cause
# https://github.com/microsoft/CCF/issues/5078
Expand Down
8 changes: 4 additions & 4 deletions tests/infra/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def request(
nf.write(msg_bytes)
nf.flush()
content_path = f"@{nf.name}"
if not "content-type" in headers and request.body:
if "content-type" not in headers and request.body:
headers["content-type"] = content_type

cmd = ["curl"]
Expand Down Expand Up @@ -636,7 +636,7 @@ def request(
request_body = json.dumps(request.body).encode()
content_type = CONTENT_TYPE_JSON

if not "content-type" in request.headers and len(request.body) > 0:
if "content-type" not in request.headers and len(request.body) > 0:
extra_headers["content-type"] = content_type

if self.cose_signing_auth is not None and request.http_verb != "GET":
Expand Down Expand Up @@ -837,10 +837,10 @@ def request(
content_type = CONTENT_TYPE_JSON
content_length = len(request_body)

if not "content-type" in request.headers and len(request.body) > 0:
if "content-type" not in request.headers and len(request.body) > 0:
extra_headers["content-type"] = content_type

if not "content-length" in extra_headers:
if "content-length" not in extra_headers:
extra_headers["content-length"] = content_length

if self.signing_details is not None:
Expand Down
4 changes: 2 additions & 2 deletions tests/infra/consortium.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ def get_active_non_recovery_members(self):

def get_any_active_member(self, recovery_member=None):
if recovery_member is not None:
if recovery_member == True:
if recovery_member is True:
return random.choice(self.get_active_recovery_members())
elif recovery_member == False:
elif recovery_member is False:
return random.choice(self.get_active_non_recovery_members())
else:
return random.choice(self.get_active_members())
Expand Down
12 changes: 6 additions & 6 deletions tests/infra/logging_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ class LoggingTxsVerifyException(Exception):
"""


def sample_list(l, n):
if n > len(l):
def sample_list(list_, n):
if n > len(list_):
# Return all elements
return l
return list_
elif n == 0:
return []
elif n == 1:
# Return last element only
return l[-1:]
return list_[-1:]
elif n == 2:
# Return first and last elements
return l[:1] + l[-1:]
return list_[:1] + list_[-1:]
else:
# Return first, last, and random sample of values in-between
return l[:1] + random.sample(l[1:-1], n - 2) + l[-1:]
return list_[:1] + random.sample(list_[1:-1], n - 2) + list_[-1:]


class LoggingTxs:
Expand Down
2 changes: 1 addition & 1 deletion tests/infra/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ def wait_for_all_nodes_to_commit(self, primary=None, tx_id=None, timeout=10):
end_time = time.time() + timeout

# If no TxID is specified, retrieve latest readable one
if tx_id == None:
if tx_id is None:
while time.time() < end_time:
with primary.client() as c:
resp = c.get("/node/state") # Well-known read-only endpoint
Expand Down
4 changes: 2 additions & 2 deletions tests/infra/piccolo_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import json

sys.path.insert(0, "../tests/perf-system/generator")
import generator
import generator # noqa: E402

sys.path.insert(0, "../tests/perf-system/analyzer")
import analyzer
import analyzer # noqa: E402


def get_command_args(args, network, get_command):
Expand Down
4 changes: 2 additions & 2 deletions tests/infra/remote_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ def stop(self):
remote_file_dst = f"{self.name}_{csv}"
self.remote.get(csv, self.common_dir, 1, remote_file_dst)
if csv == "perf_summary.csv":
with open("perf_summary.csv", "a", encoding="utf-8") as l:
with open("perf_summary.csv", "a", encoding="utf-8") as csvfd:
with open(
os.path.join(self.common_dir, remote_file_dst),
"r",
encoding="utf-8",
) as r:
l.write(r.read())
csvfd.write(r.read())

def check_done(self):
return self.remote.check_done()
Expand Down
Loading

0 comments on commit c93dd7a

Please sign in to comment.