flake8-annotations-complexity

0.1.0 · active · verified Wed Apr 15

An extension for flake8 to report on excessively complex type annotations. Complex type annotations often indicate suboptimal annotation usage, poor code decomposition, or an improper choice of data structure, making the code harder to read. The plugin calculates annotation complexity based on the maximum nesting level (e.g., `List[int]` is 2, `Tuple[List[Optional[str]], int]` is 4). The current version is 0.1.0, and it receives infrequent but active maintenance.

Warnings

Install

Quickstart

This quickstart demonstrates how to install and use `flake8-annotations-complexity`. It creates a temporary Python file with varying annotation complexities, then runs `flake8` first with the default complexity limit (3) and then with a higher limit (4). The plugin reports errors (TAE002) for annotations exceeding the configured maximum complexity.

import os
from typing import List, Dict, Union, Optional

# Create a dummy Python file to lint
python_code = '''
# test_complexity.py
from typing import List, Dict, Union, Optional

def func_with_complex_annotation(
    data: Dict[str, List[Union[int, Optional[str]]]]
) -> Dict[str, List[Union[int, Optional[str]]]]:
    """A function with a very complex type annotation."""
    return data

def func_with_medium_annotation(
    config: Dict[str, List[str]]
) -> None:
    """A function with a medium-complexity annotation."""
    pass

def func_with_simple_annotation(
    name: str, age: int
) -> str:
    """A function with simple annotations."""
    return f"{name} is {age} years old"
'''

with open('test_complexity.py', 'w') as f:
    f.write(python_code)

# Run flake8 with the plugin
print("\n--- Running flake8 with default max-annotations-complexity (3) ---")
os.system("flake8 test_complexity.py")

print("\n--- Running flake8 with max-annotations-complexity set to 4 ---")
os.system("flake8 --max-annotations-complexity=4 test_complexity.py")

# Clean up the dummy file
os.remove('test_complexity.py')

view raw JSON →