From e73f88d398448791a68980e4e1d62000f80ab2e8 Mon Sep 17 00:00:00 2001 From: David Rheinsberg Date: Mon, 20 Apr 2020 08:20:48 +0200 Subject: [PATCH] test: make ast check pylint conforming A few adjustments to the AST check: * Avoid local imports. Move the AST import to the head of the file. We will get warnings if it is unused, so it should not get stale there once we drop the AST hack. * Avoid `TODO:` since pylint will parse it (parsing comments, yay!) * Use `isinstance()` for type-checks. --- test/test_stageinfo.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_stageinfo.py b/test/test_stageinfo.py index 4e970c0d..8a9321ab 100644 --- a/test/test_stageinfo.py +++ b/test/test_stageinfo.py @@ -1,3 +1,4 @@ +import ast import json import unittest from pathlib import Path @@ -13,18 +14,17 @@ class TestStageInfo(unittest.TestCase): @staticmethod def get_stage_info(stage: Path) -> dict: '''Return the STAGE_* variables from the given stage.''' - # TODO: This works for now, but stages should probably have some + # NOTE: This works for now, but stages should probably have some # standard way of dumping this info so we (and other higher-level # tools) don't have to parse the code and walk through the AST # to find these values. - import ast stage_info = {} with open(stage) as fobj: stage_ast = ast.parse(fobj.read(), filename=stage) # STAGE_* assignments are at the toplevel, no need to walk() for node in stage_ast.body: - if type(node) is ast.Assign and type(node.value) == ast.Str: + if isinstance(node, ast.Assign) and isinstance(node.value, ast.Str): for target in node.targets: if target.id.startswith("STAGE_"): stage_info[target.id] = node.value.s @@ -55,7 +55,7 @@ class TestStageInfo(unittest.TestCase): stage_opts = self.parse_stage_opts(stage_info["STAGE_OPTS"]) self.assertIsNotNone(stage_opts) - # TODO: we probably want an actual JSON Schema validator but + # NOTE: We probably want an actual JSON Schema validator but # a nicely basic sanity test for our current STAGE_OPTS is: # 1. If it's not empty, there should be a "properties" object, # 2. If "required" exists, each item should be a property name