potiuk commented on code in PR #30775: URL: https://github.com/apache/airflow/pull/30775#discussion_r1174320718
########## scripts/ci/pre_commit/pre_commit_unittest_testcase.py: ########## @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import ast +import pathlib +import sys + + +def check_test_file(file: str) -> int: + node = ast.parse(pathlib.Path(file).read_text("utf-8"), file) + + found = 0 + classes = [c for c in node.body if isinstance(c, ast.ClassDef)] + for c in classes: + # Some classes are returned as an ast.Attribute, some as an ast.Name object. Not quite sure why + parent_classes = [base.attr for base in c.bases if isinstance(base, ast.Attribute)] + parent_classes.extend([base.id for base in c.bases if isinstance(base, ast.Name)]) Review Comment: FYI. In general, this is not a complete to find out if your class derives from a TestCase or not. However possibly it is good-enough. The cases that are not handled (potentially) with this code: * TestCase **might** be another TestCase not necessarily the unit TestCase - it could be "airflow.test_util.TestCase" - in order to find out if this is the real Test Case, we would have to walk the tree likely and find out where it has been imported from * The parent class could be transitively deriving from TestCase and this code would not have found it. So Test Case might be your grand-parent and this case is not handled by this code. HOWEVER. I think it's good-enough :) : * we can assume any 'TestCase` is bad - and keep it as convention (never use TestCase as your base test class name) * we can assume that even if we have "grand-parent" Test Case, then the parent of ours is also checked during the pre-commit and it will be the parent that will fail. This would not be a correct if the parent class is outside of the "tests" directory, but let's asume it will never happen Unless of course you would like to dig deeper and make it "perfect" :) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
