|
| 1 | +""" |
| 2 | +Demo app.py for CodeSentry screenshot |
| 3 | +Triggers CS001 at line 47 and CS003 at line 82 |
| 4 | +""" |
| 5 | + |
| 6 | +import asyncio |
| 7 | +import time |
| 8 | +import json |
| 9 | + |
| 10 | + |
| 11 | +# ============================================ |
| 12 | +# Normal code padding to get line numbers right |
| 13 | +# ============================================ |
| 14 | + |
| 15 | +def process_data(data): |
| 16 | + """Process incoming data.""" |
| 17 | + result = [] |
| 18 | + for item in data: |
| 19 | + if item.get("active"): |
| 20 | + result.append(item["value"]) |
| 21 | + return result |
| 22 | + |
| 23 | + |
| 24 | +def validate_input(user_input): |
| 25 | + """Validate user input.""" |
| 26 | + if not user_input: |
| 27 | + return False |
| 28 | + if len(user_input) > 1000: |
| 29 | + return False |
| 30 | + return True |
| 31 | + |
| 32 | + |
| 33 | +def format_response(data): |
| 34 | + """Format data for API response.""" |
| 35 | + return { |
| 36 | + "status": "success", |
| 37 | + "data": data, |
| 38 | + "timestamp": "2026-02-03T12:00:00Z" |
| 39 | + } |
| 40 | + |
| 41 | + |
| 42 | +class DataProcessor: |
| 43 | + # Line 47: CS001 - generic-exception-swallow (indented for col 5) |
| 44 | + def fetch_config(self): |
| 45 | + try: |
| 46 | + with open("config.json") as f: |
| 47 | + return json.load(f) |
| 48 | + except Exception: |
| 49 | + pass |
| 50 | + |
| 51 | + def calculate_metrics(self, values): |
| 52 | + """Calculate metrics from values.""" |
| 53 | + if not values: |
| 54 | + return {} |
| 55 | + return { |
| 56 | + "total": sum(values), |
| 57 | + "average": sum(values) / len(values), |
| 58 | + "count": len(values) |
| 59 | + } |
| 60 | + |
| 61 | + def get_user_preferences(self, user_id): |
| 62 | + """Get user preferences from cache.""" |
| 63 | + cache = {} |
| 64 | + return cache.get(user_id, {}) |
| 65 | + |
| 66 | + def normalize_text(self, text): |
| 67 | + """Normalize text input.""" |
| 68 | + if text is None: |
| 69 | + return "" |
| 70 | + return text.strip().lower() |
| 71 | + |
| 72 | + |
| 73 | +class AsyncService: |
| 74 | + """Async service for remote operations.""" |
| 75 | + |
| 76 | + async def connect(self): |
| 77 | + """Establish connection.""" |
| 78 | + pass |
| 79 | + |
| 80 | + # Line 82: CS003 - blocking-call-in-async (indented for col 9) |
| 81 | + async def sync_with_server(self): |
| 82 | + """Sync data with remote server.""" |
| 83 | + print("Starting sync...") |
| 84 | + time.sleep(5) # Blocking call in async! |
| 85 | + print("Sync complete") |
| 86 | + |
| 87 | + |
| 88 | +async def main(): |
| 89 | + """Main async entry point.""" |
| 90 | + service = AsyncService() |
| 91 | + await service.sync_with_server() |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + asyncio.run(main()) |
0 commit comments