This document describes the testing infrastructure, code quality tools, and quality assurance practices within DB-GPT. It covers test organization, the pytest testing framework, code quality tools (mypy for type checking and ruff for linting), test utilities, and guidelines for writing and running tests. The testing framework supports both unit tests (isolated component testing) and integration tests (multi-component interaction testing).
For information about the overall development workflow, see Development Environment Setup. For CI/CD pipelines and automated quality checks, see Contributing and CI/CD.
Sources: tests/unit_tests/test_plugins.py1-130 tests/unit_tests/vector_store/test_pgvector.py1-14 .mypy.ini1-127
DB-GPT organizes tests into two primary categories: unit tests and integration tests. The test suite is located in the tests/ directory at the repository root.
Test Directory Organization with Code Paths
Test Directory Organization
| Directory Path | Test Type | Components Under Test | Key Test Files |
|---|---|---|---|
tests/unit_tests/ | Unit tests | Individual components in isolation | test_plugins.py |
tests/intetration_tests/ | Integration tests | Multi-component workflows | Directory structure only |
tests/unit_tests/embedding_engine/ | Unit tests | dbgpt.EmbeddingEngine, KnowledgeType | url_test.py, document_test.py |
tests/unit_tests/vector_store/ | Unit tests | dbgpt.storage.vector_store, IndexStoreBase, IndexStoreConfig | test_pgvector.py |
tests/intetration_tests/vector_store/ | Integration tests | End-to-end vector storage workflows | __init__.py |
tests/intetration_tests/kbqa/ | Integration tests | Knowledge-based QA pipelines | __init__.py |
Note: The directory name intetration_tests contains an intentional misspelling in the codebase.
Sources: tests/unit_tests/embedding_engine/url_test.py1-21 tests/unit_tests/test_plugins.py1-130 tests/unit_tests/vector_store/test_pgvector.py1-14 tests/intetration_tests/vector_store/__init__.py1 tests/intetration_tests/kbqa/__init__.py1
Unit tests focus on testing individual components in isolation. They use mocking and fixtures to eliminate external dependencies.
Example: Vector Store Import Validation
The test_vetorestore_imports function validates that all vector store classes properly implement the required base classes:
tests/unit_tests/vector_store/test_pgvector.py5-13
This test validates the structure of all vector store implementations by:
dbgpt.storage.vector_store.__all__getattr() to dynamically retrieve each (store_cls, config_cls) tupleissubclass(store_cls, IndexStoreBase) for the store classissubclass(config_cls, IndexStoreConfig) for the configuration classThis pattern ensures consistent interface implementation across all vector store backends (Milvus, Chroma, PGVector, Elasticsearch, Weaviate, OceanBase).
Example: Plugin Testing
The plugin test suite demonstrates comprehensive unit testing patterns for the dbgpt.plugins module:
tests/unit_tests/test_plugins.py19-25
The test_inspect_zip_for_modules function validates inspect_zip_for_modules() by verifying it correctly identifies Python module files within plugin ZIP archives.
Key testing patterns demonstrated:
MockConfig objectsmonkeypatch.setattr("builtins.input", lambda _: "y") to simulate user inputdenylist_allowlist_check() function behaviorIntegration tests validate end-to-end workflows involving multiple components. The directory structure separates integration tests by functional area.
Example Test Areas:
Sources: tests/unit_tests/vector_store/test_pgvector.py5-13 tests/unit_tests/test_plugins.py1-130
DB-GPT uses pytest as its primary testing framework. The framework provides:
pytest Framework Execution Flow
Fixture Example
Mock configuration fixture for plugin testing:
tests/unit_tests/test_plugins.py28-38
The mock_config_denylist_allowlist_check fixture:
MockConfig class instanceplugins_denylist = ["BadPlugin"]plugins_allowlist = ["GoodPlugin"]authorise_key = "y" and exit_key = "n" for user input simulationMonkeypatch Example
Simulating user input in plugin authorization tests:
tests/unit_tests/test_plugins.py59-66
The test_denylist_allowlist_check_user_input_yes function:
monkeypatch fixture as parametermonkeypatch.setattr("builtins.input", lambda _: "y") to replace input() functiondenylist_allowlist_check("UnknownPlugin", mock_config_denylist_allowlist_check)True when user approvesSources: tests/unit_tests/test_plugins.py28-66
Embedding engine tests validate document and URL embedding functionality using the dbgpt.EmbeddingEngine class. These tests are located in tests/unit_tests/embedding_engine/.
Embedding Engine Test Code Structure
URL Embedding Test Structure
tests/unit_tests/embedding_engine/url_test.py1-20
The URL test:
knowledge_source = "https://docs.dbgpt.site/docs/overview"KnowledgeType.URL.value for web contentvector_store_name = url.replace(":", "") to create valid store nameEmbeddingEngine instance with configurationembedding_engine.knowledge_embedding() to executeDocument Embedding Test Structure
tests/unit_tests/embedding_engine/document_test.py1-21
The document test:
knowledge_source = document_path pointing to local fileKnowledgeType.DOCUMENT.value for file content.md, .pdf, .docx, .csv, .htmlvector_store_name = "document_test" as fixed identifierBoth tests follow the pattern:
embedding_model, vector_store_type, and chroma_persist_pathvector_store_config dictionary with required keysEmbeddingEngine with knowledge_source, knowledge_type, model_name, and vector_store_configknowledge_embedding() method to execute embedding pipelineSupported Document Types
| Format | File Extension | Notes |
|---|---|---|
| Markdown | .md | Text-based documentation |
.pdf | Portable document format | |
| Word | .docx | Microsoft Word documents |
| CSV | .csv | Comma-separated values |
| HTML | .html | Web pages |
Sources: tests/unit_tests/embedding_engine/url_test.py1-21 tests/unit_tests/embedding_engine/document_test.py1-21
Plugin testing validates the plugin system's security and functionality, including denylist/allowlist checks and plugin scanning.
Plugin Test Function Mapping to Code Under Test
tests/unit_tests/test_plugins.py12-16
Test resource constants:
| Constant | Value | Purpose |
|---|---|---|
PLUGINS_TEST_DIR | "tests/unit/data/test_plugins" | Original test plugin directory path |
PLUGINS_TEST_DIR_TEMP | "data/test_plugins" | Alternative test plugin directory |
PLUGIN_TEST_ZIP_FILE | "Auto-GPT-Plugin-Test-master.zip" | Sample plugin ZIP for inspect_zip_for_modules() tests |
PLUGIN_TEST_INIT_PY | "Auto-GPT-Plugin-Test-master/src/auto_gpt_vicuna/__init__.py" | Expected module path within ZIP |
PLUGIN_TEST_OPENAI | "https://weathergpt.vercel.app/" | OpenAI plugin URL for scan_plugins() tests |
Denylist/Allowlist Mock Config:
tests/unit_tests/test_plugins.py28-38
This mock provides:
plugins_denylist: List of blocked pluginsplugins_allowlist: List of approved pluginsauthorise_key / exit_key: User input optionsOpenAI Plugin Mock Config:
tests/unit_tests/test_plugins.py89-102
Generic Plugin Mock Config:
tests/unit_tests/test_plugins.py111-123
| Test Function | Purpose | Mock/Fixture Used |
|---|---|---|
test_inspect_zip_for_modules | Validates ZIP file module inspection | File system access |
test_denylist_allowlist_check_denylist | Ensures denylisted plugins are blocked | mock_config_denylist_allowlist_check |
test_denylist_allowlist_check_allowlist | Ensures allowlisted plugins are allowed | mock_config_denylist_allowlist_check |
test_denylist_allowlist_check_user_input_yes | Tests user approval flow | mock_config_denylist_allowlist_check + monkeypatch |
test_denylist_allowlist_check_user_input_no | Tests user rejection flow | mock_config_denylist_allowlist_check + monkeypatch |
test_denylist_allowlist_check_user_input_invalid | Tests invalid input handling | mock_config_denylist_allowlist_check + monkeypatch |
test_scan_plugins_openai | Tests OpenAI plugin scanning | mock_config_openai_plugin |
test_scan_plugins_generic | Tests generic plugin scanning | mock_config_generic_plugin |
Sources: tests/unit_tests/test_plugins.py1-130
| Convention | Purpose | Discovery | Example Files |
|---|---|---|---|
test_*.py | pytest test files | Automatically discovered by pytest | test_plugins.py, test_pgvector.py |
*_test.py | Example/demo scripts | Not discovered by pytest by default | url_test.py, document_test.py |
Pytest automatically discovers files matching the test_*.py pattern. Files with the *_test.py pattern are typically example scripts demonstrating usage rather than automated tests. To run these, they must be explicitly specified: pytest tests/unit_tests/embedding_engine/url_test.py.
Basic Test Function:
Test with Fixture:
Test with Monkeypatch:
Sources: tests/unit_tests/test_plugins.py19-130
Run all tests:
Run unit tests only:
Run integration tests only:
Run specific test file:
Run specific test function:
Run tests matching pattern:
Run with verbose output:
Run with coverage report:
Run tests with JUnit XML output:
pytest Execution Flow with Code References
Sources: tests/unit_tests/test_plugins.py1-130 tests/unit_tests/vector_store/test_pgvector.py1-14
The test_vetorestore_imports function demonstrates a pattern for validating that all implementations properly inherit from base classes:
tests/unit_tests/vector_store/test_pgvector.py5-13
Implementation details:
This pattern:
dbgpt.storage.vector_store.__all__ to enumerate all implementationsgetattr(vector_store, cls) to dynamically retrieve (store_cls, config_cls) tuplesIndexStoreBase inheritance for store classesIndexStoreConfig inheritance for configuration classesMock configuration objects simulate the dbgpt._private.config.Config class for testing:
tests/unit_tests/test_plugins.py30-37
The MockConfig class:
Implementation characteristics:
dbgpt._private.config.Config interface@pytest.fixture decorator for reusabilityTesting interactive features using the monkeypatch fixture:
tests/unit_tests/test_plugins.py59-66
Implementation:
Pattern characteristics:
monkeypatch fixture as function parametermonkeypatch.setattr("builtins.input", lambda _: "y") to replace the global input() functiondenylist_allowlist_check() without requiring actual user interaction"y" (yes), "n" (no), "invalid" (invalid input)The embedding engine tests use a consistent three-step pattern:
Step 1: Configuration setup tests/unit_tests/embedding_engine/url_test.py3-11
Step 2: Engine initialization tests/unit_tests/embedding_engine/url_test.py12-17
Step 3: Execution tests/unit_tests/embedding_engine/url_test.py20
This pattern applies to testing other KnowledgeType values: DOCUMENT, TEXT, KNOWLEDGE_BASE.
Sources: tests/unit_tests/vector_store/test_pgvector.py5-13 tests/unit_tests/test_plugins.py28-66 tests/unit_tests/embedding_engine/url_test.py1-21
Principle: Each test should be independent and not rely on the execution of other tests.
Implementation:
Example: tests/unit_tests/test_plugins.py28-38 - Each test gets a fresh mock config
Principle: Tests should not depend on external services, file systems, or databases unless testing integration.
Implementation:
Example: tests/unit_tests/test_plugins.py45-48 - Mocking user input
Principle: Test names should clearly communicate what is being tested and the expected behavior.
Pattern: test_<component>_<scenario>_<expected_result>
Examples:
test_denylist_allowlist_check_denylist - Tests denylist checkingtest_denylist_allowlist_check_user_input_yes - Tests user approval flowtest_scan_plugins_openai - Tests OpenAI plugin scanningPrinciple: Tests serve as documentation for expected behavior.
Implementation:
Example: tests/unit_tests/test_plugins.py31-32 - Mock config with descriptive docstring
Principle: Common setup should be extracted into reusable fixtures.
Implementation:
function, class, module, session)@pytest.fixture decoratorExamples:
Principle: Aim for high test coverage, especially for critical paths.
Focus Areas:
Example: Plugin tests cover multiple scenarios:
Principle: Unit tests should run quickly to enable fast feedback loops.
Implementation:
Sources: tests/unit_tests/test_plugins.py1-130 tests/unit_tests/vector_store/test_pgvector.py5-13
| Resource Type | Location | Purpose | Referenced In |
|---|---|---|---|
| Test plugins | tests/unit/data/test_plugins/ or data/test_plugins/ | Plugin ZIP files and modules for inspect_zip_for_modules() | PLUGINS_TEST_DIR, PLUGINS_TEST_DIR_TEMP constants |
| Test documents | Configured per test via knowledge_source parameter | Sample .md, .pdf, .docx, .csv, .html files | url_test.py, document_test.py |
| Test fixtures | Within test files using @pytest.fixture | Mock configuration objects (MockConfig classes) | mock_config_denylist_allowlist_check, etc. |
Example: tests/unit_tests/test_plugins.py12-13 defines PLUGINS_TEST_DIR and PLUGINS_TEST_DIR_TEMP constants for plugin test data location.
Embedding Engine Tests Configuration
Tests using dbgpt.EmbeddingEngine require:
| Parameter | Type | Example Value | Purpose |
|---|---|---|---|
embedding_model | str | "your_embedding_model" | Embedding model identifier |
vector_store_type | str | "Chroma" | Vector store implementation name |
chroma_persist_path | str | "your_persist_path" | Local directory for Chroma persistence |
knowledge_source | str | "https://docs.dbgpt.site/docs/overview" or "path/to/file.md" | URL or file path to embed |
vector_store_name | str | "document_test" | Unique identifier for vector store instance |
Plugin Tests Configuration
Tests using dbgpt.plugins functions require MockConfig with:
| Attribute | Type | Example Value | Purpose |
|---|---|---|---|
plugins_dir | str | f"{current_dir}/data/test_plugins/" | Directory containing plugin files |
plugins_denylist | List[str] | ["BadPlugin"] | List of blocked plugin names |
plugins_allowlist | List[str] | ["GoodPlugin"] | List of approved plugin names |
plugins_openai | List[str] | ["https://weathergpt.vercel.app/"] | List of OpenAI plugin URLs |
Sources: tests/unit_tests/embedding_engine/url_test.py3-11 tests/unit_tests/test_plugins.py12-16 tests/unit_tests/test_plugins.py93-101
Integration tests validate end-to-end workflows involving multiple components. These tests are located in tests/intetration_tests/ (note the spelling in the codebase).
Differences from Unit Tests:
When to Use Integration Tests:
Sources: tests/intetration_tests/vector_store/__init__.py1 tests/intetration_tests/kbqa/__init__.py1
The .gitignore file defines test-related artifacts that are excluded from version control.
Coverage and Test Output Files
| Pattern | Description | Generated By |
|---|---|---|
htmlcov/ | HTML coverage report directory | pytest --cov-report=html |
.tox/ | tox virtual environment directory | tox test automation |
.nox/ | nox virtual environment directory | nox test automation |
.coverage | Coverage data file | pytest-cov / coverage.py |
.coverage.* | Coverage data files for parallel runs | coverage combine |
.cache | pytest cache directory | pytest |
nosetests.xml | Nose test runner XML output | nosetests (legacy) |
coverage.xml | XML coverage report | pytest --cov-report=xml |
*.cover | Coverage report files | coverage report |
*.py,cover | Coverage annotated Python files | coverage annotate |
.hypothesis/ | Hypothesis testing database | pytest-hypothesis |
.pytest_cache/ | pytest cache directory | pytest session cache |
Test Data and Temporary Files
| Pattern | Description |
|---|---|
.plugin_env | Plugin environment configuration |
/pilot/meta_data/alembic/versions/* | Database migration versions |
/pilot/meta_data/*.db | Test metadata databases |
/pilot/benchmark_meta_data/*.db | Benchmark test databases |
/pilot/benchmark_meta_data/result/* | Benchmark test results |
These directories and files are generated during test execution and should not be committed to the repository.
Sources: .gitignore50-62 .gitignore152-156
DB-GPT uses GitHub Actions to automate testing and quality checks on every pull request and push to the main branch.
The test workflow runs automatically on:
main branchmain branchdbgpt/**, pilot/meta_data/**, or workflow filesCI Execution Matrix
The workflow uses a matrix strategy defined to test across multiple environments:
| Parameter | Values | Purpose |
|---|---|---|
| Operating System | ubuntu-latest, macos-latest | Cross-platform compatibility testing |
| Python Version | 3.10, 3.11 | Multi-version Python support |
| Total Combinations | 4 (2 OS × 2 Python versions) | Comprehensive environment coverage |
This creates four parallel CI jobs: ubuntu-3.10, ubuntu-3.11, macos-3.10, macos-3.11.
CI Workflow Execution Flow
Setup Phase
The CI workflow performs environment setup:
actions/checkout@v4 to clone the repositoryactions/setup-python@v4 to install Python (version from matrix)python -m pip install --upgrade pip to update pippip install -e ".[openai]" to install DB-GPT with OpenAI extraspip install -r requirements/dev-requirements.txt to install development dependenciesTest Execution Phase
The workflow runs pytest with reporting options:
Command breakdown:
pytest dbgpt: Run tests in the dbgpt/ package directory--cov=dbgpt: Measure code coverage for the dbgpt package--cov-report=xml: Generate machine-readable XML coverage report with matrix-specific filename--cov-report=html: Generate human-readable HTML coverage report with matrix-specific directory name--junitxml: Generate JUnit XML test results with matrix-specific filenameCoverage Report Generation
On Ubuntu runs only, the workflow parses the coverage-${{ matrix.python-version }}-${{ matrix.os }}.xml file to extract package-level line coverage rates:
This extracts top-level package coverage (e.g., dbgpt.rag, dbgpt.model, dbgpt.core) and their line coverage rates.
Test Report Parsing
The workflow extracts test statistics from the pytest_report-${{ matrix.python-version }}-${{ matrix.os }}.xml file:
Extracted attributes:
tests="N": Total number of test functions executedfailures="N": Number of test failuresskipped="N": Number of skipped testsArtifact Upload
All test and coverage reports are uploaded using actions/upload-artifact@v3:
test-and-coverage-results-${{ matrix.python-version }}-${{ matrix.os }}coverage-${{ matrix.python-version }}-${{ matrix.os }}.xml: XML coverage datahtmlcov-${{ matrix.python-version }}-${{ matrix.os }}/: HTML coverage report directorypytest_report-${{ matrix.python-version }}-${{ matrix.os }}.xml: JUnit test resultsExample artifact names:
test-and-coverage-results-3.10-ubuntu-latesttest-and-coverage-results-3.11-macos-latestIn Pull Requests:
Coverage Reports:
htmlcov-{version}-{os}/index.htmlTest Reports:
Sources: .github/workflows/test-python.yml90-99
DB-GPT uses pytest-cov (a pytest plugin wrapping coverage.py) to measure code coverage during test execution.
Coverage Types and Report Formats
Coverage Report File Structure
Line Coverage:
line-rate="0.85" (85% coverage)Branch Coverage:
branch-rate="0.75" (75% branch coverage)Sources: .gitignore50-62
Generate HTML Coverage Report:
This creates the htmlcov/ directory (excluded from git via .gitignore:51).
View HTML Report:
Generate Terminal Report with Missing Lines:
This displays coverage summary in terminal and lists specific line numbers not covered.
Generate XML Report:
This creates coverage.xml (excluded from git via .gitignore:58), which CI pipelines parse.
Coverage Configuration Options
| Option | Description | Output File/Directory | Excluded by .gitignore |
|---|---|---|---|
--cov=<package> | Measure coverage for package | .coverage data file | Yes (.coverage) |
--cov=dbgpt | Measure coverage for dbgpt package | .coverage | Yes |
--cov-report=html | Generate HTML report | htmlcov/ directory | Yes (htmlcov/) |
--cov-report=xml | Generate XML report | coverage.xml | Yes (coverage.xml) |
--cov-report=term | Terminal summary | stdout | N/A |
--cov-report=term-missing | Show missing lines in terminal | stdout | N/A |
--cov-fail-under=<min> | Fail if coverage < threshold | Exit code | N/A |
--cov-branch | Measure branch coverage | Adds branch data to reports | N/A |
Reading Coverage Reports
The HTML coverage report (htmlcov/index.html) displays:
| Report Element | Description | Example |
|---|---|---|
| Overall coverage percentage | Total line coverage across all measured code | 85% |
| Package breakdown | Coverage per package/module | dbgpt.rag: 82%, dbgpt.model: 78% |
| File-level coverage | Individual file coverage with percentages | dbgpt/core/interface/llm.py: 91% |
| Line-by-line view | Color-coded source code display | Green (covered), red (not covered), yellow (partially covered) |
| Missing lines | Specific line numbers not executed | Lines 45-52, 67 not covered |
| Branch coverage | Conditional branch execution status | Branch 3->4 not taken |
The XML coverage report (coverage.xml) contains:
<package name="dbgpt.rag" line-rate="0.82" branch-rate="0.75">: Package-level metrics<class name="EmbeddingEngine" filename="dbgpt/rag/embedding.py" line-rate="0.90">: Class-level metrics<line number="45" hits="0"/>: Line-level hit countsSources: .gitignore50-62
dbgpt.rag, dbgpt.core, dbgpt.model)--cov-report=term-missing to identify specific lines not tested--cov-branch to ensure both true and false paths of conditionals are tested.gitignore excludes tests/ from coverage measurement to focus on production codeSources: .gitignore50-62
DB-GPT uses pre-commit hooks to enforce code quality standards before commits reach the repository. This catches issues early in the development process.
Install pre-commit hooks: CONTRIBUTING.md83-86
After installation, hooks run automatically on git commit. The hooks validate:
Pre-commit Hook Execution Flow
Sources: CONTRIBUTING.md83-146
DB-GPT provides Makefile targets for common quality checks: CONTRIBUTING.md112-130
Code Formatting:
Runs ruff format to automatically format code according to Black-compatible style.
Linting Check:
Runs ruff check without auto-fixing to verify code style compliance.
Type Checking:
Runs mypy to validate type annotations and detect type errors.
Test Execution:
Runs the full test suite using pytest.
Development Workflow: CONTRIBUTING.md100-146
make fmt to format codemake test to verify tests passmake mypy to check typesmake fmt-check to verify lint rulesIf pre-commit hooks fail, fix the issues and re-run the commit command. The hooks ensure code quality before changes reach the remote repository.
Sources: CONTRIBUTING.md83-146
DB-GPT's testing infrastructure relies on several development dependencies defined in requirements/dev-requirements.txt1-18
Core Testing Framework
| Package | Version | Purpose |
|---|---|---|
pytest | Latest | Primary testing framework |
pytest-cov | Latest | Coverage reporting plugin |
pytest-asyncio | Latest | Async test support |
pytest-benchmark | Latest | Performance benchmarking |
pytest-integration | Latest | Integration test markers |
pytest-mock | Latest | Mocking utilities |
pytest-recording | Latest | VCR-based HTTP recording |
asynctest | Latest | Async testing utilities |
aioresponses | Latest | Async HTTP mocking |
HTTP and Recording:
httpx: Modern HTTP client for testing API callsvcrpy<6.0.0: Record and replay HTTP interactions for deterministic testspytesseract==0.3.10: OCR for document processing testsType Checking and Quality:
mypy==1.7.0: Static type checkerpre-commit: Git hook framework for automated checksSources: requirements/dev-requirements.txt1-18
Using uv (recommended): CONTRIBUTING.md56-66
Using pip:
Sources: CONTRIBUTING.md56-66 requirements/dev-requirements.txt1-18
DB-GPT provides a Dev Container configuration for consistent development environments with pre-installed testing tools.
The Dev Container setup is defined in .devcontainer.json1-80 and uses a custom Docker image built from .devcontainer/Dockerfile.dev1-56
Dev Container Features
VS Code Extensions: .devcontainer.json67-77
The Dev Container automatically installs testing-related extensions:
ms-python.python: Python language supportms-python.vscode-pylance: Python IntelliSensems-python.mypy-type-checker: Mypy integrationcharliermarsh.ruff: Ruff linter and formatterms-python.flake8: Flake8 linterms-python.autopep8: Code formattingPython Configuration: .devcontainer.json55-66
The Dev Container configures Python tools:
Post-create Setup: .devcontainer/post-create.sh1-70
After container creation, the script:
.envDevelopment Workflow in Dev Container: .devcontainer/README.md13-36
Sources: .devcontainer.json1-80 .devcontainer/Dockerfile.dev1-56 .devcontainer/post-create.sh1-70 .devcontainer/README.md1-37
DB-GPT uses static analysis tools to maintain code quality and prevent common errors before runtime. The primary tools are mypy for type checking and ruff for linting and formatting.
mypy is a static type checker for Python that validates type annotations and detects type-related errors before code execution. The .mypy_cache/ directory (excluded from git via .gitignore136) stores mypy's incremental checking cache.
Type Checking Configuration Structure
mypy Configuration Hierarchy
Global Configuration
The global [mypy] section excludes the tests/ directory from type checking:
This exclusion is necessary because:
MockConfig, monkeypatch.setattr) that violate type safetyCore Module Configuration
| Module Pattern | Configuration Options | Rationale |
|---|---|---|
[mypy-dbgpt.rag.*] | strict_optional=Falseignore_missing_imports=Truefollow_imports=skip | RAG components use optional types flexibly; many embedding/vector store libraries lack type stubs |
[mypy-dbgpt.app.*] | follow_imports=skip | Application layer integrates external services without complete type information |
[mypy-dbgpt.serve.*] | follow_imports=skip | Service layer uses FastAPI and other frameworks with dynamic typing patterns |
[mypy-dbgpt.model.*] | follow_imports=skip | Model layer interfaces with ML frameworks (PyTorch, vLLM) that have incomplete type stubs |
[mypy-dbgpt.util.*] | follow_imports=skip | Utility modules have diverse dependencies with varying type coverage |
Third-party Library Configurations
DB-GPT configures mypy to handle third-party libraries lacking type stubs or having incompatible type definitions. Configuration pattern: [mypy-<library>.*] with ignore_missing_imports=True.
Storage and Vector Store Libraries
| Library | Configuration Section | Purpose |
|---|---|---|
weaviate | [mypy-weaviate.*] | Weaviate vector database client |
pymilvus | [mypy-pymilvus.*] | Milvus vector database client |
elasticsearch | [mypy-elasticsearch.*] | Elasticsearch client |
msgpack | [mypy-msgpack.*] | Binary serialization library |
rocksdict | [mypy-rocksdict.*] | RocksDB Python bindings |
cryptography | [mypy-cryptography.*] | Cryptographic operations |
Data Source Libraries
| Library | Configuration Section | Purpose |
|---|---|---|
pyspark | [mypy-pyspark.*] | Apache Spark Python API |
sqlparse | [mypy-sqlparse.*] | SQL parsing library |
clickhouse_connect | [mypy-clickhouse_connect.*] | ClickHouse database client |
neo4j | [mypy-neo4j.*] | Neo4j graph database client |
Agent and NLP Libraries
| Library | Configuration Section | Purpose |
|---|---|---|
unstructured | [mypy-unstructured.*] | Document processing library |
ollama | [mypy-ollama.*] | Ollama API client |
pypdf | [mypy-pypdf.*] | PDF processing library |
networkx | [mypy-networkx.*] | Graph algorithms library |
seaborn | [mypy-seaborn.*] | Statistical visualization |
rich | [mypy-rich.*] | Terminal formatting |
qianfan | [mypy-qianfan.*] | Baidu Qianfan API client |
Pydantic Configuration
[mypy-pydantic.*] uses strict_optional=False and follow_imports=skip to accommodate Pydantic's dynamic model generation and metaclass patterns.
Running mypy
mypy generates a .mypy_cache/ directory (excluded from git via .gitignore136) for incremental type checking performance.
Common mypy Errors and Resolutions
| Error Type | Description | Resolution |
|---|---|---|
error: Missing type annotation | Variable lacks type hint | Add type annotation: var: str = "value" |
error: Incompatible types | Type mismatch in assignment | Check type compatibility or use cast |
error: Cannot find implementation or library stub | Missing type stubs for library | Add to .mypy.ini with ignore_missing_imports=True |
error: "Optional[X]" has no attribute "Y" | Accessing attribute on optional type | Check for None: if obj is not None: obj.method() |
ruff is a fast Python linter and code formatter that combines the functionality of multiple tools (flake8, isort, black) in a single binary.
ruff Features
Running ruff
Common ruff Rules
| Rule Code | Description | Category |
|---|---|---|
E501 | Line too long (>88 characters) | Style |
F401 | Module imported but unused | Pyflakes |
F841 | Local variable assigned but never used | Pyflakes |
I001 | Import block is un-sorted or un-formatted | isort |
W291 | Trailing whitespace | Whitespace |
C901 | Function is too complex | Complexity |
S101 | Use of assert detected | Security (bandit) |
Integration with Development Workflow
Type Annotation Guidelines
Add type hints to function signatures:
Use Optional for nullable values:
Use generic types for collections:
Use Protocol for structural typing:
Linting Best Practices
ruff check --fix before committingConfiguration Management
pyproject.toml or dedicated config files.mypy.ini sections for module-specific settingsSources: .mypy.ini1-127 examples/rag/embedding_rag_example.py1-59 examples/rag/rag_embedding_api_example.py1-89
The DB-GPT testing framework provides comprehensive testing infrastructure:
Key Test Categories:
Running Tests:
For contributing tests to the codebase, see Contributing Guidelines.
Sources: tests/unit_tests/test_plugins.py1-130 tests/unit_tests/vector_store/test_pgvector.py1-14 tests/unit_tests/embedding_engine/url_test.py1-21 tests/unit_tests/embedding_engine/document_test.py1-21
Refresh this wiki