Skip to content

Commit

Permalink
Basic CI type checking
Browse files Browse the repository at this point in the history
Uses 'standard' rules
    + reportMissingTypeArgument
    + reportMissingParameterType
    - reportMissingImports
shows a table with count and change per error type and per file
fails CI if any file increased in the number of errors
lowered cpu and ram requirements for pyright, lint, docs jobs

Internal-tag: [#63605]
Signed-off-by: Grzegorz Kierzkowski <gkierzkowski@internships.antmicro.com>
  • Loading branch information
gkierzkowski-ant committed Aug 26, 2024
1 parent 9cd1837 commit 21dd6e8
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 5 deletions.
16 changes: 16 additions & 0 deletions .ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ variables:
SCALENODE_CPU: 6

lint:
variables:
SCALENODE_RAM: 2048
SCALENODE_CPU: 2
stage: test
image: debian:bookworm
script:
Expand Down Expand Up @@ -47,7 +50,20 @@ package_cores:
paths:
- core_repo/**

pyright_check:
variables:
SCALENODE_RAM: 12288
SCALENODE_CPU: 2
stage: test
image: debian:bookworm
script:
- ./.github/scripts/ci.sh pyright_check
allow_failure: true

build_docs:
variables:
SCALENODE_RAM: 2048
SCALENODE_CPU: 2
image: debian:bookworm
stage: build_docs
script:
Expand Down
19 changes: 17 additions & 2 deletions .github/scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ install_common_system_packages() {
git \
python3-dev \
python3-pip \
python3-venv
python3-venv \
nodejs \
npm
end_command_group
}

Expand Down Expand Up @@ -186,6 +188,16 @@ package_dist() {
end_command_group
}

pyright_check(){
install_common_system_packages
install_topwrap_system_deps
install_nox

begin_command_group "checking types"
log_cmd nox -s pyright_check -- compare
end_command_group
}

case "$1" in
lint)
run_lint
Expand All @@ -202,10 +214,13 @@ package_cores)
package_dist)
package_dist
;;
pyright_check)
pyright_check
;;
docs)
generate_docs
;;
*)
echo "Usage: $0 {lint|tests|examples|package_cores|package_dist|docs}"
echo "Usage: $0 {lint|tests|examples|package_cores|package_dist|docs|pyright_check}"
;;
esac
21 changes: 21 additions & 0 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ jobs:
run: |
./.github/scripts/ci.sh lint
typing_test:
runs-on: ubuntu-latest
container:
image: debian:bookworm
name: "Test typing"

steps:
- name: Install git package
run: |
apt-get update -qq
apt-get install -y git
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Run typing checks
continue-on-error: true
run: |
./.github/scripts/ci.sh pyright_check
tests:
runs-on: ubuntu-latest
container:
Expand Down
82 changes: 81 additions & 1 deletion noxfile.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# Copyright (c) 2023-2024 Antmicro <www.antmicro.com>
# SPDX-License-Identifier: Apache-2.0

import json
import os
import shutil
from collections import defaultdict
from pathlib import Path, PurePath
from tempfile import TemporaryDirectory, TemporaryFile
from typing import Any, Dict, List, Tuple

import nox
from nox.command import CommandFailed

PYTHON_VERSIONS = ["3.8", "3.9", "3.10", "3.11", "3.12"]

Expand Down Expand Up @@ -118,8 +123,83 @@ def _install_test(session: nox.Session) -> None:


@nox.session
def doc_gen(session) -> None:
def doc_gen(session: nox.Session) -> None:
session.install(".[docs]")
session.run("make", "-C", "docs", "html", external=True)
session.run("make", "-C", "docs", "latexpdf", external=True)
session.run("cp", "docs/build/latex/topwrap.pdf", "docs/build/html", external=True)


@nox.session
def pyright_check(session: nox.Session) -> None:
# this is a wrapper for _pyright_check that installs dependencies
session.install(".[topwrap-parse]")
session.install(".[tests]")
compare_with_main = "compare" in session.posargs

if compare_with_main:
session.run("nox", "-s", "_pyright_check", "--", "compare")
else:
session.run("nox", "-s", "_pyright_check")


@nox.session
def _pyright_check(session: nox.Session) -> None:
# this is not supposed to be called outright, use `pyright_check`
compare_with_main = "compare" in session.posargs

# counting down errors on branch
def count_down_errors() -> Tuple[Dict[str, Any], Dict[str, Any]]:
errortypes = defaultdict(int)
errorfiles = defaultdict(int)
with TemporaryFile() as f:
session.run("pyright", "--outputjson", stdout=f, success_codes=[0, 1], external=True)
f.seek(0)
errors_data = json.load(f)
for errno in errors_data["generalDiagnostics"]:
errortypes[errno["rule"]] += 1
errorfiles[str(Path(errno["file"]).relative_to(Path(".").resolve()))] += 1
return (errortypes, errorfiles)

errortypes, errorfiles = count_down_errors()

errortypes_main = defaultdict(int)
errorfiles_main = defaultdict(int)
if compare_with_main:
# save location of used config
pyproject_origin = Path(os.getcwd(), "pyproject.toml")

with TemporaryDirectory() as dir:
# copy into temp dir and go into it
shutil.copytree(Path("."), dir, dirs_exist_ok=True)
with session.chdir(Path(dir)):
# switch to main and replace pyproject
session.run("git", "switch", "main", "--force", external=True)
session.run("rm", "pyproject.toml", external=True)
shutil.copy(pyproject_origin, dir)

# counting down errors on main
errortypes_main, errorfiles_main = count_down_errors()

# human readable pyright output
session.run("pyright", success_codes=[0, 1], external=True)

columns = 3 if compare_with_main else 2

from prettytable import PrettyTable

def print_table(
header: List[str], columns: int, errtypes: Dict[str, int], errtypes_main: Dict[str, int]
) -> None:
t = PrettyTable(header[:columns])
for errtype, num in sorted(errtypes.items(), key=lambda x: x[1], reverse=True):
t.add_row([errtype, num, num - errtypes_main[errtype]][:columns])
print(t)

print_table(["Error", "Count", "Change"], columns, errortypes, errortypes_main)
print_table(["File", "Errors", "Change"], columns, errorfiles, errorfiles_main)

if compare_with_main:
for errtype, num in errorfiles.items():
if num - errorfiles_main[errtype] > 0:
raise CommandFailed()
11 changes: 10 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies = [
"pygtrie",
"pipeline_manager_backend_communication @ git+https://github.com/antmicro/kenning-pipeline-manager-backend-communication@eb690cfb7766bfbd85a4eff2a1e809573b8b72d0",
"pipeline_manager @ git+https://github.com/antmicro/kenning-pipeline-manager@f45289d7073e885b5e3f461db9a8667807b69380",
"soc_generator @ git+https://github.com/antmicro/soc-generator@76ac5b3743bea1bb6e5d2ac7ecae31ab39d5e8b3"
"soc_generator @ git+https://github.com/antmicro/soc-generator@76ac5b3743bea1bb6e5d2ac7ecae31ab39d5e8b3",
]

[project.optional-dependencies]
Expand All @@ -51,6 +51,8 @@ tests = [
"pytest-lazy-fixtures==1.0.1",
"pyfakefs==5.3.5",
"deepdiff==7.0.1",
"pyright",
"prettytable",
]
deploy = [
"build==1.2.1"
Expand Down Expand Up @@ -114,3 +116,10 @@ skip = [
[tool.codespell]
skip = "build,venv,cov_html,./tests/data,.git,.nox,__pycache__"
ignore-words-list = "convertor, inout, inouts"

[tool.pyright]
include = ["topwrap"]
reportMissingImports = false
typeCheckingMode = "standard"
reportMissingTypeArgument = true
reportMissingParameterType = true
2 changes: 1 addition & 1 deletion topwrap/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def parse_main(use_yosys, iface_deduce, iface, dest_dir, log_level, files):
help="Specify directory name for output files",
)
@click.argument("yamlfiles", type=click_r_file, nargs=-1)
def kpm_client_main(host, port, log_level, design, yamlfiles, build_dir):
def kpm_client_main(host, port, log_level, design, yamlfiles: list[str], build_dir):
configure_log_level(log_level)

logging.info("Starting kenning pipeline manager client")
Expand Down

0 comments on commit 21dd6e8

Please sign in to comment.