|
| 1 | +import ast |
| 2 | +from flake8_baserow.ai_imports import BaserowAIImportsChecker |
| 3 | + |
| 4 | + |
| 5 | +def run_checker(code: str): |
| 6 | + """Helper to run the checker on code and return errors.""" |
| 7 | + tree = ast.parse(code) |
| 8 | + checker = BaserowAIImportsChecker(tree, "test.py") |
| 9 | + return list(checker.run()) |
| 10 | + |
| 11 | + |
| 12 | +def test_global_dspy_import(): |
| 13 | + """Test that global dspy imports are flagged.""" |
| 14 | + code = """ |
| 15 | +import dspy |
| 16 | +""" |
| 17 | + errors = run_checker(code) |
| 18 | + assert len(errors) == 1 |
| 19 | + assert "BAI001" in errors[0][2] |
| 20 | + assert "dspy" in errors[0][2] |
| 21 | + |
| 22 | + |
| 23 | +def test_global_litellm_import(): |
| 24 | + """Test that global litellm imports are flagged.""" |
| 25 | + code = """ |
| 26 | +import litellm |
| 27 | +""" |
| 28 | + errors = run_checker(code) |
| 29 | + assert len(errors) == 1 |
| 30 | + assert "BAI001" in errors[0][2] |
| 31 | + assert "litellm" in errors[0][2] |
| 32 | + |
| 33 | + |
| 34 | +def test_global_dspy_from_import(): |
| 35 | + """Test that global 'from dspy import' statements are flagged.""" |
| 36 | + code = """ |
| 37 | +from dspy import ChainOfThought |
| 38 | +from dspy.predict import Predict |
| 39 | +""" |
| 40 | + errors = run_checker(code) |
| 41 | + assert len(errors) == 2 |
| 42 | + assert all("BAI001" in error[2] for error in errors) |
| 43 | + |
| 44 | + |
| 45 | +def test_global_litellm_from_import(): |
| 46 | + """Test that global 'from litellm import' statements are flagged.""" |
| 47 | + code = """ |
| 48 | +from litellm import completion |
| 49 | +from litellm.utils import get_llm_provider |
| 50 | +""" |
| 51 | + errors = run_checker(code) |
| 52 | + assert len(errors) == 2 |
| 53 | + assert all("BAI001" in error[2] for error in errors) |
| 54 | + |
| 55 | + |
| 56 | +def test_local_import_in_function(): |
| 57 | + """Test that local imports within functions are allowed.""" |
| 58 | + code = """ |
| 59 | +def my_function(): |
| 60 | + import dspy |
| 61 | + import litellm |
| 62 | + from dspy import ChainOfThought |
| 63 | + from litellm import completion |
| 64 | + return dspy, litellm |
| 65 | +""" |
| 66 | + errors = run_checker(code) |
| 67 | + assert len(errors) == 0 |
| 68 | + |
| 69 | + |
| 70 | +def test_local_import_in_method(): |
| 71 | + """Test that local imports within class methods are allowed.""" |
| 72 | + code = """ |
| 73 | +class MyClass: |
| 74 | + def my_method(self): |
| 75 | + import dspy |
| 76 | + from litellm import completion |
| 77 | + return dspy.ChainOfThought() |
| 78 | +""" |
| 79 | + errors = run_checker(code) |
| 80 | + assert len(errors) == 0 |
| 81 | + |
| 82 | + |
| 83 | +def test_local_import_in_async_function(): |
| 84 | + """Test that local imports within async functions are allowed.""" |
| 85 | + code = """ |
| 86 | +async def my_async_function(): |
| 87 | + import dspy |
| 88 | + from litellm import acompletion |
| 89 | + return await acompletion() |
| 90 | +""" |
| 91 | + errors = run_checker(code) |
| 92 | + assert len(errors) == 0 |
| 93 | + |
| 94 | + |
| 95 | +def test_mixed_global_and_local_imports(): |
| 96 | + """Test that global imports are flagged while local imports are not.""" |
| 97 | + code = """ |
| 98 | +import dspy # This should be flagged |
| 99 | +
|
| 100 | +def my_function(): |
| 101 | + import litellm # This should be OK |
| 102 | + return litellm.completion() |
| 103 | +
|
| 104 | +from dspy import ChainOfThought # This should be flagged |
| 105 | +""" |
| 106 | + errors = run_checker(code) |
| 107 | + assert len(errors) == 2 |
| 108 | + assert all("BAI001" in error[2] for error in errors) |
| 109 | + |
| 110 | + |
| 111 | +def test_nested_function_imports(): |
| 112 | + """Test that imports in nested functions are allowed.""" |
| 113 | + code = """ |
| 114 | +def outer_function(): |
| 115 | + def inner_function(): |
| 116 | + import dspy |
| 117 | + from litellm import completion |
| 118 | + return dspy, completion |
| 119 | + return inner_function() |
| 120 | +""" |
| 121 | + errors = run_checker(code) |
| 122 | + assert len(errors) == 0 |
| 123 | + |
| 124 | + |
| 125 | +def test_other_imports_not_affected(): |
| 126 | + """Test that other imports are not flagged.""" |
| 127 | + code = """ |
| 128 | +import os |
| 129 | +import sys |
| 130 | +from typing import List |
| 131 | +from baserow.core.models import User |
| 132 | +""" |
| 133 | + errors = run_checker(code) |
| 134 | + assert len(errors) == 0 |
| 135 | + |
| 136 | + |
| 137 | +def test_multiple_global_imports(): |
| 138 | + """Test multiple global AI imports.""" |
| 139 | + code = """ |
| 140 | +import dspy |
| 141 | +import litellm |
| 142 | +from dspy import ChainOfThought |
| 143 | +from litellm import completion |
| 144 | +import os # This should not be flagged |
| 145 | +""" |
| 146 | + errors = run_checker(code) |
| 147 | + assert len(errors) == 4 |
| 148 | + assert all("BAI001" in error[2] for error in errors) |
| 149 | + |
| 150 | + |
| 151 | +def test_import_with_alias(): |
| 152 | + """Test that imports with aliases are also caught.""" |
| 153 | + code = """ |
| 154 | +import dspy as d |
| 155 | +import litellm as llm |
| 156 | +
|
| 157 | +def my_function(): |
| 158 | + import dspy as local_d |
| 159 | + return local_d |
| 160 | +""" |
| 161 | + errors = run_checker(code) |
| 162 | + assert len(errors) == 2 |
| 163 | + assert all("BAI001" in error[2] for error in errors) |
| 164 | + |
| 165 | + |
| 166 | +def test_submodule_imports(): |
| 167 | + """Test that submodule imports are caught at global scope.""" |
| 168 | + code = """ |
| 169 | +from dspy.teleprompt import BootstrapFewShot |
| 170 | +from litellm.utils import token_counter |
| 171 | +
|
| 172 | +def my_function(): |
| 173 | + from dspy.predict import Predict |
| 174 | + from litellm.integrations import log_event |
| 175 | + return Predict, log_event |
| 176 | +""" |
| 177 | + errors = run_checker(code) |
| 178 | + assert len(errors) == 2 |
| 179 | + assert all("BAI001" in error[2] for error in errors) |
| 180 | + # Verify the errors are for the global imports |
| 181 | + assert errors[0][0] == 2 # Line number of first import |
| 182 | + assert errors[1][0] == 3 # Line number of second import |
| 183 | + |
| 184 | + |
| 185 | +def test_class_method_and_staticmethod(): |
| 186 | + """Test that imports in classmethods and staticmethods are allowed.""" |
| 187 | + code = """ |
| 188 | +class MyClass: |
| 189 | + @classmethod |
| 190 | + def my_classmethod(cls): |
| 191 | + import dspy |
| 192 | + return dspy |
| 193 | +
|
| 194 | + @staticmethod |
| 195 | + def my_staticmethod(): |
| 196 | + from litellm import completion |
| 197 | + return completion |
| 198 | +""" |
| 199 | + errors = run_checker(code) |
| 200 | + assert len(errors) == 0 |
| 201 | + |
| 202 | + |
| 203 | +def test_lambda_not_considered_function(): |
| 204 | + """Test that imports in lambdas (which aren't supported anyway) at module level are flagged.""" |
| 205 | + code = """ |
| 206 | +# Note: This is contrived since you can't actually have imports in lambdas, |
| 207 | +# but this tests that lambda doesn't count as a function scope |
| 208 | +import dspy |
| 209 | +""" |
| 210 | + errors = run_checker(code) |
| 211 | + assert len(errors) == 1 |
| 212 | + assert "BAI001" in errors[0][2] |
0 commit comments