Skip to content

Commit

Permalink
notebook: convert mypy outputs to pyright format
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <jos_verlinde@hotmail.com>
  • Loading branch information
Josverl committed Dec 28, 2023
1 parent 918fa6b commit e33eeac
Showing 1 changed file with 168 additions and 73 deletions.
241 changes: 168 additions & 73 deletions scripts/try_mypy.ipynb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": []
"source": [
"Expiriment to get mypy to play ball with the snippet testing strategy used for pyright\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 1,
"metadata": {},
"outputs": [
{
Expand All @@ -34,7 +34,7 @@
},
{
"cell_type": "code",
"execution_count": 73,
"execution_count": 2,
"metadata": {},
"outputs": [
{
Expand All @@ -55,7 +55,7 @@
"Building wheels for collected packages: micropython-stdlib-stubs\n",
" Building wheel for micropython-stdlib-stubs (pyproject.toml): started\n",
" Building wheel for micropython-stdlib-stubs (pyproject.toml): finished with status 'done'\n",
" Created wheel for micropython-stdlib-stubs: filename=micropython_stdlib_stubs-1.1.1a3-py3-none-any.whl size=121918 sha256=a58e4b00b24064de30b21a2029ff78a2038605e125990cf1f3bb13198b4e6908\n",
" Created wheel for micropython-stdlib-stubs: filename=micropython_stdlib_stubs-1.1.1a3-py3-none-any.whl size=121779 sha256=8f415fb4ae0d88647b3a36012816c46560f8a9854119a08f3380a3596691711a\n",
" Stored in directory: c:\\users\\josverl\\appdata\\local\\pip\\cache\\wheels\\dc\\bf\\64\\badaeca3b642752745d83261430f6134cd4a4216f273387121\n",
"Successfully built micropython-stdlib-stubs\n",
"Installing collected packages: micropython-stdlib-stubs, micropython-esp32-stubs\n",
Expand All @@ -76,109 +76,204 @@
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Found C:\\develop\\MyPython\\micropython-stubs\\tests\\quality_tests\\check_esp32\n",
"Removing typings/sys.pyi\n"
]
}
],
"outputs": [],
"source": [
"if check_path.exists():\n",
" print(f\"Found {check_path}\")\n",
"else:\n",
" Exception(f\"Path {check_path} not found\")\n",
"\n",
"for f in (\n",
" \"typings/sys.pyi\",\n",
" # below is only if the typings folder is checked by mypy - whichit should not do\n",
" # \"typings/stdlib/typing.pyi\",\n",
" # \"typings/stdlib/_collections_abc.pyi\",\n",
" # \"typings/stdlib/abc.pyi\",\n",
" # \"typings/stdlib/types.pyi\",\n",
" # \"typings/stdlib/typing_extensions.pyi\",\n",
" # \"typings/stdlib/_typeshed\",\n",
" # \"typings/stdlib/collections\",\n",
" # \"typings/stdlib/sys\",\n",
" # \"typings/stdlib/builtins.pyi\",\n",
"):\n",
" file = check_path / f\n",
" if file.exists():\n",
" print(f\"Removing {f}\")\n",
" if file.is_file():\n",
" file.unlink()\n",
" elif file.is_dir():\n",
" shutil.rmtree(file)\n",
" else:\n",
" print(f\"File {f} not found\")"
"def mypy_patch(check_path):\n",
" if not check_path.exists():\n",
" Exception(f\"Path {check_path} not found\")\n",
"\n",
" for f in (\"typings/sys.pyi\",):\n",
" file = check_path / f\n",
"\n",
" if file.exists():\n",
" print(f\"Removing {f}\")\n",
" if file.is_file():\n",
" file.unlink()\n",
"\n",
" elif file.is_dir():\n",
" shutil.rmtree(file)\n",
"\n",
" # else:\n",
"\n",
" # print(f\"File {f} not found\")"
]
},
{
"cell_type": "code",
"execution_count": 83,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running mypy in C:\\develop\\MyPython\\micropython-stubs\\tests\\quality_tests\\check_esp32\n",
"Command '['mypy', '-v']' returned non-zero exit status 1.\n"
]
}
],
"outputs": [],
"source": [
"import subprocess\n",
"\n",
"\n",
"def run_mypy(path: Path):\n",
" print(f\"Running mypy in {path}\")\n",
" cmd = [\"mypy\", \"-v\", \"./check_esp32.py\"]\n",
" mypy_patch(path)\n",
" cmd = [\"mypy\", \"-v\", \".\"]\n",
" cmd = [\"mypy\", \"--no-error-summary\", \"--no-color\", \".\"]\n",
" try:\n",
" output = subprocess.run(\n",
" cmd,\n",
" check=True,\n",
" # check=True,\n",
" cwd=path,\n",
" capture_output=True,\n",
" text=True,\n",
" shell=False,\n",
" )\n",
" print(output.stdout)\n",
" print(output.stderr)\n",
" return output.stdout\n",
" except subprocess.CalledProcessError as e:\n",
" print(e)\n",
"\n",
"\n",
"run_mypy(check_path)"
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C:\\develop\\MyPython\\micropython-stubs\\tests\\quality_tests\\check_esp32\\check_esp32.py:2: \u001b[1m\u001b[91merror:\u001b[0m Cannot find implementation or library stub for module named \u001b[0m\u001b[1m\"esp32\"\u001b[0m \u001b[0m\u001b[93m[import-not-found]\u001b[0m\n",
"C:\\develop\\MyPython\\micropython-stubs\\tests\\quality_tests\\check_esp32\\check_esp32.py:2: \u001b[94mnote:\u001b[0m See \u001b[4mhttps://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports\u001b[0m\u001b[0m\n",
"C:\\develop\\MyPython\\micropython-stubs\\tests\\quality_tests\\check_esp32\\check_esp32.py:14: \u001b[1m\u001b[91merror:\u001b[0m Cannot find implementation or library stub for module named \u001b[0m\u001b[1m\"machine\"\u001b[0m \u001b[0m\u001b[93m[import-not-found]\u001b[0m\n",
"\u001b[1m\u001b[91mFound 2 errors in 1 file (checked 1 source file)\u001b[0m\n"
"Running mypy in C:\\develop\\MyPython\\micropython-stubs\\tests\\quality_tests\\check_esp32\n"
]
}
],
"source": [
"\n",
"# run mypy\n",
"!cd {check_path}\n",
"# !mypy {check_path}\n",
"!mypy {check_path}/check_esp32.py"
"raw_results = run_mypy(check_path)\n",
"results = raw_results.split(\"\\n\")\n",
"# errors = []\n",
"# for i, line in enumerate(results):\n",
"# if \"error:\" in line:\n",
"# errors.append(line)\n",
"# elif \"warning\" in line:\n",
"# errors.append(line)\n",
"\n",
"# # print results\n",
"# for line in errors:\n",
"# print(line)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# now parse to json and save\n",
"# https://github.com/python/mypy/issues/10816\n",
"\n",
"# mypy program.py | mypy-gitlab-code-quality"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# # convert from mypy to gitlab code quality format\n",
"# from mypy_gitlab_code_quality import generate_report, parse_issue\n",
"\n",
"# gitlab_report = generate_report(map(str.rstrip, results))\n",
"\n",
"# gitlab_report"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# convert from gitlab to pyright format\n",
"\n",
"import json\n",
"HEADER = \"\"\"\n",
"{\n",
" \"version\": \"\",\n",
" \"time\": \"\",\n",
" \"generalDiagnostics\": [],\n",
" \"summary\": {\n",
" \"filesAnalyzed\": -1,\n",
" \"errorCount\": -1,\n",
" \"warningCount\": -1,\n",
" \"informationCount\": -1,\n",
" \"timeInSec\": -1\n",
" }\n",
"}\n",
"\"\"\"\n",
"DIAGNOSTIC = \"\"\"\n",
"{\n",
" \"file\": \"\",\n",
" \"severity\": \"\",\n",
" \"message\": \"\",\n",
" \"rule\": \"\",\n",
" \"range\": {\n",
" \"start\": {\n",
" \"line\": -1,\n",
" \"character\": -1\n",
" },\n",
" \"end\": {\n",
" \"line\": -1,\n",
" \"character\": -1\n",
" }\n",
" }\n",
"}\n",
"\"\"\"\n",
"\n",
"def gitlab_to_pyright(report):\n",
" pyright_report = json.loads(HEADER)\n",
" pyright_report[\"version\"] = \"1.0\"\n",
" pyright_report[\"generalDiagnostics\"] = []\n",
" for issue in report:\n",
" i = json.loads(DIAGNOSTIC)\n",
" i[\"file\"] = issue[\"location\"][\"path\"]\n",
" i[\"severity\"] = issue[\"severity\"] # TODO: map to pyright severity\n",
" i[\"message\"] = issue[\"description\"]\n",
" i[\"rule\"] = issue[\"check_name\"]\n",
" i[\"range\"][\"start\"][\"line\"] = issue[\"location\"][\"lines\"][\"begin\"]\n",
" pyright_report[\"generalDiagnostics\"].append(i)\n",
"\n",
" pyright_report[\"summary\"][\"errorCount\"] = len(pyright_report[\"generalDiagnostics\"])\n",
" return pyright_report\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'version': '1.0',\n",
" 'time': '',\n",
" 'generalDiagnostics': [{'file': 'check_aioespnow.py',\n",
" 'severity': <Severity.major: 'major'>,\n",
" 'message': 'Cannot instantiate abstract class \"AIOESPNow\" with abstract attribute \"__next__\"',\n",
" 'rule': 'abstract',\n",
" 'range': {'start': {'line': 14, 'character': -1},\n",
" 'end': {'line': -1, 'character': -1}}}],\n",
" 'summary': {'filesAnalyzed': -1,\n",
" 'errorCount': 1,\n",
" 'warningCount': -1,\n",
" 'informationCount': -1,\n",
" 'timeInSec': -1}}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from mypy_gitlab_code_quality import generate_report as gitlab_report\n",
"gitlab_to_pyright(gitlab_report(map(str.rstrip, results)))"
]
},
{
Expand Down

0 comments on commit e33eeac

Please sign in to comment.