|
| 1 | +# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. |
| 2 | +# Lean CLI v1.0. Copyright 2021 QuantConnect Corporation. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | + |
| 17 | +MYPY_IGNORE_PATTERNS = [ |
| 18 | + 'click.', 'subprocess.', 'Module "', |
| 19 | + 'has incompatible type "Optional', |
| 20 | + 'validator', 'pydantic', '__call__', |
| 21 | + 'OnlyValueValidator', 'V1Validator', |
| 22 | + 'QCParameter', 'QCBacktest' |
| 23 | +] |
| 24 | + |
| 25 | +def run_mypy_check(): |
| 26 | + result = subprocess.run( |
| 27 | + ["python", "-m", "mypy", "lean/", |
| 28 | + "--show-error-codes", |
| 29 | + "--no-error-summary", |
| 30 | + "--ignore-missing-imports", |
| 31 | + "--check-untyped-defs"], |
| 32 | + capture_output=True, |
| 33 | + text=True |
| 34 | + ) |
| 35 | + |
| 36 | + errors = [] |
| 37 | + for line in result.stdout.splitlines() + result.stderr.splitlines(): |
| 38 | + if not line.strip() or '[call-arg]' not in line: |
| 39 | + continue |
| 40 | + |
| 41 | + # Skip false positives |
| 42 | + if any(pattern in line for pattern in MYPY_IGNORE_PATTERNS): |
| 43 | + continue |
| 44 | + |
| 45 | + errors.append(line.strip()) |
| 46 | + |
| 47 | + return errors |
| 48 | + |
| 49 | +def run_flake8_check(select_code): |
| 50 | + result = subprocess.run( |
| 51 | + ["python", "-m", "flake8", "lean/", |
| 52 | + f"--select={select_code}", |
| 53 | + "--ignore=ALL", |
| 54 | + "--exit-zero"], |
| 55 | + capture_output=True, |
| 56 | + text=True |
| 57 | + ) |
| 58 | + |
| 59 | + errors = [line.strip() for line in result.stdout.splitlines() if line.strip()] |
| 60 | + return errors, len(errors) |
| 61 | + |
| 62 | +def display_errors(title, errors, is_critical = True): |
| 63 | + level = "CRITICAL" if is_critical else "WARNING" |
| 64 | + if errors: |
| 65 | + print(f"{level}: {len(errors)} {title} found:") |
| 66 | + for error in errors: |
| 67 | + # Clean path for better display |
| 68 | + clean_error = error.replace('/home/runner/work/lean-cli/lean-cli/', '') |
| 69 | + print(f" {clean_error}") |
| 70 | + else: |
| 71 | + print(f"No {title} found") |
| 72 | + |
| 73 | +def display_warning_summary(unused_count): |
| 74 | + print("\nWarnings:") |
| 75 | + if unused_count > 0: |
| 76 | + print(f" - Unused imports: {unused_count}") |
| 77 | + print(" Consider addressing warnings in future updates.") |
| 78 | + |
| 79 | +def run_analysis() -> int: |
| 80 | + print("Running static analysis...") |
| 81 | + print("=" * 60) |
| 82 | + |
| 83 | + critical_error_count = 0 |
| 84 | + warning_count = 0 |
| 85 | + |
| 86 | + # Check for missing function arguments with mypy |
| 87 | + print("\n1. Checking for missing function arguments...") |
| 88 | + print("-" * 40) |
| 89 | + |
| 90 | + call_arg_errors = run_mypy_check() |
| 91 | + display_errors("function call argument mismatch(es)", call_arg_errors) |
| 92 | + critical_error_count += len(call_arg_errors) |
| 93 | + |
| 94 | + # Check for undefined variables with flake8 |
| 95 | + print("\n2. Checking for undefined variables...") |
| 96 | + print("-" * 40) |
| 97 | + |
| 98 | + undefined_errors, undefined_count = run_flake8_check("F821") |
| 99 | + display_errors("undefined variable(s)", undefined_errors) |
| 100 | + critical_error_count += undefined_count |
| 101 | + |
| 102 | + # Check for unused imports with flake8 |
| 103 | + print("\n3. Checking for unused imports...") |
| 104 | + print("-" * 40) |
| 105 | + |
| 106 | + unused_imports, unused_count = run_flake8_check("F401") |
| 107 | + display_errors("unused import(s)", unused_imports, is_critical=False) |
| 108 | + warning_count += unused_count |
| 109 | + |
| 110 | + # Summary |
| 111 | + print("\n" + "=" * 60) |
| 112 | + |
| 113 | + if critical_error_count > 0: |
| 114 | + print(f"BUILD FAILED: Found {critical_error_count} critical error(s)") |
| 115 | + print("\nSummary of critical errors:") |
| 116 | + print(f" - Function call argument mismatches: {len(call_arg_errors)}") |
| 117 | + print(f" - Undefined variables: {undefined_count}") |
| 118 | + |
| 119 | + if warning_count > 0: |
| 120 | + display_warning_summary(unused_count) |
| 121 | + |
| 122 | + return 1 |
| 123 | + |
| 124 | + if warning_count > 0: |
| 125 | + print(f"BUILD PASSED with {warning_count} warning(s)") |
| 126 | + display_warning_summary(unused_count) |
| 127 | + return 0 |
| 128 | + |
| 129 | + print("SUCCESS: All checks passed with no warnings") |
| 130 | + return 0 |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + sys.exit(run_analysis()) |
0 commit comments