|
1 | | -# Copilot Instructions for Python Files in `packages/slackBotFunction` |
| 1 | +--- |
| 2 | +description: 'Guidelines for writing high-quality, maintainable python code with best practices for logging, error handling, code organization, naming, formatting, and style.' |
| 3 | +applyTo: '**/*.py' |
| 4 | +--- |
2 | 5 |
|
3 | | -## Purpose |
4 | | -These instructions guide GitHub Copilot to generate high-quality, maintainable Python code, with modular architecture, service layers, and test coverage. |
| 6 | +# Python Copilot Instructions |
5 | 7 |
|
6 | | -## General Guidelines |
7 | | -- Use Python 3.9+ syntax and typing. |
8 | | -- Follow PEP8 for formatting and naming conventions. |
9 | | -- Prefer explicit imports and avoid wildcard imports. |
10 | | -- Use docstrings for all public functions, classes, and modules. |
11 | | -- Organize code into logical modules: `core`, `services`, `utils`. |
12 | | -- Write modular, testable code with clear separation of concerns. |
13 | | -- Use dependency injection for services and external integrations. |
14 | | -- Handle exceptions gracefully and log errors using the standard `logging` module. |
15 | | -- NEVER hardcode secrets or configuration; use environment variables or config files. |
| 8 | +These instructions are designed to guide GitHub Copilot in generating effective, maintainable, and domain-appropriate Python code. They are intended to be generic and applicable to a wide range of Python projects. |
16 | 9 |
|
17 | | -## File/Folder Specific Instructions |
18 | | -### `app/handler.py` |
19 | | -- Main entry point for AWS Lambda handler. |
20 | | -- Validate and parse incoming Slack events. |
21 | | -- Delegate business logic to service or core modules. |
22 | | -- Return AWS Lambda-compatible response objects. |
| 10 | +## 1. Code Organization & Structure |
| 11 | +- Organize code into logical modules and packages. Use directories such as `core/`, `services/`, `utils/` for separation of concerns. |
| 12 | +- Place entry points (e.g., `handler.py`) at the top level of the main package. |
| 13 | +- Use `__init__.py` files to define package boundaries and expose public APIs. |
| 14 | +- Group related functions and classes together. Avoid large monolithic files. |
| 15 | +- Store tests in a dedicated `tests/` directory, mirroring the structure of the main codebase. |
23 | 16 |
|
24 | | -### `app/core/` |
25 | | -- Implement core business logic and reusable components. |
26 | | -- Avoid direct dependencies on Slack or AWS SDKs. |
27 | | -- Write pure functions where possible. |
| 17 | +## 2. Naming Conventions |
| 18 | +- Use `snake_case` for function and variable names. |
| 19 | +- Use `PascalCase` for class names. |
| 20 | +- Prefix private functions and variables with a single underscore (`_`). |
| 21 | +- Name modules and packages using short, descriptive, lowercase names. |
| 22 | +- Use clear, descriptive names for all symbols. Avoid abbreviations unless they are widely understood. |
28 | 23 |
|
29 | | -### `app/services/` |
30 | | -- Integrate with external APIs (e.g., Bedrock, DynamoDB). |
31 | | -- Use abstraction layers for AWS services. |
32 | | -- Mock external dependencies in tests. |
| 24 | +## 3. Formatting & Style |
| 25 | +- Follow [PEP 8](https://peps.python.org/pep-0008/) for code style and formatting. |
| 26 | +- Use 4 spaces per indentation level. Do not use tabs. |
| 27 | +- Limit lines to 120 characters. |
| 28 | +- Use blank lines to separate functions, classes, and logical sections. |
| 29 | +- Place imports at the top of each file, grouped by standard library, third-party, and local imports. |
| 30 | +- Use single quotes for strings unless double quotes are required. |
| 31 | +- Add docstrings to all public modules, classes, and functions. Use triple double quotes for docstrings. |
33 | 32 |
|
34 | | -### `app/utils/` |
35 | | -- Provide utility functions for common tasks (e.g., string manipulation, config loading). |
36 | | -- Keep utilities stateless and reusable. |
| 33 | +## 4. Logging Best Practices |
| 34 | +- Use the standard `logging` library for all logging. |
| 35 | +- Configure logging in the main entry point or via a dedicated utility module. |
| 36 | +- Use appropriate log levels: `debug`, `info`, `warning`, `error`, `critical`. |
| 37 | +- Avoid logging sensitive information. |
| 38 | +- Include contextual information in log messages (e.g., function names, parameters, error details). |
| 39 | +- Example: |
| 40 | + ```python |
| 41 | + import logging |
| 42 | + logger = logging.getLogger(__name__) |
| 43 | + logger.info('Processing event: %s', event) |
| 44 | + ``` |
37 | 45 |
|
38 | | -### `tests/` |
39 | | -- Use `pytest` for all tests. |
40 | | -- Name test files and functions descriptively (e.g., `test_handler_utils.py`, `test_forward_to_lambda`). |
41 | | -- Mock AWS and Slack APIs using `pytest-mock` or `unittest.mock`. |
42 | | -- Ensure coverage for error cases and edge conditions. |
| 46 | +## 5. Error Handling Best Practices |
| 47 | +- Use `try`/`except` blocks to handle exceptions gracefully. |
| 48 | +- Catch specific exceptions rather than using bare `except`. |
| 49 | +- Log exceptions with stack traces using `logger.exception()`. |
| 50 | +- Raise custom exceptions for domain-specific errors. |
| 51 | +- Validate inputs and fail fast with clear error messages. |
| 52 | +- Example: |
| 53 | + ```python |
| 54 | + try: |
| 55 | + result = process_event(event) |
| 56 | + except ValueError as e: |
| 57 | + logger.error('Invalid event: %s', e) |
| 58 | + raise |
| 59 | + ``` |
43 | 60 |
|
44 | | -## Best Practices |
45 | | -- Use type hints and `Optional` where appropriate. |
46 | | -- Prefer f-strings for string formatting. |
47 | | -- Use context managers for resource handling. |
48 | | -- Write unit tests for all new code and maintain high coverage. |
49 | | -- Document public APIs and expected input/output formats. |
50 | | -- Avoid global state; prefer passing dependencies explicitly. |
| 61 | +## 6. Testing Guidelines |
| 62 | +- Write unit tests for all public functions and classes. |
| 63 | +- Use `pytest` as the preferred testing framework. |
| 64 | +- Name test files and functions using `test_` prefix. |
| 65 | +- Use fixtures for setup and teardown. |
| 66 | +- Mock external dependencies in tests. |
| 67 | +- Ensure tests are isolated and repeatable. |
| 68 | + |
| 69 | +## 7. Dependency Management |
| 70 | +- Use `pyproject.toml` to specify dependencies. |
| 71 | +- Never use `requirements.txt` to specify dependencies. |
| 72 | +- Pin versions for critical dependencies. |
| 73 | +- Avoid unnecessary dependencies. |
51 | 74 |
|
52 | | -## Example Docstring |
53 | | -""" |
54 | | -Handles incoming Slack event and routes to appropriate service. |
55 | | -Args: |
56 | | - event (dict): Slack event payload. |
57 | | - context (LambdaContext): AWS Lambda context object. |
58 | | -Returns: |
59 | | - dict: AWS Lambda response object. |
60 | | -Raises: |
61 | | - ValueError: If event payload is invalid. |
62 | | -""" |
| 75 | +## 8. Documentation |
| 76 | +- Document all public APIs with clear docstrings. |
| 77 | +- Use [Google](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings) or [NumPy](https://numpydoc.readthedocs.io/en/latest/format.html) style for docstrings. |
| 78 | +- Provide usage examples in README files. |
63 | 79 |
|
64 | | -## Prohibited Patterns |
65 | | -- No direct AWS credentials or secrets in code. |
66 | | -- No print statements for logging (use `logging` instead). |
67 | | -- No business logic in handler files; delegate to services/core. |
| 80 | +## 9. Security & Privacy |
| 81 | +- Do not log or expose secrets, credentials, or sensitive data. |
| 82 | +- Validate and sanitize all external inputs. |
| 83 | +- Use environment variables for configuration secrets. |
68 | 84 |
|
69 | | -## References |
70 | | -- [PEP8](https://peps.python.org/pep-0008/) |
71 | | -- [pytest Documentation](https://docs.pytest.org/en/stable/) |
| 85 | +## 10. General Guidelines |
| 86 | +- Prefer readability and simplicity over cleverness. |
| 87 | +- Refactor duplicated code into reusable functions or classes. |
| 88 | +- Use type hints for function signatures and variables where appropriate. |
| 89 | +- Avoid global variables; use function arguments or class attributes. |
72 | 90 |
|
73 | 91 | --- |
| 92 | + |
0 commit comments