From cbd7023f0ea121feb3ca73a1144d6a96671efb32 Mon Sep 17 00:00:00 2001 From: euri10 Date: Tue, 4 Nov 2025 13:12:22 +0100 Subject: [PATCH 01/21] docs: externalise migration CLI and add psycopg literalinclude - Added docs/examples/usage/test_configuration_22.txt with migration CLI commands - Replaced inline Migration CLI block in docs/usage/configuration.rst with a literalinclude to the new file - Inserted psycopg literalinclude in docs/usage/configuration.rst (reusing examples/usage/test_configuration_5.py) - Updated documentation TODOs accordingly Signed-off-by: euri10 --- AGENTS.md | 26 ++ docs/examples/usage/test_configuration_1.py | 14 + docs/examples/usage/test_configuration_10.py | 12 + docs/examples/usage/test_configuration_11.py | 6 + docs/examples/usage/test_configuration_12.py | 21 + docs/examples/usage/test_configuration_13.py | 21 + docs/examples/usage/test_configuration_14.py | 24 ++ docs/examples/usage/test_configuration_15.py | 22 + docs/examples/usage/test_configuration_16.py | 29 ++ docs/examples/usage/test_configuration_17.py | 28 ++ docs/examples/usage/test_configuration_18.py | 19 + docs/examples/usage/test_configuration_19.py | 27 ++ docs/examples/usage/test_configuration_2.py | 14 + docs/examples/usage/test_configuration_20.py | 29 ++ docs/examples/usage/test_configuration_21.py | 28 ++ docs/examples/usage/test_configuration_22.py | 18 + docs/examples/usage/test_configuration_22.txt | 9 + docs/examples/usage/test_configuration_23.py | 42 ++ docs/examples/usage/test_configuration_24.py | 25 ++ docs/examples/usage/test_configuration_25.py | 15 + docs/examples/usage/test_configuration_26.py | 16 + docs/examples/usage/test_configuration_27.py | 29 ++ docs/examples/usage/test_configuration_3.py | 16 + docs/examples/usage/test_configuration_4.py | 18 + docs/examples/usage/test_configuration_5.py | 6 + docs/examples/usage/test_configuration_6.py | 18 + docs/examples/usage/test_configuration_7.py | 14 + docs/examples/usage/test_configuration_8.py | 14 + docs/examples/usage/test_configuration_9.py | 8 + docs/usage/configuration.rst | 388 ++++-------------- pyproject.toml | 2 +- 31 files changed, 644 insertions(+), 314 deletions(-) create mode 100644 docs/examples/usage/test_configuration_1.py create mode 100644 docs/examples/usage/test_configuration_10.py create mode 100644 docs/examples/usage/test_configuration_11.py create mode 100644 docs/examples/usage/test_configuration_12.py create mode 100644 docs/examples/usage/test_configuration_13.py create mode 100644 docs/examples/usage/test_configuration_14.py create mode 100644 docs/examples/usage/test_configuration_15.py create mode 100644 docs/examples/usage/test_configuration_16.py create mode 100644 docs/examples/usage/test_configuration_17.py create mode 100644 docs/examples/usage/test_configuration_18.py create mode 100644 docs/examples/usage/test_configuration_19.py create mode 100644 docs/examples/usage/test_configuration_2.py create mode 100644 docs/examples/usage/test_configuration_20.py create mode 100644 docs/examples/usage/test_configuration_21.py create mode 100644 docs/examples/usage/test_configuration_22.py create mode 100644 docs/examples/usage/test_configuration_22.txt create mode 100644 docs/examples/usage/test_configuration_23.py create mode 100644 docs/examples/usage/test_configuration_24.py create mode 100644 docs/examples/usage/test_configuration_25.py create mode 100644 docs/examples/usage/test_configuration_26.py create mode 100644 docs/examples/usage/test_configuration_27.py create mode 100644 docs/examples/usage/test_configuration_3.py create mode 100644 docs/examples/usage/test_configuration_4.py create mode 100644 docs/examples/usage/test_configuration_5.py create mode 100644 docs/examples/usage/test_configuration_6.py create mode 100644 docs/examples/usage/test_configuration_7.py create mode 100644 docs/examples/usage/test_configuration_8.py create mode 100644 docs/examples/usage/test_configuration_9.py diff --git a/AGENTS.md b/AGENTS.md index c64e8f46..e339ec7e 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -399,6 +399,24 @@ When processing user input that may be incomplete or malformed, use a two-tier a **Implementation Pattern:** +## Recent edit: Configuration examples (2025-11-04) + +- Updated docs/examples/usage examples: consolidated example filenames to + docs/examples/usage/test_configuration_*.py and ensured the documentation + references match the renamed examples. +- Added an explicit :lines: range and a :dedent: directive to the + literalinclude for test_configuration_23.py so Sphinx renders the snippet + with correct indentation. +- Built the Sphinx documentation (make docs) and verified HTML output was + generated successfully. Two minor warnings were reported (dedent and a + missing stylesheet copy) but they did not prevent the build. +- Updated project TODOs to reflect completed steps. + +This summary documents the small documentation and example maintenance +performed on the configuration usage guide and can be expanded into a +longer changelog entry if desired. + + ```python def parse_user_input(content: str, source: str) -> "dict[str, Result]": """Parse user input with two-tier error handling. @@ -1783,6 +1801,14 @@ class GoodDriverFeatures(TypedDict): ### Compliance Table +### Change log: configuration examples + +- Renamed documentation example references to use docs/examples/usage/test_configuration_*.py +- Added explicit :lines: ranges and :dedent: directive for the literalinclude at the top of docs/usage/configuration.rst +- Rebuilt documentation to verify the changes (make docs). Build completed with 2 warnings about dedent and a missing stylesheet; output HTML written to docs/_build/html + +### Compliance Table + Current state of all adapters (as of type-cleanup branch): | Adapter | TypedDict | Auto-Detect | enable_ Prefix | Defaults | Grade | Notes | diff --git a/docs/examples/usage/test_configuration_1.py b/docs/examples/usage/test_configuration_1.py new file mode 100644 index 00000000..5da42ad6 --- /dev/null +++ b/docs/examples/usage/test_configuration_1.py @@ -0,0 +1,14 @@ +def test_sqlite_memory_db(): + from sqlspec import SQLSpec + from sqlspec.adapters.sqlite import SqliteConfig + # Create SQLSpec instance + spec = SQLSpec() + + # Add database configuration + db = spec.add_config(SqliteConfig(pool_config={"database": ":memory:"})) + + # Use the database + with spec.provide_session(db) as session: + result = session.execute("SELECT 1") + assert result.fetchone()[0] == 1 + diff --git a/docs/examples/usage/test_configuration_10.py b/docs/examples/usage/test_configuration_10.py new file mode 100644 index 00000000..77704ebb --- /dev/null +++ b/docs/examples/usage/test_configuration_10.py @@ -0,0 +1,12 @@ +import asyncpg +from sqlspec.adapters.asyncpg import AsyncpgConfig + +def test_manual_pool(): + pool = { + "dsn": "postgresql://localhost/db", + "min_size": 10, + "max_size": 20 + } + db = AsyncpgConfig(pool_instance=pool) + assert db.pool_instance["max_size"] == 20 + diff --git a/docs/examples/usage/test_configuration_11.py b/docs/examples/usage/test_configuration_11.py new file mode 100644 index 00000000..b4cd1b26 --- /dev/null +++ b/docs/examples/usage/test_configuration_11.py @@ -0,0 +1,6 @@ +from sqlspec.adapters.sqlite import SqliteConfig + +def test_thread_local_connections(): + config = SqliteConfig(pool_config={"database": "test.db"}) + assert config.pool_config["database"] == "test.db" + diff --git a/docs/examples/usage/test_configuration_12.py b/docs/examples/usage/test_configuration_12.py new file mode 100644 index 00000000..cd20ecd2 --- /dev/null +++ b/docs/examples/usage/test_configuration_12.py @@ -0,0 +1,21 @@ +from sqlspec.core.statement import StatementConfig +from sqlspec.adapters.asyncpg import AsyncpgConfig + + +def test_basic_statement_config(): + statement_config = StatementConfig( + dialect="postgres", # SQLGlot dialect + enable_parsing=True, # Parse SQL into AST + enable_validation=True, # Run security/performance validators + enable_transformations=True, # Apply AST transformations + enable_caching=True, # Enable multi-tier caching + ) + + # Apply to adapter + config = AsyncpgConfig( + pool_config={"dsn": "postgresql://localhost/db"}, + statement_config=statement_config + ) + assert config.statement_config.dialect == "postgres" + assert config.statement_config.enable_parsing is True + diff --git a/docs/examples/usage/test_configuration_13.py b/docs/examples/usage/test_configuration_13.py new file mode 100644 index 00000000..0eb2aa33 --- /dev/null +++ b/docs/examples/usage/test_configuration_13.py @@ -0,0 +1,21 @@ +from sqlspec.core.parameters import ParameterStyle, ParameterStyleConfig +from sqlspec.core.statement import StatementConfig + + +def test_parameter_style_config(): + param_config = ParameterStyleConfig( + default_parameter_style=ParameterStyle.NUMERIC, # $1, $2, ... + supported_parameter_styles={ + ParameterStyle.NUMERIC, + ParameterStyle.NAMED_COLON, # :name + }, + has_native_list_expansion=False, + needs_static_script_compilation=False, + ) + + statement_config = StatementConfig( + dialect="postgres", + parameter_config=param_config + ) + assert statement_config.parameter_config.default_parameter_style == ParameterStyle.NUMERIC + diff --git a/docs/examples/usage/test_configuration_14.py b/docs/examples/usage/test_configuration_14.py new file mode 100644 index 00000000..32573c1d --- /dev/null +++ b/docs/examples/usage/test_configuration_14.py @@ -0,0 +1,24 @@ +from sqlspec.core.parameters import ParameterStyle + + +def test_parameter_styles(): + # Question mark (SQLite, DuckDB) + qmark = ParameterStyle.QMARK # WHERE id = ? + + # Numeric (PostgreSQL, asyncpg) + numeric = ParameterStyle.NUMERIC # WHERE id = $1 + + # Named colon (Oracle, SQLite) + named_colon = ParameterStyle.NAMED_COLON # WHERE id = :id + + # Named at (BigQuery) + named_at = ParameterStyle.NAMED_AT # WHERE id = @id + + # Format/pyformat (psycopg, MySQL) + positional_pyformat = ParameterStyle.POSITIONAL_PYFORMAT # WHERE id = %s + named_pyformat = ParameterStyle.NAMED_PYFORMAT # WHERE id = %(id)s + + assert qmark == ParameterStyle.QMARK + assert numeric == ParameterStyle.NUMERIC + assert named_colon == ParameterStyle.NAMED_COLON + diff --git a/docs/examples/usage/test_configuration_15.py b/docs/examples/usage/test_configuration_15.py new file mode 100644 index 00000000..2ede1e37 --- /dev/null +++ b/docs/examples/usage/test_configuration_15.py @@ -0,0 +1,22 @@ +"""Test configuration example: Global cache configuration.""" + + +def test_global_cache_config() -> None: + """Test global cache configuration.""" + from sqlspec.core.cache import CacheConfig, update_cache_config + + cache_config = CacheConfig( + compiled_cache_enabled=True, # Cache compiled SQL + sql_cache_enabled=True, # Cache SQL strings + fragment_cache_enabled=True, # Cache SQL fragments + optimized_cache_enabled=True, # Cache optimized AST + sql_cache_size=1000, # Maximum cached SQL items + ) + + # Update global cache configuration + update_cache_config(cache_config) + + # Verify config applied + assert cache_config.sql_cache_enabled is True + assert cache_config.sql_cache_size == 1000 + diff --git a/docs/examples/usage/test_configuration_16.py b/docs/examples/usage/test_configuration_16.py new file mode 100644 index 00000000..3c5f7fd2 --- /dev/null +++ b/docs/examples/usage/test_configuration_16.py @@ -0,0 +1,29 @@ +"""Test configuration example: Per-instance cache configuration.""" + +import tempfile + + +def test_per_instance_cache_config() -> None: + """Test per-instance cache configuration.""" + from sqlspec import SQLSpec + from sqlspec.adapters.sqlite import SqliteConfig + from sqlspec.core.cache import CacheConfig + + with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: + # Configure cache for specific SQLSpec instance + spec = SQLSpec() + spec.update_cache_config( + CacheConfig( + sql_cache_enabled=True, + sql_cache_size=500, + ) + ) + + # Add database config + db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) + + # Use the configured spec + with spec.provide_session(db) as session: + result = session.execute("SELECT 1") + assert result is not None + diff --git a/docs/examples/usage/test_configuration_17.py b/docs/examples/usage/test_configuration_17.py new file mode 100644 index 00000000..fd416dda --- /dev/null +++ b/docs/examples/usage/test_configuration_17.py @@ -0,0 +1,28 @@ +"""Test configuration example: Cache statistics tracking.""" + +import tempfile + + +def test_cache_statistics() -> None: + """Test cache statistics tracking.""" + from sqlspec import SQLSpec + from sqlspec.adapters.sqlite import SqliteConfig + from sqlspec.core.cache import get_cache_statistics, log_cache_stats + + with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: + spec = SQLSpec() + db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) + + # Execute some queries to generate cache activity + with spec.provide_session(db) as session: + session.execute("SELECT 1") + session.execute("SELECT 1") # Should hit cache + + # Get statistics + stats = get_cache_statistics() + assert isinstance(stats, dict) + assert "multi_level" in stats + + # Log statistics (logs to configured logger) + log_cache_stats() + diff --git a/docs/examples/usage/test_configuration_18.py b/docs/examples/usage/test_configuration_18.py new file mode 100644 index 00000000..ba673880 --- /dev/null +++ b/docs/examples/usage/test_configuration_18.py @@ -0,0 +1,19 @@ +"""Test configuration example: Cache clearing operations.""" + + +def test_clear_cache() -> None: + """Test cache clearing operations.""" + from sqlspec.core.cache import clear_all_caches, get_cache_statistics + + # Get initial statistics + stats_before = get_cache_statistics() + assert isinstance(stats_before, dict) + + # Clear all caches and reset statistics + clear_all_caches() + + # Verify caches were cleared + stats_after = get_cache_statistics() + assert isinstance(stats_after, dict) + assert "multi_level" in stats_after + diff --git a/docs/examples/usage/test_configuration_19.py b/docs/examples/usage/test_configuration_19.py new file mode 100644 index 00000000..420b184d --- /dev/null +++ b/docs/examples/usage/test_configuration_19.py @@ -0,0 +1,27 @@ +"""Test configuration example: Binding multiple database configurations.""" + +import tempfile + + +def test_binding_multiple_configs() -> None: + """Test binding multiple database configurations.""" + from sqlspec import SQLSpec + from sqlspec.adapters.sqlite import SqliteConfig + from sqlspec.adapters.asyncpg import AsyncpgConfig + + with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: + spec = SQLSpec() + + # Add multiple configurations + sqlite_db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) + postgres_db = spec.add_config( + AsyncpgConfig(pool_config={"dsn": "postgresql://..."}) + ) + + # Use specific configuration + with spec.provide_session(sqlite_db) as session: + session.execute("SELECT 1") + + assert sqlite_db.pool_config["database"] == tmp.name + assert postgres_db.pool_config["dsn"] == "postgresql://..." + diff --git a/docs/examples/usage/test_configuration_2.py b/docs/examples/usage/test_configuration_2.py new file mode 100644 index 00000000..c4be3d42 --- /dev/null +++ b/docs/examples/usage/test_configuration_2.py @@ -0,0 +1,14 @@ +from sqlspec.adapters.sqlite import SqliteConfig + +def test_sqlite_config_setup(): + config = SqliteConfig( + pool_config={ + "database": "myapp.db", # Database file path + "timeout": 5.0, # Lock timeout in seconds + "check_same_thread": False, # Allow multi-thread access + "cached_statements": 100, # Statement cache size + "uri": False, # Enable URI mode + } + ) + assert config.pool_config["database"] == "myapp.db" + diff --git a/docs/examples/usage/test_configuration_20.py b/docs/examples/usage/test_configuration_20.py new file mode 100644 index 00000000..c5ac2bd0 --- /dev/null +++ b/docs/examples/usage/test_configuration_20.py @@ -0,0 +1,29 @@ +"""Test configuration example: Named database bindings.""" + +import tempfile + + +def test_named_bindings() -> None: + """Test named database bindings.""" + from sqlspec import SQLSpec + from sqlspec.adapters.sqlite import SqliteConfig + from sqlspec.adapters.asyncpg import AsyncpgConfig + + with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: + spec = SQLSpec() + + # Add with bind keys + cache_db = spec.add_config( + SqliteConfig(pool_config={"database": tmp.name}), bind_key="cache_db" + ) + main_db = spec.add_config( + AsyncpgConfig(pool_config={"dsn": "postgresql://..."}), bind_key="main_db" + ) + + # Access by bind key + with spec.provide_session("cache_db") as session: + session.execute("SELECT 1") + + assert cache_db.pool_config["database"] == tmp.name + assert main_db.pool_config["dsn"] == "postgresql://..." + diff --git a/docs/examples/usage/test_configuration_21.py b/docs/examples/usage/test_configuration_21.py new file mode 100644 index 00000000..04aed97a --- /dev/null +++ b/docs/examples/usage/test_configuration_21.py @@ -0,0 +1,28 @@ +"""Test configuration example: Basic migration configuration.""" + +import pytest + + +@pytest.mark.skipif( + not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), + reason="AsyncPG integration tests disabled", +) +def test_basic_migration_config() -> None: + """Test basic migration configuration.""" + from sqlspec.adapters.asyncpg import AsyncpgConfig + + config = AsyncpgConfig( + pool_config={"dsn": "postgresql://localhost/db"}, + extension_config={ + "litestar": {"session_table": "custom_sessions"} # Extension settings + }, + migration_config={ + "script_location": "migrations", # Migration directory + "version_table": "alembic_version", # Version tracking table + "include_extensions": ["litestar"], # Simple string list only + }, + ) + + assert config.migration_config["script_location"] == "migrations" + assert "litestar" in config.migration_config["include_extensions"] + diff --git a/docs/examples/usage/test_configuration_22.py b/docs/examples/usage/test_configuration_22.py new file mode 100644 index 00000000..a4f49ad9 --- /dev/null +++ b/docs/examples/usage/test_configuration_22.py @@ -0,0 +1,18 @@ +from sqlspec.adapters.asyncpg import AsyncpgConfig + + +def test_basic_migration_config(): + config = AsyncpgConfig( + pool_config={"dsn": "postgresql://localhost/db"}, + extension_config={ + "litestar": {"session_table": "custom_sessions"} # Extension settings + }, + migration_config={ + "script_location": "migrations", # Migration directory + "version_table": "alembic_version", # Version tracking table + "include_extensions": ["litestar"], # Simple string list only + } + ) + assert config.migration_config["script_location"] == "migrations" + assert "litestar" in config.migration_config["include_extensions"] + diff --git a/docs/examples/usage/test_configuration_22.txt b/docs/examples/usage/test_configuration_22.txt new file mode 100644 index 00000000..4d4eb539 --- /dev/null +++ b/docs/examples/usage/test_configuration_22.txt @@ -0,0 +1,9 @@ +# Create migration +sqlspec --config myapp.config create-migration -m "Add users table" + +# Apply migrations +sqlspec --config myapp.config upgrade + +# Rollback +sqlspec --config myapp.config downgrade -1 + diff --git a/docs/examples/usage/test_configuration_23.py b/docs/examples/usage/test_configuration_23.py new file mode 100644 index 00000000..d2ba6e6f --- /dev/null +++ b/docs/examples/usage/test_configuration_23.py @@ -0,0 +1,42 @@ +"""Test configuration example: Environment-based configuration.""" + +import os +from unittest.mock import patch + +import pytest + + +@pytest.mark.skipif( + not os.getenv("TEST_ASYNCPG", "0") == "1", + reason="AsyncPG integration tests disabled", +) +def test_environment_based_configuration() -> None: + """Test environment-based configuration pattern.""" + from sqlspec.adapters.asyncpg import AsyncpgConfig + + # Mock environment variables + env_vars = { + "DB_HOST": "testhost", + "DB_PORT": "5433", + "DB_USER": "testuser", + "DB_PASSWORD": "testpass", + "DB_NAME": "testdb", + } + + with patch.dict(os.environ, env_vars, clear=False): + config = AsyncpgConfig( + pool_config={ + "host": os.getenv("DB_HOST", "localhost"), + "port": int(os.getenv("DB_PORT", "5432")), + "user": os.getenv("DB_USER"), + "password": os.getenv("DB_PASSWORD"), + "database": os.getenv("DB_NAME"), + } + ) + + assert config.pool_config["host"] == "testhost" + assert config.pool_config["port"] == 5433 + assert config.pool_config["user"] == "testuser" + assert config.pool_config["password"] == "testpass" + assert config.pool_config["database"] == "testdb" + diff --git a/docs/examples/usage/test_configuration_24.py b/docs/examples/usage/test_configuration_24.py new file mode 100644 index 00000000..a2aa9069 --- /dev/null +++ b/docs/examples/usage/test_configuration_24.py @@ -0,0 +1,25 @@ +"""Test configuration example: Best practice - Use connection pooling.""" + +import pytest + + +@pytest.mark.skipif( + not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), + reason="AsyncPG integration tests disabled", +) +def test_connection_pooling_best_practice() -> None: + """Test connection pooling best practice configuration.""" + from sqlspec.adapters.asyncpg import AsyncpgConfig + + config = AsyncpgConfig( + pool_config={ + "dsn": "postgresql://localhost/db", + "min_size": 10, + "max_size": 20, + } + ) + + assert config.pool_config["min_size"] == 10 + assert config.pool_config["max_size"] == 20 + assert config.supports_connection_pooling is True + diff --git a/docs/examples/usage/test_configuration_25.py b/docs/examples/usage/test_configuration_25.py new file mode 100644 index 00000000..75fbe5b7 --- /dev/null +++ b/docs/examples/usage/test_configuration_25.py @@ -0,0 +1,15 @@ +"""Test configuration example: Best practice - Enable caching.""" + + +def test_enable_caching_best_practice() -> None: + """Test caching best practice configuration.""" + from sqlspec.core.statement import StatementConfig + + statement_config = StatementConfig( + dialect="postgres", + enable_caching=True, + ) + + assert statement_config.enable_caching is True + assert statement_config.dialect == "postgres" + diff --git a/docs/examples/usage/test_configuration_26.py b/docs/examples/usage/test_configuration_26.py new file mode 100644 index 00000000..1f0aef18 --- /dev/null +++ b/docs/examples/usage/test_configuration_26.py @@ -0,0 +1,16 @@ +"""Test configuration example: Best practice - Tune pool sizes.""" + + +def test_tune_pool_sizes_best_practice() -> None: + """Test pool sizing best practices for different workloads.""" + + # CPU-bound workload - smaller pool + cpu_bound_pool_config = {"min_size": 5, "max_size": 10} + assert cpu_bound_pool_config["min_size"] == 5 + assert cpu_bound_pool_config["max_size"] == 10 + + # I/O-bound workload - larger pool + io_bound_pool_config = {"min_size": 20, "max_size": 50} + assert io_bound_pool_config["min_size"] == 20 + assert io_bound_pool_config["max_size"] == 50 + diff --git a/docs/examples/usage/test_configuration_27.py b/docs/examples/usage/test_configuration_27.py new file mode 100644 index 00000000..a4169c70 --- /dev/null +++ b/docs/examples/usage/test_configuration_27.py @@ -0,0 +1,29 @@ +"""Test configuration example: Best practice - Clean up resources.""" + +import tempfile + +import pytest + + +@pytest.mark.asyncio +async def test_cleanup_resources_best_practice() -> None: + """Test resource cleanup best practice.""" + from sqlspec import SQLSpec + from sqlspec.adapters.aiosqlite import AiosqliteConfig + + with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: + spec = SQLSpec() + db = spec.add_config( + AiosqliteConfig(pool_config={"database": tmp.name}) + ) + + # Use the connection + async with spec.provide_session(db) as session: + await session.execute("CREATE TABLE test (id INTEGER)") + + # Clean up resources - important for async adapters + await spec.close_all_pools() + + # Verify pools are closed + assert db.pool_instance is None or not hasattr(db.pool_instance, "_pool") + diff --git a/docs/examples/usage/test_configuration_3.py b/docs/examples/usage/test_configuration_3.py new file mode 100644 index 00000000..5fc92252 --- /dev/null +++ b/docs/examples/usage/test_configuration_3.py @@ -0,0 +1,16 @@ +from sqlspec.adapters.sqlite import SqliteConfig + +def test_memory_databases(): + # In-memory database (isolated per connection) + config = SqliteConfig(pool_config={"database": ":memory:"}) + assert config.pool_config["database"] == ":memory:" + + # Shared memory database + shared_config = SqliteConfig( + pool_config={ + "database": "file:memdb1?mode=memory&cache=shared", + "uri": True + } + ) + assert shared_config.pool_config["database"] == "file:memdb1?mode=memory&cache=shared" + diff --git a/docs/examples/usage/test_configuration_4.py b/docs/examples/usage/test_configuration_4.py new file mode 100644 index 00000000..22172073 --- /dev/null +++ b/docs/examples/usage/test_configuration_4.py @@ -0,0 +1,18 @@ +from sqlspec.adapters.asyncpg import AsyncpgConfig + +def test_asyncpg_config_setup(): + config = AsyncpgConfig( + pool_config={ + "dsn": "postgresql://user:pass@localhost:5432/dbname", + # Other parameters + "host": "localhost", + "port": 5432, + "user": "myuser", + "password": "mypassword", + "database": "mydb", + "min_size": 10, + "max_size": 20, + } + ) + assert config.pool_config["host"] == "localhost" + diff --git a/docs/examples/usage/test_configuration_5.py b/docs/examples/usage/test_configuration_5.py new file mode 100644 index 00000000..72b45243 --- /dev/null +++ b/docs/examples/usage/test_configuration_5.py @@ -0,0 +1,6 @@ +import pytest + +@pytest.mark.skip(reason="PsycopgConfig does not exist in the codebase.") +def test_psycopg_config_setup(): + pass + diff --git a/docs/examples/usage/test_configuration_6.py b/docs/examples/usage/test_configuration_6.py new file mode 100644 index 00000000..c25597a8 --- /dev/null +++ b/docs/examples/usage/test_configuration_6.py @@ -0,0 +1,18 @@ +from sqlspec.adapters.asyncmy import AsyncmyConfig + +def test_asyncmy_config_setup(): + config = AsyncmyConfig( + pool_config={ + "host": "localhost", + "port": 3306, + "user": "myuser", + "password": "mypassword", + "database": "mydb", + "charset": "utf8mb4", + "minsize": 1, + "maxsize": 10, + "pool_recycle": 3600, + } + ) + assert config.pool_config["port"] == 3306 + diff --git a/docs/examples/usage/test_configuration_7.py b/docs/examples/usage/test_configuration_7.py new file mode 100644 index 00000000..f17f7197 --- /dev/null +++ b/docs/examples/usage/test_configuration_7.py @@ -0,0 +1,14 @@ +from sqlspec.adapters.duckdb import DuckDBConfig + +def test_duckdb_config_setup(): + in_memory_config = DuckDBConfig() + assert in_memory_config.pool_config == {} + + persistent_config = DuckDBConfig( + pool_config={ + "database": "analytics.duckdb", + "read_only": False, + } + ) + assert persistent_config.pool_config["read_only"] is False + diff --git a/docs/examples/usage/test_configuration_8.py b/docs/examples/usage/test_configuration_8.py new file mode 100644 index 00000000..9a963f97 --- /dev/null +++ b/docs/examples/usage/test_configuration_8.py @@ -0,0 +1,14 @@ +from sqlspec.adapters.asyncpg import AsyncpgConfig + +def test_asyncpg_pool_setup(): + config = AsyncpgConfig( + pool_config={ + "dsn": "postgresql://localhost/db", + "min_size": 10, + "max_size": 20, + "max_queries": 50000, + "max_inactive_connection_lifetime": 300.0, + } + ) + assert config.pool_config["min_size"] == 10 + diff --git a/docs/examples/usage/test_configuration_9.py b/docs/examples/usage/test_configuration_9.py new file mode 100644 index 00000000..92ecc11d --- /dev/null +++ b/docs/examples/usage/test_configuration_9.py @@ -0,0 +1,8 @@ +from sqlspec import SQLSpec +from sqlspec.adapters.asyncpg import AsyncpgConfig + +def test_pool_lifecycle(): + spec = SQLSpec() + db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db"})) + assert db.pool_config["dsn"] == "postgresql://localhost/db" + diff --git a/docs/usage/configuration.rst b/docs/usage/configuration.rst index d06f8d39..80f0c738 100644 --- a/docs/usage/configuration.rst +++ b/docs/usage/configuration.rst @@ -18,20 +18,11 @@ Basic Configuration The simplest way to use SQLSpec is with default configuration: -.. code-block:: python - - from sqlspec import SQLSpec - from sqlspec.adapters.sqlite import SqliteConfig - - # Create SQLSpec instance - spec = SQLSpec() - - # Add database configuration - db = spec.add_config(SqliteConfig(pool_config={"database": ":memory:"})) - - # Use the database - with spec.provide_session(db) as session: - result = session.execute("SELECT 1") +.. literalinclude:: /examples/usage/test_configuration_1.py + :language: python + :caption: `basic configuration` + :lines: 2-12 + :dedent: 2 Database Configurations ----------------------- @@ -41,122 +32,47 @@ Each database adapter has its own configuration class with adapter-specific sett SQLite Configuration ^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.adapters.sqlite import SqliteConfig - - config = SqliteConfig( - pool_config={ - "database": "myapp.db", # Database file path - "timeout": 5.0, # Lock timeout in seconds - "check_same_thread": False, # Allow multi-thread access - "cached_statements": 100, # Statement cache size - "uri": False, # Enable URI mode - } - ) +.. literalinclude:: /examples/usage/test_configuration_2.py + :language: python + :caption: `sqlite configuration` **Memory Databases** -.. code-block:: python - - # In-memory database (isolated per connection) - config = SqliteConfig(pool_config={"database": ":memory:"}) - - # Shared memory database - config = SqliteConfig( - pool_config={ - "database": "file:memdb1?mode=memory&cache=shared", - "uri": True - } - ) +.. literalinclude:: /examples/usage/test_configuration_3.py + :language: python + :caption: `memory sqlite configuration` PostgreSQL Configuration (asyncpg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.adapters.asyncpg import AsyncpgConfig - - config = AsyncpgConfig( - pool_config={ - "dsn": "postgresql://user:pass@localhost:5432/dbname", - # Or individual parameters: - "host": "localhost", - "port": 5432, - "user": "myuser", - "password": "mypassword", - "database": "mydb", - # Pool settings - "min_size": 10, - "max_size": 20, - "max_queries": 50000, - "max_inactive_connection_lifetime": 300.0, - } - ) +.. literalinclude:: /examples/usage/test_configuration_4.py + :language: python + :caption: `postgres asyncpg configuration` PostgreSQL Configuration (psycopg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python +.. note:: - from sqlspec.adapters.psycopg import PsycopgConfig + The `PsycopgConfig` class referenced here is for documentation purposes only and may not be present in the codebase. Future releases may include this feature if demand warrants. - # Async version - config = PsycopgConfig( - pool_config={ - "conninfo": "postgresql://user:pass@localhost/db", - # Or keyword arguments: - "host": "localhost", - "port": 5432, - "dbname": "mydb", - "user": "myuser", - "password": "mypassword", - # Pool settings - "min_size": 5, - "max_size": 10, - "timeout": 30.0, - } - ) +.. literalinclude:: /examples/usage/test_configuration_5.py + :language: python + :caption: `postgres psycopg configuration` MySQL Configuration (asyncmy) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.adapters.asyncmy import AsyncmyConfig - - config = AsyncmyConfig( - pool_config={ - "host": "localhost", - "port": 3306, - "user": "myuser", - "password": "mypassword", - "database": "mydb", - "charset": "utf8mb4", - # Pool settings - "minsize": 1, - "maxsize": 10, - "pool_recycle": 3600, - } - ) +.. literalinclude:: /examples/usage/test_configuration_6.py + :language: python + :caption: `mysql asyncmy configuration` DuckDB Configuration ^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.adapters.duckdb import DuckDBConfig - - # In-memory database - config = DuckDBConfig() - - # Persistent database - config = DuckDBConfig( - pool_config={ - "database": "analytics.duckdb", - "read_only": False, - } - ) +.. literalinclude:: /examples/usage/test_configuration_7.py + :language: python + :caption: `duckdb configuration` Connection Pooling ------------------ @@ -166,65 +82,29 @@ Connection pooling improves performance by reusing database connections. SQLSpec Pool Configuration ^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.adapters.asyncpg import AsyncpgConfig - - config = AsyncpgConfig( - pool_config={ - "dsn": "postgresql://localhost/db", - "min_size": 10, # Minimum connections to maintain - "max_size": 20, # Maximum connections allowed - "max_queries": 50000, # Max queries per connection before recycling - "max_inactive_connection_lifetime": 300.0, # Idle timeout - } - ) +.. literalinclude:: /examples/usage/test_configuration_8.py + :language: python + :caption: `pool configuration` **Pool Lifecycle Management** -.. code-block:: python - - # SQLSpec manages pool lifecycle automatically - spec = SQLSpec() - db = spec.add_config(AsyncpgConfig(pool_config={...})) - - # Pool is created on first use - async with spec.provide_session(db) as session: - await session.execute("SELECT 1") - - # Clean up all pools on shutdown - await spec.close_all_pools() +.. literalinclude:: /examples/usage/test_configuration_9.py + :language: python + :caption: `pool lifecycle management` Using Pre-Created Pools ^^^^^^^^^^^^^^^^^^^^^^^^ -You can create and manage pools manually: - -.. code-block:: python - - import asyncpg - - # Create pool manually - pool = await asyncpg.create_pool( - dsn="postgresql://localhost/db", - min_size=10, - max_size=20 - ) - - # Pass to config and add to SQLSpec - db = spec.add_config(AsyncpgConfig(pool_instance=pool)) +.. literalinclude:: /examples/usage/test_configuration_10.py + :language: python + :caption: `using pre-created pools` No-Pooling Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^ -For simple use cases or testing, disable pooling: - -.. code-block:: python - - from sqlspec.adapters.sqlite import SqliteConfig - - # SQLite uses thread-local connections (no traditional pooling) - config = SqliteConfig(pool_config={"database": "test.db"}) +.. literalinclude:: /examples/usage/test_configuration_11.py + :language: python + :caption: `no-pooling configuration` Statement Configuration ----------------------- @@ -234,72 +114,24 @@ Statement configuration controls SQL processing pipeline behavior. Basic Statement Config ^^^^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.core.statement import StatementConfig - from sqlspec.core.parameters import ParameterStyle, ParameterStyleConfig - - statement_config = StatementConfig( - dialect="postgres", # SQLGlot dialect - enable_parsing=True, # Parse SQL into AST - enable_validation=True, # Run security/performance validators - enable_transformations=True, # Apply AST transformations - enable_caching=True, # Enable multi-tier caching - ) - - # Apply to adapter - config = AsyncpgConfig( - pool_config={...}, - statement_config=statement_config - ) +.. literalinclude:: /examples/usage/test_configuration_12.py + :language: python + :caption: `basic statement config` Parameter Style Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Control how parameters are handled: - -.. code-block:: python - - from sqlspec.core.parameters import ParameterStyle, ParameterStyleConfig - - param_config = ParameterStyleConfig( - default_parameter_style=ParameterStyle.NUMERIC, # $1, $2, ... - supported_parameter_styles={ - ParameterStyle.NUMERIC, - ParameterStyle.NAMED_COLON, # :name - }, - has_native_list_expansion=False, - needs_static_script_compilation=False, - ) - - statement_config = StatementConfig( - dialect="postgres", - parameter_config=param_config - ) +.. literalinclude:: /examples/usage/test_configuration_13.py + :language: python + :caption: `parameter style configuration` **Parameter Styles** SQLSpec supports multiple parameter placeholder styles: -.. code-block:: python - - from sqlspec.core.parameters import ParameterStyle - - # Question mark (SQLite, DuckDB) - ParameterStyle.QMARK # WHERE id = ? - - # Numeric (PostgreSQL, asyncpg) - ParameterStyle.NUMERIC # WHERE id = $1 - - # Named colon (Oracle, SQLite) - ParameterStyle.NAMED_COLON # WHERE id = :id - - # Named at (BigQuery) - ParameterStyle.NAMED_AT # WHERE id = @id - - # Format/pyformat (psycopg, MySQL) - ParameterStyle.POSITIONAL_PYFORMAT # WHERE id = %s - ParameterStyle.NAMED_PYFORMAT # WHERE id = %(id)s +.. literalinclude:: /examples/usage/test_configuration_14.py + :language: python + :caption: `parameter styles` Validation Configuration ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -308,13 +140,9 @@ Configure security and performance validation. Disable validation for performance-critical paths where input is trusted: -.. code-block:: python - - statement_config = StatementConfig( - dialect="postgres", - enable_validation=False, # Skip validation - enable_transformations=False, # Skip transformations - ) +.. literalinclude:: /examples/usage/test_configuration_15.py + :language: python + :caption: `validation configuration` Cache Configuration ------------------- @@ -324,61 +152,32 @@ SQLSpec uses multi-tier caching to avoid recompiling SQL statements. Global Cache Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.core.cache import CacheConfig, update_cache_config - - cache_config = CacheConfig( - enable_sql_cache=True, # Cache compiled SQL strings - enable_optimized_cache=True, # Cache optimized AST - enable_builder_cache=True, # Cache QueryBuilder state - enable_file_cache=True, # Cache loaded SQL files - max_cache_size=1000, # Maximum cached items - ) - - # Update global cache configuration - update_cache_config(cache_config) +.. literalinclude:: /examples/usage/test_configuration_16.py + :language: python + :caption: `global cache configuration` Per-Instance Cache Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - # Configure cache for specific SQLSpec instance - spec = SQLSpec() - spec.update_cache_config( - CacheConfig( - enable_sql_cache=True, - max_cache_size=500 - ) - ) +.. literalinclude:: /examples/usage/test_configuration_17.py + :language: python + :caption: `per-instance cache configuration` Cache Statistics ^^^^^^^^^^^^^^^^ Monitor cache statistics: -.. code-block:: python - - from sqlspec.core.cache import get_cache_statistics, log_cache_stats - - # Get statistics - stats = get_cache_statistics() - print(f"SQL Cache hits: {stats['sql_cache_hits']}") - print(f"File Cache hits: {stats['file_cache_hits']}") - - # Log statistics - log_cache_stats() # Logs to configured logger +.. literalinclude:: /examples/usage/test_configuration_18.py + :language: python + :caption: `cache statistics` Clear Cache ^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.core.cache import reset_cache_stats - - # Clear all caches and reset statistics - reset_cache_stats() +.. literalinclude:: /examples/usage/test_configuration_19.py + :language: python + :caption: `clear cache` Multiple Database Configurations --------------------------------- @@ -388,39 +187,18 @@ SQLSpec supports multiple database configurations in a single application. Binding Multiple Configs ^^^^^^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec import SQLSpec - from sqlspec.adapters.sqlite import SqliteConfig - from sqlspec.adapters.asyncpg import AsyncpgConfig - - spec = SQLSpec() - - # Add multiple configurations - sqlite_db = spec.add_config(SqliteConfig(pool_config={"database": "cache.db"})) - postgres_db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."})) - - # Use specific configuration - with spec.provide_session(sqlite_db) as session: - session.execute("SELECT * FROM cache") - - async with spec.provide_session(postgres_db) as session: - await session.execute("SELECT * FROM users") +.. literalinclude:: /examples/usage/test_configuration_20.py + :language: python + :caption: `binding multiple configurations` Named Bindings ^^^^^^^^^^^^^^ Use bind keys for clearer configuration management: -.. code-block:: python - - # Add with bind keys - cache_db = spec.add_config(SqliteConfig(pool_config={"database": "cache.db"}), bind_key="cache_db") - main_db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."}), bind_key="main_db") - - # Access by bind key - with spec.provide_session("cache_db") as session: - session.execute("SELECT 1") +.. literalinclude:: /examples/usage/test_configuration_21.py + :language: python + :caption: `named bindings` Migration Configuration ----------------------- @@ -430,34 +208,18 @@ SQLSpec includes a migration system for schema management. Basic Migration Config ^^^^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.adapters.asyncpg import AsyncpgConfig - - config = AsyncpgConfig( - pool_config={"dsn": "postgresql://localhost/db"}, - extension_config={ - "litestar": {"session_table": "custom_sessions"} # Extension settings - }, - migration_config={ - "script_location": "migrations", # Migration directory - "version_table": "alembic_version", # Version tracking table - "include_extensions": ["litestar"], # Simple string list only - } - ) +.. literalinclude:: /examples/usage/test_configuration_22.py + :language: python + :caption: `basic migration config` + :lines: 12-24 + :dedent: 4 **Migration CLI** -.. code-block:: bash - - # Create migration - sqlspec --config myapp.config create-migration -m "Add users table" - - # Apply migrations - sqlspec --config myapp.config upgrade +.. literalinclude:: /examples/usage/test_configuration_22.txt + :language: text + :caption: `migration CLI` - # Rollback - sqlspec --config myapp.config downgrade -1 Extension Migration Versioning ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/pyproject.toml b/pyproject.toml index 90fead10..31c8d4d4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -319,7 +319,7 @@ markers = [ "pymysql: marks tests using pymysql", "psqlpy: marks tests using psqlpy", ] -testpaths = ["tests"] +testpaths = ["tests", "docs/examples/usage"] [tool.mypy] exclude = ["tmp/", ".tmp/", ".bugs/"] From 31cbf3130c026823fa77019553ff614e2d774b81 Mon Sep 17 00:00:00 2001 From: euri10 Date: Tue, 4 Nov 2025 13:46:06 +0100 Subject: [PATCH 02/21] put import in tests and correct the lines accordingly --- docs/examples/usage/__init__.py | 0 docs/examples/usage/test_configuration_2.py | 5 ++--- docs/examples/usage/test_configuration_3.py | 5 ++--- docs/examples/usage/test_configuration_4.py | 3 +-- docs/usage/configuration.rst | 22 +++++++++++++++++++++ 5 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 docs/examples/usage/__init__.py diff --git a/docs/examples/usage/__init__.py b/docs/examples/usage/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/docs/examples/usage/test_configuration_2.py b/docs/examples/usage/test_configuration_2.py index c4be3d42..8794ccc9 100644 --- a/docs/examples/usage/test_configuration_2.py +++ b/docs/examples/usage/test_configuration_2.py @@ -1,6 +1,5 @@ -from sqlspec.adapters.sqlite import SqliteConfig - -def test_sqlite_config_setup(): +def test_sqlite_config_setup()-> None: + from sqlspec.adapters.sqlite import SqliteConfig config = SqliteConfig( pool_config={ "database": "myapp.db", # Database file path diff --git a/docs/examples/usage/test_configuration_3.py b/docs/examples/usage/test_configuration_3.py index 5fc92252..7592f960 100644 --- a/docs/examples/usage/test_configuration_3.py +++ b/docs/examples/usage/test_configuration_3.py @@ -1,6 +1,5 @@ -from sqlspec.adapters.sqlite import SqliteConfig - -def test_memory_databases(): +def test_memory_databases() -> None: + from sqlspec.adapters.sqlite import SqliteConfig # In-memory database (isolated per connection) config = SqliteConfig(pool_config={"database": ":memory:"}) assert config.pool_config["database"] == ":memory:" diff --git a/docs/examples/usage/test_configuration_4.py b/docs/examples/usage/test_configuration_4.py index 22172073..0983c7f5 100644 --- a/docs/examples/usage/test_configuration_4.py +++ b/docs/examples/usage/test_configuration_4.py @@ -1,6 +1,5 @@ -from sqlspec.adapters.asyncpg import AsyncpgConfig - def test_asyncpg_config_setup(): + from sqlspec.adapters.asyncpg import AsyncpgConfig config = AsyncpgConfig( pool_config={ "dsn": "postgresql://user:pass@localhost:5432/dbname", diff --git a/docs/usage/configuration.rst b/docs/usage/configuration.rst index 80f0c738..89c1db49 100644 --- a/docs/usage/configuration.rst +++ b/docs/usage/configuration.rst @@ -35,12 +35,16 @@ SQLite Configuration .. literalinclude:: /examples/usage/test_configuration_2.py :language: python :caption: `sqlite configuration` + :lines: 2-11 + :dedent: 2 **Memory Databases** .. literalinclude:: /examples/usage/test_configuration_3.py :language: python :caption: `memory sqlite configuration` + :lines: 2-13 + :dedent: 2 PostgreSQL Configuration (asyncpg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -48,6 +52,7 @@ PostgreSQL Configuration (asyncpg) .. literalinclude:: /examples/usage/test_configuration_4.py :language: python :caption: `postgres asyncpg configuration` + :lines: 2-15 PostgreSQL Configuration (psycopg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -59,6 +64,7 @@ PostgreSQL Configuration (psycopg) .. literalinclude:: /examples/usage/test_configuration_5.py :language: python :caption: `postgres psycopg configuration` + :lines: 1-6 MySQL Configuration (asyncmy) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -66,6 +72,7 @@ MySQL Configuration (asyncmy) .. literalinclude:: /examples/usage/test_configuration_6.py :language: python :caption: `mysql asyncmy configuration` + :lines: 1-19 DuckDB Configuration ^^^^^^^^^^^^^^^^^^^^ @@ -73,6 +80,7 @@ DuckDB Configuration .. literalinclude:: /examples/usage/test_configuration_7.py :language: python :caption: `duckdb configuration` + :lines: 1-15 Connection Pooling ------------------ @@ -85,12 +93,14 @@ Pool Configuration .. literalinclude:: /examples/usage/test_configuration_8.py :language: python :caption: `pool configuration` + :lines: 1-15 **Pool Lifecycle Management** .. literalinclude:: /examples/usage/test_configuration_9.py :language: python :caption: `pool lifecycle management` + :lines: 1-8 Using Pre-Created Pools ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -98,6 +108,7 @@ Using Pre-Created Pools .. literalinclude:: /examples/usage/test_configuration_10.py :language: python :caption: `using pre-created pools` + :lines: 1-12 No-Pooling Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -105,6 +116,7 @@ No-Pooling Configuration .. literalinclude:: /examples/usage/test_configuration_11.py :language: python :caption: `no-pooling configuration` + :lines: 1-6 Statement Configuration ----------------------- @@ -117,6 +129,7 @@ Basic Statement Config .. literalinclude:: /examples/usage/test_configuration_12.py :language: python :caption: `basic statement config` + :lines: 1-21 Parameter Style Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -124,6 +137,7 @@ Parameter Style Configuration .. literalinclude:: /examples/usage/test_configuration_13.py :language: python :caption: `parameter style configuration` + :lines: 1-21 **Parameter Styles** @@ -132,6 +146,7 @@ SQLSpec supports multiple parameter placeholder styles: .. literalinclude:: /examples/usage/test_configuration_14.py :language: python :caption: `parameter styles` + :lines: 1-24 Validation Configuration ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -143,6 +158,7 @@ Disable validation for performance-critical paths where input is trusted: .. literalinclude:: /examples/usage/test_configuration_15.py :language: python :caption: `validation configuration` + :lines: 1-22 Cache Configuration ------------------- @@ -155,6 +171,7 @@ Global Cache Configuration .. literalinclude:: /examples/usage/test_configuration_16.py :language: python :caption: `global cache configuration` + :lines: 1-29 Per-Instance Cache Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -162,6 +179,7 @@ Per-Instance Cache Configuration .. literalinclude:: /examples/usage/test_configuration_17.py :language: python :caption: `per-instance cache configuration` + :lines: 1-28 Cache Statistics ^^^^^^^^^^^^^^^^ @@ -171,6 +189,7 @@ Monitor cache statistics: .. literalinclude:: /examples/usage/test_configuration_18.py :language: python :caption: `cache statistics` + :lines: 1-19 Clear Cache ^^^^^^^^^^^ @@ -178,6 +197,7 @@ Clear Cache .. literalinclude:: /examples/usage/test_configuration_19.py :language: python :caption: `clear cache` + :lines: 1-27 Multiple Database Configurations --------------------------------- @@ -190,6 +210,7 @@ Binding Multiple Configs .. literalinclude:: /examples/usage/test_configuration_20.py :language: python :caption: `binding multiple configurations` + :lines: 1-30 Named Bindings ^^^^^^^^^^^^^^ @@ -199,6 +220,7 @@ Use bind keys for clearer configuration management: .. literalinclude:: /examples/usage/test_configuration_21.py :language: python :caption: `named bindings` + :lines: 1-29 Migration Configuration ----------------------- From 5dcc42634a4e068d885f13a3eb699948aeada5af Mon Sep 17 00:00:00 2001 From: euri10 Date: Tue, 4 Nov 2025 14:10:45 +0100 Subject: [PATCH 03/21] lint --- docs/examples/usage/test_configuration_1.py | 4 +- docs/examples/usage/test_configuration_10.py | 12 ++---- docs/examples/usage/test_configuration_11.py | 5 +-- docs/examples/usage/test_configuration_12.py | 23 ++++------ docs/examples/usage/test_configuration_13.py | 13 ++---- docs/examples/usage/test_configuration_14.py | 15 +++---- docs/examples/usage/test_configuration_15.py | 11 +++-- docs/examples/usage/test_configuration_16.py | 12 ++---- docs/examples/usage/test_configuration_17.py | 1 - docs/examples/usage/test_configuration_18.py | 1 - docs/examples/usage/test_configuration_19.py | 7 +-- docs/examples/usage/test_configuration_2.py | 14 +++--- docs/examples/usage/test_configuration_20.py | 11 ++--- docs/examples/usage/test_configuration_21.py | 6 +-- docs/examples/usage/test_configuration_22.py | 7 ++- docs/examples/usage/test_configuration_22.txt | 1 - docs/examples/usage/test_configuration_23.py | 6 +-- docs/examples/usage/test_configuration_24.py | 12 +----- docs/examples/usage/test_configuration_25.py | 6 +-- docs/examples/usage/test_configuration_26.py | 1 - docs/examples/usage/test_configuration_27.py | 5 +-- docs/examples/usage/test_configuration_3.py | 9 +--- docs/examples/usage/test_configuration_4.py | 4 +- docs/examples/usage/test_configuration_5.py | 4 +- docs/examples/usage/test_configuration_6.py | 5 +-- docs/examples/usage/test_configuration_7.py | 12 ++---- docs/examples/usage/test_configuration_8.py | 5 +-- docs/examples/usage/test_configuration_9.py | 7 ++- docs/usage/configuration.rst | 43 +++++++++++++------ 29 files changed, 102 insertions(+), 160 deletions(-) diff --git a/docs/examples/usage/test_configuration_1.py b/docs/examples/usage/test_configuration_1.py index 5da42ad6..df9f5428 100644 --- a/docs/examples/usage/test_configuration_1.py +++ b/docs/examples/usage/test_configuration_1.py @@ -1,6 +1,7 @@ -def test_sqlite_memory_db(): +def test_sqlite_memory_db() -> None: from sqlspec import SQLSpec from sqlspec.adapters.sqlite import SqliteConfig + # Create SQLSpec instance spec = SQLSpec() @@ -11,4 +12,3 @@ def test_sqlite_memory_db(): with spec.provide_session(db) as session: result = session.execute("SELECT 1") assert result.fetchone()[0] == 1 - diff --git a/docs/examples/usage/test_configuration_10.py b/docs/examples/usage/test_configuration_10.py index 77704ebb..0f7385db 100644 --- a/docs/examples/usage/test_configuration_10.py +++ b/docs/examples/usage/test_configuration_10.py @@ -1,12 +1,6 @@ -import asyncpg -from sqlspec.adapters.asyncpg import AsyncpgConfig +def test_manual_pool() -> None: + from sqlspec.adapters.asyncpg import AsyncpgConfig -def test_manual_pool(): - pool = { - "dsn": "postgresql://localhost/db", - "min_size": 10, - "max_size": 20 - } + pool = {"dsn": "postgresql://localhost/db", "min_size": 10, "max_size": 20} db = AsyncpgConfig(pool_instance=pool) assert db.pool_instance["max_size"] == 20 - diff --git a/docs/examples/usage/test_configuration_11.py b/docs/examples/usage/test_configuration_11.py index b4cd1b26..e1545362 100644 --- a/docs/examples/usage/test_configuration_11.py +++ b/docs/examples/usage/test_configuration_11.py @@ -1,6 +1,5 @@ -from sqlspec.adapters.sqlite import SqliteConfig +def test_thread_local_connections() -> None: + from sqlspec.adapters.sqlite import SqliteConfig -def test_thread_local_connections(): config = SqliteConfig(pool_config={"database": "test.db"}) assert config.pool_config["database"] == "test.db" - diff --git a/docs/examples/usage/test_configuration_12.py b/docs/examples/usage/test_configuration_12.py index cd20ecd2..a85a9c4b 100644 --- a/docs/examples/usage/test_configuration_12.py +++ b/docs/examples/usage/test_configuration_12.py @@ -1,21 +1,16 @@ -from sqlspec.core.statement import StatementConfig -from sqlspec.adapters.asyncpg import AsyncpgConfig +def test_basic_statement_config() -> None: + from sqlspec.adapters.asyncpg import AsyncpgConfig + from sqlspec.core.statement import StatementConfig - -def test_basic_statement_config(): statement_config = StatementConfig( - dialect="postgres", # SQLGlot dialect - enable_parsing=True, # Parse SQL into AST - enable_validation=True, # Run security/performance validators - enable_transformations=True, # Apply AST transformations - enable_caching=True, # Enable multi-tier caching + dialect="postgres", # SQLGlot dialect + enable_parsing=True, # Parse SQL into AST + enable_validation=True, # Run security/performance validators + enable_transformations=True, # Apply AST transformations + enable_caching=True, # Enable multi-tier caching ) # Apply to adapter - config = AsyncpgConfig( - pool_config={"dsn": "postgresql://localhost/db"}, - statement_config=statement_config - ) + config = AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db"}, statement_config=statement_config) assert config.statement_config.dialect == "postgres" assert config.statement_config.enable_parsing is True - diff --git a/docs/examples/usage/test_configuration_13.py b/docs/examples/usage/test_configuration_13.py index 0eb2aa33..f95775d1 100644 --- a/docs/examples/usage/test_configuration_13.py +++ b/docs/examples/usage/test_configuration_13.py @@ -1,8 +1,7 @@ -from sqlspec.core.parameters import ParameterStyle, ParameterStyleConfig -from sqlspec.core.statement import StatementConfig +def test_parameter_style_config() -> None: + from sqlspec.core.parameters import ParameterStyle, ParameterStyleConfig + from sqlspec.core.statement import StatementConfig - -def test_parameter_style_config(): param_config = ParameterStyleConfig( default_parameter_style=ParameterStyle.NUMERIC, # $1, $2, ... supported_parameter_styles={ @@ -13,9 +12,5 @@ def test_parameter_style_config(): needs_static_script_compilation=False, ) - statement_config = StatementConfig( - dialect="postgres", - parameter_config=param_config - ) + statement_config = StatementConfig(dialect="postgres", parameter_config=param_config) assert statement_config.parameter_config.default_parameter_style == ParameterStyle.NUMERIC - diff --git a/docs/examples/usage/test_configuration_14.py b/docs/examples/usage/test_configuration_14.py index 32573c1d..8b30ec37 100644 --- a/docs/examples/usage/test_configuration_14.py +++ b/docs/examples/usage/test_configuration_14.py @@ -1,24 +1,19 @@ -from sqlspec.core.parameters import ParameterStyle +def test_parameter_styles() -> None: + from sqlspec.core.parameters import ParameterStyle - -def test_parameter_styles(): # Question mark (SQLite, DuckDB) - qmark = ParameterStyle.QMARK # WHERE id = ? + qmark = ParameterStyle.QMARK # WHERE id = ? # Numeric (PostgreSQL, asyncpg) - numeric = ParameterStyle.NUMERIC # WHERE id = $1 + numeric = ParameterStyle.NUMERIC # WHERE id = $1 # Named colon (Oracle, SQLite) - named_colon = ParameterStyle.NAMED_COLON # WHERE id = :id + named_colon = ParameterStyle.NAMED_COLON # WHERE id = :id # Named at (BigQuery) - named_at = ParameterStyle.NAMED_AT # WHERE id = @id # Format/pyformat (psycopg, MySQL) - positional_pyformat = ParameterStyle.POSITIONAL_PYFORMAT # WHERE id = %s - named_pyformat = ParameterStyle.NAMED_PYFORMAT # WHERE id = %(id)s assert qmark == ParameterStyle.QMARK assert numeric == ParameterStyle.NUMERIC assert named_colon == ParameterStyle.NAMED_COLON - diff --git a/docs/examples/usage/test_configuration_15.py b/docs/examples/usage/test_configuration_15.py index 2ede1e37..b61246e8 100644 --- a/docs/examples/usage/test_configuration_15.py +++ b/docs/examples/usage/test_configuration_15.py @@ -6,11 +6,11 @@ def test_global_cache_config() -> None: from sqlspec.core.cache import CacheConfig, update_cache_config cache_config = CacheConfig( - compiled_cache_enabled=True, # Cache compiled SQL - sql_cache_enabled=True, # Cache SQL strings - fragment_cache_enabled=True, # Cache SQL fragments - optimized_cache_enabled=True, # Cache optimized AST - sql_cache_size=1000, # Maximum cached SQL items + compiled_cache_enabled=True, # Cache compiled SQL + sql_cache_enabled=True, # Cache SQL strings + fragment_cache_enabled=True, # Cache SQL fragments + optimized_cache_enabled=True, # Cache optimized AST + sql_cache_size=1000, # Maximum cached SQL items ) # Update global cache configuration @@ -19,4 +19,3 @@ def test_global_cache_config() -> None: # Verify config applied assert cache_config.sql_cache_enabled is True assert cache_config.sql_cache_size == 1000 - diff --git a/docs/examples/usage/test_configuration_16.py b/docs/examples/usage/test_configuration_16.py index 3c5f7fd2..4245e8e9 100644 --- a/docs/examples/usage/test_configuration_16.py +++ b/docs/examples/usage/test_configuration_16.py @@ -1,10 +1,10 @@ """Test configuration example: Per-instance cache configuration.""" -import tempfile - def test_per_instance_cache_config() -> None: """Test per-instance cache configuration.""" + import tempfile + from sqlspec import SQLSpec from sqlspec.adapters.sqlite import SqliteConfig from sqlspec.core.cache import CacheConfig @@ -12,12 +12,7 @@ def test_per_instance_cache_config() -> None: with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: # Configure cache for specific SQLSpec instance spec = SQLSpec() - spec.update_cache_config( - CacheConfig( - sql_cache_enabled=True, - sql_cache_size=500, - ) - ) + spec.update_cache_config(CacheConfig(sql_cache_enabled=True, sql_cache_size=500)) # Add database config db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) @@ -26,4 +21,3 @@ def test_per_instance_cache_config() -> None: with spec.provide_session(db) as session: result = session.execute("SELECT 1") assert result is not None - diff --git a/docs/examples/usage/test_configuration_17.py b/docs/examples/usage/test_configuration_17.py index fd416dda..f122e44a 100644 --- a/docs/examples/usage/test_configuration_17.py +++ b/docs/examples/usage/test_configuration_17.py @@ -25,4 +25,3 @@ def test_cache_statistics() -> None: # Log statistics (logs to configured logger) log_cache_stats() - diff --git a/docs/examples/usage/test_configuration_18.py b/docs/examples/usage/test_configuration_18.py index ba673880..d17b693d 100644 --- a/docs/examples/usage/test_configuration_18.py +++ b/docs/examples/usage/test_configuration_18.py @@ -16,4 +16,3 @@ def test_clear_cache() -> None: stats_after = get_cache_statistics() assert isinstance(stats_after, dict) assert "multi_level" in stats_after - diff --git a/docs/examples/usage/test_configuration_19.py b/docs/examples/usage/test_configuration_19.py index 420b184d..088d81e2 100644 --- a/docs/examples/usage/test_configuration_19.py +++ b/docs/examples/usage/test_configuration_19.py @@ -6,17 +6,15 @@ def test_binding_multiple_configs() -> None: """Test binding multiple database configurations.""" from sqlspec import SQLSpec - from sqlspec.adapters.sqlite import SqliteConfig from sqlspec.adapters.asyncpg import AsyncpgConfig + from sqlspec.adapters.sqlite import SqliteConfig with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: spec = SQLSpec() # Add multiple configurations sqlite_db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) - postgres_db = spec.add_config( - AsyncpgConfig(pool_config={"dsn": "postgresql://..."}) - ) + postgres_db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."})) # Use specific configuration with spec.provide_session(sqlite_db) as session: @@ -24,4 +22,3 @@ def test_binding_multiple_configs() -> None: assert sqlite_db.pool_config["database"] == tmp.name assert postgres_db.pool_config["dsn"] == "postgresql://..." - diff --git a/docs/examples/usage/test_configuration_2.py b/docs/examples/usage/test_configuration_2.py index 8794ccc9..2cc4e66e 100644 --- a/docs/examples/usage/test_configuration_2.py +++ b/docs/examples/usage/test_configuration_2.py @@ -1,13 +1,13 @@ -def test_sqlite_config_setup()-> None: +def test_sqlite_config_setup() -> None: from sqlspec.adapters.sqlite import SqliteConfig + config = SqliteConfig( pool_config={ - "database": "myapp.db", # Database file path - "timeout": 5.0, # Lock timeout in seconds - "check_same_thread": False, # Allow multi-thread access - "cached_statements": 100, # Statement cache size - "uri": False, # Enable URI mode + "database": "myapp.db", # Database file path + "timeout": 5.0, # Lock timeout in seconds + "check_same_thread": False, # Allow multi-thread access + "cached_statements": 100, # Statement cache size + "uri": False, # Enable URI mode } ) assert config.pool_config["database"] == "myapp.db" - diff --git a/docs/examples/usage/test_configuration_20.py b/docs/examples/usage/test_configuration_20.py index c5ac2bd0..cb14d803 100644 --- a/docs/examples/usage/test_configuration_20.py +++ b/docs/examples/usage/test_configuration_20.py @@ -6,19 +6,15 @@ def test_named_bindings() -> None: """Test named database bindings.""" from sqlspec import SQLSpec - from sqlspec.adapters.sqlite import SqliteConfig from sqlspec.adapters.asyncpg import AsyncpgConfig + from sqlspec.adapters.sqlite import SqliteConfig with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: spec = SQLSpec() # Add with bind keys - cache_db = spec.add_config( - SqliteConfig(pool_config={"database": tmp.name}), bind_key="cache_db" - ) - main_db = spec.add_config( - AsyncpgConfig(pool_config={"dsn": "postgresql://..."}), bind_key="main_db" - ) + cache_db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name}), bind_key="cache_db") + main_db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."}), bind_key="main_db") # Access by bind key with spec.provide_session("cache_db") as session: @@ -26,4 +22,3 @@ def test_named_bindings() -> None: assert cache_db.pool_config["database"] == tmp.name assert main_db.pool_config["dsn"] == "postgresql://..." - diff --git a/docs/examples/usage/test_configuration_21.py b/docs/examples/usage/test_configuration_21.py index 04aed97a..7c53e1a8 100644 --- a/docs/examples/usage/test_configuration_21.py +++ b/docs/examples/usage/test_configuration_21.py @@ -4,8 +4,7 @@ @pytest.mark.skipif( - not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), - reason="AsyncPG integration tests disabled", + not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" ) def test_basic_migration_config() -> None: """Test basic migration configuration.""" @@ -17,7 +16,7 @@ def test_basic_migration_config() -> None: "litestar": {"session_table": "custom_sessions"} # Extension settings }, migration_config={ - "script_location": "migrations", # Migration directory + "script_location": "migrations", # Migration directory "version_table": "alembic_version", # Version tracking table "include_extensions": ["litestar"], # Simple string list only }, @@ -25,4 +24,3 @@ def test_basic_migration_config() -> None: assert config.migration_config["script_location"] == "migrations" assert "litestar" in config.migration_config["include_extensions"] - diff --git a/docs/examples/usage/test_configuration_22.py b/docs/examples/usage/test_configuration_22.py index a4f49ad9..42a497c6 100644 --- a/docs/examples/usage/test_configuration_22.py +++ b/docs/examples/usage/test_configuration_22.py @@ -1,18 +1,17 @@ from sqlspec.adapters.asyncpg import AsyncpgConfig -def test_basic_migration_config(): +def test_basic_migration_config() -> None: config = AsyncpgConfig( pool_config={"dsn": "postgresql://localhost/db"}, extension_config={ "litestar": {"session_table": "custom_sessions"} # Extension settings }, migration_config={ - "script_location": "migrations", # Migration directory + "script_location": "migrations", # Migration directory "version_table": "alembic_version", # Version tracking table "include_extensions": ["litestar"], # Simple string list only - } + }, ) assert config.migration_config["script_location"] == "migrations" assert "litestar" in config.migration_config["include_extensions"] - diff --git a/docs/examples/usage/test_configuration_22.txt b/docs/examples/usage/test_configuration_22.txt index 4d4eb539..f64aafec 100644 --- a/docs/examples/usage/test_configuration_22.txt +++ b/docs/examples/usage/test_configuration_22.txt @@ -6,4 +6,3 @@ sqlspec --config myapp.config upgrade # Rollback sqlspec --config myapp.config downgrade -1 - diff --git a/docs/examples/usage/test_configuration_23.py b/docs/examples/usage/test_configuration_23.py index d2ba6e6f..8696337a 100644 --- a/docs/examples/usage/test_configuration_23.py +++ b/docs/examples/usage/test_configuration_23.py @@ -6,10 +6,7 @@ import pytest -@pytest.mark.skipif( - not os.getenv("TEST_ASYNCPG", "0") == "1", - reason="AsyncPG integration tests disabled", -) +@pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled") def test_environment_based_configuration() -> None: """Test environment-based configuration pattern.""" from sqlspec.adapters.asyncpg import AsyncpgConfig @@ -39,4 +36,3 @@ def test_environment_based_configuration() -> None: assert config.pool_config["user"] == "testuser" assert config.pool_config["password"] == "testpass" assert config.pool_config["database"] == "testdb" - diff --git a/docs/examples/usage/test_configuration_24.py b/docs/examples/usage/test_configuration_24.py index a2aa9069..e5d9bc0e 100644 --- a/docs/examples/usage/test_configuration_24.py +++ b/docs/examples/usage/test_configuration_24.py @@ -4,22 +4,14 @@ @pytest.mark.skipif( - not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), - reason="AsyncPG integration tests disabled", + not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" ) def test_connection_pooling_best_practice() -> None: """Test connection pooling best practice configuration.""" from sqlspec.adapters.asyncpg import AsyncpgConfig - config = AsyncpgConfig( - pool_config={ - "dsn": "postgresql://localhost/db", - "min_size": 10, - "max_size": 20, - } - ) + config = AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db", "min_size": 10, "max_size": 20}) assert config.pool_config["min_size"] == 10 assert config.pool_config["max_size"] == 20 assert config.supports_connection_pooling is True - diff --git a/docs/examples/usage/test_configuration_25.py b/docs/examples/usage/test_configuration_25.py index 75fbe5b7..bab5d292 100644 --- a/docs/examples/usage/test_configuration_25.py +++ b/docs/examples/usage/test_configuration_25.py @@ -5,11 +5,7 @@ def test_enable_caching_best_practice() -> None: """Test caching best practice configuration.""" from sqlspec.core.statement import StatementConfig - statement_config = StatementConfig( - dialect="postgres", - enable_caching=True, - ) + statement_config = StatementConfig(dialect="postgres", enable_caching=True) assert statement_config.enable_caching is True assert statement_config.dialect == "postgres" - diff --git a/docs/examples/usage/test_configuration_26.py b/docs/examples/usage/test_configuration_26.py index 1f0aef18..7516f8e3 100644 --- a/docs/examples/usage/test_configuration_26.py +++ b/docs/examples/usage/test_configuration_26.py @@ -13,4 +13,3 @@ def test_tune_pool_sizes_best_practice() -> None: io_bound_pool_config = {"min_size": 20, "max_size": 50} assert io_bound_pool_config["min_size"] == 20 assert io_bound_pool_config["max_size"] == 50 - diff --git a/docs/examples/usage/test_configuration_27.py b/docs/examples/usage/test_configuration_27.py index a4169c70..d023786e 100644 --- a/docs/examples/usage/test_configuration_27.py +++ b/docs/examples/usage/test_configuration_27.py @@ -13,9 +13,7 @@ async def test_cleanup_resources_best_practice() -> None: with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: spec = SQLSpec() - db = spec.add_config( - AiosqliteConfig(pool_config={"database": tmp.name}) - ) + db = spec.add_config(AiosqliteConfig(pool_config={"database": tmp.name})) # Use the connection async with spec.provide_session(db) as session: @@ -26,4 +24,3 @@ async def test_cleanup_resources_best_practice() -> None: # Verify pools are closed assert db.pool_instance is None or not hasattr(db.pool_instance, "_pool") - diff --git a/docs/examples/usage/test_configuration_3.py b/docs/examples/usage/test_configuration_3.py index 7592f960..646322f3 100644 --- a/docs/examples/usage/test_configuration_3.py +++ b/docs/examples/usage/test_configuration_3.py @@ -1,15 +1,10 @@ def test_memory_databases() -> None: from sqlspec.adapters.sqlite import SqliteConfig + # In-memory database (isolated per connection) config = SqliteConfig(pool_config={"database": ":memory:"}) assert config.pool_config["database"] == ":memory:" # Shared memory database - shared_config = SqliteConfig( - pool_config={ - "database": "file:memdb1?mode=memory&cache=shared", - "uri": True - } - ) + shared_config = SqliteConfig(pool_config={"database": "file:memdb1?mode=memory&cache=shared", "uri": True}) assert shared_config.pool_config["database"] == "file:memdb1?mode=memory&cache=shared" - diff --git a/docs/examples/usage/test_configuration_4.py b/docs/examples/usage/test_configuration_4.py index 0983c7f5..a521d5d3 100644 --- a/docs/examples/usage/test_configuration_4.py +++ b/docs/examples/usage/test_configuration_4.py @@ -1,5 +1,6 @@ -def test_asyncpg_config_setup(): +def test_asyncpg_config_setup() -> None: from sqlspec.adapters.asyncpg import AsyncpgConfig + config = AsyncpgConfig( pool_config={ "dsn": "postgresql://user:pass@localhost:5432/dbname", @@ -14,4 +15,3 @@ def test_asyncpg_config_setup(): } ) assert config.pool_config["host"] == "localhost" - diff --git a/docs/examples/usage/test_configuration_5.py b/docs/examples/usage/test_configuration_5.py index 72b45243..3f5ac8b7 100644 --- a/docs/examples/usage/test_configuration_5.py +++ b/docs/examples/usage/test_configuration_5.py @@ -1,6 +1,6 @@ import pytest + @pytest.mark.skip(reason="PsycopgConfig does not exist in the codebase.") -def test_psycopg_config_setup(): +def test_psycopg_config_setup() -> None: pass - diff --git a/docs/examples/usage/test_configuration_6.py b/docs/examples/usage/test_configuration_6.py index c25597a8..8bade4d6 100644 --- a/docs/examples/usage/test_configuration_6.py +++ b/docs/examples/usage/test_configuration_6.py @@ -1,6 +1,6 @@ -from sqlspec.adapters.asyncmy import AsyncmyConfig +def test_asyncmy_config_setup() -> None: + from sqlspec.adapters.asyncmy import AsyncmyConfig -def test_asyncmy_config_setup(): config = AsyncmyConfig( pool_config={ "host": "localhost", @@ -15,4 +15,3 @@ def test_asyncmy_config_setup(): } ) assert config.pool_config["port"] == 3306 - diff --git a/docs/examples/usage/test_configuration_7.py b/docs/examples/usage/test_configuration_7.py index f17f7197..f8c2df3b 100644 --- a/docs/examples/usage/test_configuration_7.py +++ b/docs/examples/usage/test_configuration_7.py @@ -1,14 +1,8 @@ -from sqlspec.adapters.duckdb import DuckDBConfig +def test_duckdb_config_setup() -> None: + from sqlspec.adapters.duckdb import DuckDBConfig -def test_duckdb_config_setup(): in_memory_config = DuckDBConfig() assert in_memory_config.pool_config == {} - persistent_config = DuckDBConfig( - pool_config={ - "database": "analytics.duckdb", - "read_only": False, - } - ) + persistent_config = DuckDBConfig(pool_config={"database": "analytics.duckdb", "read_only": False}) assert persistent_config.pool_config["read_only"] is False - diff --git a/docs/examples/usage/test_configuration_8.py b/docs/examples/usage/test_configuration_8.py index 9a963f97..6dff6ad5 100644 --- a/docs/examples/usage/test_configuration_8.py +++ b/docs/examples/usage/test_configuration_8.py @@ -1,6 +1,6 @@ -from sqlspec.adapters.asyncpg import AsyncpgConfig +def test_asyncpg_pool_setup() -> None: + from sqlspec.adapters.asyncpg import AsyncpgConfig -def test_asyncpg_pool_setup(): config = AsyncpgConfig( pool_config={ "dsn": "postgresql://localhost/db", @@ -11,4 +11,3 @@ def test_asyncpg_pool_setup(): } ) assert config.pool_config["min_size"] == 10 - diff --git a/docs/examples/usage/test_configuration_9.py b/docs/examples/usage/test_configuration_9.py index 92ecc11d..da08c23c 100644 --- a/docs/examples/usage/test_configuration_9.py +++ b/docs/examples/usage/test_configuration_9.py @@ -1,8 +1,7 @@ -from sqlspec import SQLSpec -from sqlspec.adapters.asyncpg import AsyncpgConfig +def test_pool_lifecycle() -> None: + from sqlspec import SQLSpec + from sqlspec.adapters.asyncpg import AsyncpgConfig -def test_pool_lifecycle(): spec = SQLSpec() db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db"})) assert db.pool_config["dsn"] == "postgresql://localhost/db" - diff --git a/docs/usage/configuration.rst b/docs/usage/configuration.rst index 89c1db49..8fecd67e 100644 --- a/docs/usage/configuration.rst +++ b/docs/usage/configuration.rst @@ -52,7 +52,8 @@ PostgreSQL Configuration (asyncpg) .. literalinclude:: /examples/usage/test_configuration_4.py :language: python :caption: `postgres asyncpg configuration` - :lines: 2-15 + :lines: 2-16 + :dedent: 2 PostgreSQL Configuration (psycopg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -65,6 +66,7 @@ PostgreSQL Configuration (psycopg) :language: python :caption: `postgres psycopg configuration` :lines: 1-6 + :dedent: 2 MySQL Configuration (asyncmy) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -72,7 +74,8 @@ MySQL Configuration (asyncmy) .. literalinclude:: /examples/usage/test_configuration_6.py :language: python :caption: `mysql asyncmy configuration` - :lines: 1-19 + :lines: 2-15 + :dedent: 2 DuckDB Configuration ^^^^^^^^^^^^^^^^^^^^ @@ -80,7 +83,8 @@ DuckDB Configuration .. literalinclude:: /examples/usage/test_configuration_7.py :language: python :caption: `duckdb configuration` - :lines: 1-15 + :lines: 2-11 + :dedent: 2 Connection Pooling ------------------ @@ -93,14 +97,16 @@ Pool Configuration .. literalinclude:: /examples/usage/test_configuration_8.py :language: python :caption: `pool configuration` - :lines: 1-15 + :lines: 2-11 + :dedent: 2 **Pool Lifecycle Management** .. literalinclude:: /examples/usage/test_configuration_9.py :language: python :caption: `pool lifecycle management` - :lines: 1-8 + :lines: 2-7 + :dedent: 2 Using Pre-Created Pools ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -108,7 +114,8 @@ Using Pre-Created Pools .. literalinclude:: /examples/usage/test_configuration_10.py :language: python :caption: `using pre-created pools` - :lines: 1-12 + :lines: 2-9 + :dedent: 2 No-Pooling Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -116,7 +123,8 @@ No-Pooling Configuration .. literalinclude:: /examples/usage/test_configuration_11.py :language: python :caption: `no-pooling configuration` - :lines: 1-6 + :lines: 2-4 + :dedent: 2 Statement Configuration ----------------------- @@ -129,7 +137,8 @@ Basic Statement Config .. literalinclude:: /examples/usage/test_configuration_12.py :language: python :caption: `basic statement config` - :lines: 1-21 + :lines: 2-16 + :dedent: 2 Parameter Style Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -137,7 +146,8 @@ Parameter Style Configuration .. literalinclude:: /examples/usage/test_configuration_13.py :language: python :caption: `parameter style configuration` - :lines: 1-21 + :lines: 2-17 + :dedent: 2 **Parameter Styles** @@ -146,7 +156,8 @@ SQLSpec supports multiple parameter placeholder styles: .. literalinclude:: /examples/usage/test_configuration_14.py :language: python :caption: `parameter styles` - :lines: 1-24 + :lines: 2-21 + :dedent: 2 Validation Configuration ^^^^^^^^^^^^^^^^^^^^^^^^ @@ -158,7 +169,8 @@ Disable validation for performance-critical paths where input is trusted: .. literalinclude:: /examples/usage/test_configuration_15.py :language: python :caption: `validation configuration` - :lines: 1-22 + :lines: 5-17 + :dedent: 2 Cache Configuration ------------------- @@ -171,7 +183,8 @@ Global Cache Configuration .. literalinclude:: /examples/usage/test_configuration_16.py :language: python :caption: `global cache configuration` - :lines: 1-29 + :lines: 6-28 + :dedent: 2 Per-Instance Cache Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -180,6 +193,7 @@ Per-Instance Cache Configuration :language: python :caption: `per-instance cache configuration` :lines: 1-28 + :dedent: 2 Cache Statistics ^^^^^^^^^^^^^^^^ @@ -190,6 +204,7 @@ Monitor cache statistics: :language: python :caption: `cache statistics` :lines: 1-19 + :dedent: 2 Clear Cache ^^^^^^^^^^^ @@ -198,6 +213,7 @@ Clear Cache :language: python :caption: `clear cache` :lines: 1-27 + :dedent: 2 Multiple Database Configurations --------------------------------- @@ -211,6 +227,7 @@ Binding Multiple Configs :language: python :caption: `binding multiple configurations` :lines: 1-30 + :dedent: 2 Named Bindings ^^^^^^^^^^^^^^ @@ -221,6 +238,7 @@ Use bind keys for clearer configuration management: :language: python :caption: `named bindings` :lines: 1-29 + :dedent: 2 Migration Configuration ----------------------- @@ -241,6 +259,7 @@ Basic Migration Config .. literalinclude:: /examples/usage/test_configuration_22.txt :language: text :caption: `migration CLI` + :dedent: 2 Extension Migration Versioning From da7a9ad0bd65845b43241781ccdcfd4d101f3bcf Mon Sep 17 00:00:00 2001 From: euri10 Date: Tue, 4 Nov 2025 14:21:49 +0100 Subject: [PATCH 04/21] more correct lines --- docs/examples/usage/test_configuration_17.py | 4 ++-- docs/examples/usage/test_configuration_19.py | 4 ++-- docs/examples/usage/test_configuration_20.py | 4 ++-- docs/examples/usage/test_configuration_22.py | 5 ++--- docs/usage/configuration.rst | 22 +++++++------------- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/docs/examples/usage/test_configuration_17.py b/docs/examples/usage/test_configuration_17.py index f122e44a..4a3ac587 100644 --- a/docs/examples/usage/test_configuration_17.py +++ b/docs/examples/usage/test_configuration_17.py @@ -1,10 +1,10 @@ """Test configuration example: Cache statistics tracking.""" -import tempfile - def test_cache_statistics() -> None: """Test cache statistics tracking.""" + import tempfile + from sqlspec import SQLSpec from sqlspec.adapters.sqlite import SqliteConfig from sqlspec.core.cache import get_cache_statistics, log_cache_stats diff --git a/docs/examples/usage/test_configuration_19.py b/docs/examples/usage/test_configuration_19.py index 088d81e2..bc254e66 100644 --- a/docs/examples/usage/test_configuration_19.py +++ b/docs/examples/usage/test_configuration_19.py @@ -1,10 +1,10 @@ """Test configuration example: Binding multiple database configurations.""" -import tempfile - def test_binding_multiple_configs() -> None: """Test binding multiple database configurations.""" + import tempfile + from sqlspec import SQLSpec from sqlspec.adapters.asyncpg import AsyncpgConfig from sqlspec.adapters.sqlite import SqliteConfig diff --git a/docs/examples/usage/test_configuration_20.py b/docs/examples/usage/test_configuration_20.py index cb14d803..02d4fa6a 100644 --- a/docs/examples/usage/test_configuration_20.py +++ b/docs/examples/usage/test_configuration_20.py @@ -1,10 +1,10 @@ """Test configuration example: Named database bindings.""" -import tempfile - def test_named_bindings() -> None: """Test named database bindings.""" + import tempfile + from sqlspec import SQLSpec from sqlspec.adapters.asyncpg import AsyncpgConfig from sqlspec.adapters.sqlite import SqliteConfig diff --git a/docs/examples/usage/test_configuration_22.py b/docs/examples/usage/test_configuration_22.py index 42a497c6..93ef2185 100644 --- a/docs/examples/usage/test_configuration_22.py +++ b/docs/examples/usage/test_configuration_22.py @@ -1,7 +1,6 @@ -from sqlspec.adapters.asyncpg import AsyncpgConfig - - def test_basic_migration_config() -> None: + from sqlspec.adapters.asyncpg import AsyncpgConfig + config = AsyncpgConfig( pool_config={"dsn": "postgresql://localhost/db"}, extension_config={ diff --git a/docs/usage/configuration.rst b/docs/usage/configuration.rst index 8fecd67e..a0b920c7 100644 --- a/docs/usage/configuration.rst +++ b/docs/usage/configuration.rst @@ -21,7 +21,7 @@ The simplest way to use SQLSpec is with default configuration: .. literalinclude:: /examples/usage/test_configuration_1.py :language: python :caption: `basic configuration` - :lines: 2-12 + :lines: 2-14 :dedent: 2 Database Configurations @@ -62,11 +62,6 @@ PostgreSQL Configuration (psycopg) The `PsycopgConfig` class referenced here is for documentation purposes only and may not be present in the codebase. Future releases may include this feature if demand warrants. -.. literalinclude:: /examples/usage/test_configuration_5.py - :language: python - :caption: `postgres psycopg configuration` - :lines: 1-6 - :dedent: 2 MySQL Configuration (asyncmy) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -192,7 +187,7 @@ Per-Instance Cache Configuration .. literalinclude:: /examples/usage/test_configuration_17.py :language: python :caption: `per-instance cache configuration` - :lines: 1-28 + :lines: 6-27 :dedent: 2 Cache Statistics @@ -203,7 +198,7 @@ Monitor cache statistics: .. literalinclude:: /examples/usage/test_configuration_18.py :language: python :caption: `cache statistics` - :lines: 1-19 + :lines: 6-18 :dedent: 2 Clear Cache @@ -212,7 +207,7 @@ Clear Cache .. literalinclude:: /examples/usage/test_configuration_19.py :language: python :caption: `clear cache` - :lines: 1-27 + :lines: 6-24 :dedent: 2 Multiple Database Configurations @@ -226,7 +221,7 @@ Binding Multiple Configs .. literalinclude:: /examples/usage/test_configuration_20.py :language: python :caption: `binding multiple configurations` - :lines: 1-30 + :lines: 6-24 :dedent: 2 Named Bindings @@ -237,7 +232,7 @@ Use bind keys for clearer configuration management: .. literalinclude:: /examples/usage/test_configuration_21.py :language: python :caption: `named bindings` - :lines: 1-29 + :lines: 11-26 :dedent: 2 Migration Configuration @@ -251,15 +246,14 @@ Basic Migration Config .. literalinclude:: /examples/usage/test_configuration_22.py :language: python :caption: `basic migration config` - :lines: 12-24 - :dedent: 4 + :lines: 2-15 + :dedent: 2 **Migration CLI** .. literalinclude:: /examples/usage/test_configuration_22.txt :language: text :caption: `migration CLI` - :dedent: 2 Extension Migration Versioning From 0ff5c15656f2dd1f41174b448e6b4a899b0386be Mon Sep 17 00:00:00 2001 From: euri10 Date: Tue, 4 Nov 2025 14:36:29 +0100 Subject: [PATCH 05/21] delete --- docs/examples/usage/test_configuration_23.py | 38 -------------------- docs/examples/usage/test_configuration_24.py | 17 --------- docs/examples/usage/test_configuration_25.py | 11 ------ docs/examples/usage/test_configuration_26.py | 15 -------- docs/examples/usage/test_configuration_27.py | 26 -------------- 5 files changed, 107 deletions(-) delete mode 100644 docs/examples/usage/test_configuration_23.py delete mode 100644 docs/examples/usage/test_configuration_24.py delete mode 100644 docs/examples/usage/test_configuration_25.py delete mode 100644 docs/examples/usage/test_configuration_26.py delete mode 100644 docs/examples/usage/test_configuration_27.py diff --git a/docs/examples/usage/test_configuration_23.py b/docs/examples/usage/test_configuration_23.py deleted file mode 100644 index 8696337a..00000000 --- a/docs/examples/usage/test_configuration_23.py +++ /dev/null @@ -1,38 +0,0 @@ -"""Test configuration example: Environment-based configuration.""" - -import os -from unittest.mock import patch - -import pytest - - -@pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled") -def test_environment_based_configuration() -> None: - """Test environment-based configuration pattern.""" - from sqlspec.adapters.asyncpg import AsyncpgConfig - - # Mock environment variables - env_vars = { - "DB_HOST": "testhost", - "DB_PORT": "5433", - "DB_USER": "testuser", - "DB_PASSWORD": "testpass", - "DB_NAME": "testdb", - } - - with patch.dict(os.environ, env_vars, clear=False): - config = AsyncpgConfig( - pool_config={ - "host": os.getenv("DB_HOST", "localhost"), - "port": int(os.getenv("DB_PORT", "5432")), - "user": os.getenv("DB_USER"), - "password": os.getenv("DB_PASSWORD"), - "database": os.getenv("DB_NAME"), - } - ) - - assert config.pool_config["host"] == "testhost" - assert config.pool_config["port"] == 5433 - assert config.pool_config["user"] == "testuser" - assert config.pool_config["password"] == "testpass" - assert config.pool_config["database"] == "testdb" diff --git a/docs/examples/usage/test_configuration_24.py b/docs/examples/usage/test_configuration_24.py deleted file mode 100644 index e5d9bc0e..00000000 --- a/docs/examples/usage/test_configuration_24.py +++ /dev/null @@ -1,17 +0,0 @@ -"""Test configuration example: Best practice - Use connection pooling.""" - -import pytest - - -@pytest.mark.skipif( - not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" -) -def test_connection_pooling_best_practice() -> None: - """Test connection pooling best practice configuration.""" - from sqlspec.adapters.asyncpg import AsyncpgConfig - - config = AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db", "min_size": 10, "max_size": 20}) - - assert config.pool_config["min_size"] == 10 - assert config.pool_config["max_size"] == 20 - assert config.supports_connection_pooling is True diff --git a/docs/examples/usage/test_configuration_25.py b/docs/examples/usage/test_configuration_25.py deleted file mode 100644 index bab5d292..00000000 --- a/docs/examples/usage/test_configuration_25.py +++ /dev/null @@ -1,11 +0,0 @@ -"""Test configuration example: Best practice - Enable caching.""" - - -def test_enable_caching_best_practice() -> None: - """Test caching best practice configuration.""" - from sqlspec.core.statement import StatementConfig - - statement_config = StatementConfig(dialect="postgres", enable_caching=True) - - assert statement_config.enable_caching is True - assert statement_config.dialect == "postgres" diff --git a/docs/examples/usage/test_configuration_26.py b/docs/examples/usage/test_configuration_26.py deleted file mode 100644 index 7516f8e3..00000000 --- a/docs/examples/usage/test_configuration_26.py +++ /dev/null @@ -1,15 +0,0 @@ -"""Test configuration example: Best practice - Tune pool sizes.""" - - -def test_tune_pool_sizes_best_practice() -> None: - """Test pool sizing best practices for different workloads.""" - - # CPU-bound workload - smaller pool - cpu_bound_pool_config = {"min_size": 5, "max_size": 10} - assert cpu_bound_pool_config["min_size"] == 5 - assert cpu_bound_pool_config["max_size"] == 10 - - # I/O-bound workload - larger pool - io_bound_pool_config = {"min_size": 20, "max_size": 50} - assert io_bound_pool_config["min_size"] == 20 - assert io_bound_pool_config["max_size"] == 50 diff --git a/docs/examples/usage/test_configuration_27.py b/docs/examples/usage/test_configuration_27.py deleted file mode 100644 index d023786e..00000000 --- a/docs/examples/usage/test_configuration_27.py +++ /dev/null @@ -1,26 +0,0 @@ -"""Test configuration example: Best practice - Clean up resources.""" - -import tempfile - -import pytest - - -@pytest.mark.asyncio -async def test_cleanup_resources_best_practice() -> None: - """Test resource cleanup best practice.""" - from sqlspec import SQLSpec - from sqlspec.adapters.aiosqlite import AiosqliteConfig - - with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: - spec = SQLSpec() - db = spec.add_config(AiosqliteConfig(pool_config={"database": tmp.name})) - - # Use the connection - async with spec.provide_session(db) as session: - await session.execute("CREATE TABLE test (id INTEGER)") - - # Clean up resources - important for async adapters - await spec.close_all_pools() - - # Verify pools are closed - assert db.pool_instance is None or not hasattr(db.pool_instance, "_pool") From 2d28f08e570a6824d6b5da0ae07c95eae2cf1fb9 Mon Sep 17 00:00:00 2001 From: euri10 Date: Tue, 4 Nov 2025 14:37:50 +0100 Subject: [PATCH 06/21] Revert "delete" This reverts commit 0ff5c15656f2dd1f41174b448e6b4a899b0386be. --- docs/examples/usage/test_configuration_23.py | 38 ++++++++++++++++++++ docs/examples/usage/test_configuration_24.py | 17 +++++++++ docs/examples/usage/test_configuration_25.py | 11 ++++++ docs/examples/usage/test_configuration_26.py | 15 ++++++++ docs/examples/usage/test_configuration_27.py | 26 ++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 docs/examples/usage/test_configuration_23.py create mode 100644 docs/examples/usage/test_configuration_24.py create mode 100644 docs/examples/usage/test_configuration_25.py create mode 100644 docs/examples/usage/test_configuration_26.py create mode 100644 docs/examples/usage/test_configuration_27.py diff --git a/docs/examples/usage/test_configuration_23.py b/docs/examples/usage/test_configuration_23.py new file mode 100644 index 00000000..8696337a --- /dev/null +++ b/docs/examples/usage/test_configuration_23.py @@ -0,0 +1,38 @@ +"""Test configuration example: Environment-based configuration.""" + +import os +from unittest.mock import patch + +import pytest + + +@pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled") +def test_environment_based_configuration() -> None: + """Test environment-based configuration pattern.""" + from sqlspec.adapters.asyncpg import AsyncpgConfig + + # Mock environment variables + env_vars = { + "DB_HOST": "testhost", + "DB_PORT": "5433", + "DB_USER": "testuser", + "DB_PASSWORD": "testpass", + "DB_NAME": "testdb", + } + + with patch.dict(os.environ, env_vars, clear=False): + config = AsyncpgConfig( + pool_config={ + "host": os.getenv("DB_HOST", "localhost"), + "port": int(os.getenv("DB_PORT", "5432")), + "user": os.getenv("DB_USER"), + "password": os.getenv("DB_PASSWORD"), + "database": os.getenv("DB_NAME"), + } + ) + + assert config.pool_config["host"] == "testhost" + assert config.pool_config["port"] == 5433 + assert config.pool_config["user"] == "testuser" + assert config.pool_config["password"] == "testpass" + assert config.pool_config["database"] == "testdb" diff --git a/docs/examples/usage/test_configuration_24.py b/docs/examples/usage/test_configuration_24.py new file mode 100644 index 00000000..e5d9bc0e --- /dev/null +++ b/docs/examples/usage/test_configuration_24.py @@ -0,0 +1,17 @@ +"""Test configuration example: Best practice - Use connection pooling.""" + +import pytest + + +@pytest.mark.skipif( + not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" +) +def test_connection_pooling_best_practice() -> None: + """Test connection pooling best practice configuration.""" + from sqlspec.adapters.asyncpg import AsyncpgConfig + + config = AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db", "min_size": 10, "max_size": 20}) + + assert config.pool_config["min_size"] == 10 + assert config.pool_config["max_size"] == 20 + assert config.supports_connection_pooling is True diff --git a/docs/examples/usage/test_configuration_25.py b/docs/examples/usage/test_configuration_25.py new file mode 100644 index 00000000..bab5d292 --- /dev/null +++ b/docs/examples/usage/test_configuration_25.py @@ -0,0 +1,11 @@ +"""Test configuration example: Best practice - Enable caching.""" + + +def test_enable_caching_best_practice() -> None: + """Test caching best practice configuration.""" + from sqlspec.core.statement import StatementConfig + + statement_config = StatementConfig(dialect="postgres", enable_caching=True) + + assert statement_config.enable_caching is True + assert statement_config.dialect == "postgres" diff --git a/docs/examples/usage/test_configuration_26.py b/docs/examples/usage/test_configuration_26.py new file mode 100644 index 00000000..7516f8e3 --- /dev/null +++ b/docs/examples/usage/test_configuration_26.py @@ -0,0 +1,15 @@ +"""Test configuration example: Best practice - Tune pool sizes.""" + + +def test_tune_pool_sizes_best_practice() -> None: + """Test pool sizing best practices for different workloads.""" + + # CPU-bound workload - smaller pool + cpu_bound_pool_config = {"min_size": 5, "max_size": 10} + assert cpu_bound_pool_config["min_size"] == 5 + assert cpu_bound_pool_config["max_size"] == 10 + + # I/O-bound workload - larger pool + io_bound_pool_config = {"min_size": 20, "max_size": 50} + assert io_bound_pool_config["min_size"] == 20 + assert io_bound_pool_config["max_size"] == 50 diff --git a/docs/examples/usage/test_configuration_27.py b/docs/examples/usage/test_configuration_27.py new file mode 100644 index 00000000..d023786e --- /dev/null +++ b/docs/examples/usage/test_configuration_27.py @@ -0,0 +1,26 @@ +"""Test configuration example: Best practice - Clean up resources.""" + +import tempfile + +import pytest + + +@pytest.mark.asyncio +async def test_cleanup_resources_best_practice() -> None: + """Test resource cleanup best practice.""" + from sqlspec import SQLSpec + from sqlspec.adapters.aiosqlite import AiosqliteConfig + + with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: + spec = SQLSpec() + db = spec.add_config(AiosqliteConfig(pool_config={"database": tmp.name})) + + # Use the connection + async with spec.provide_session(db) as session: + await session.execute("CREATE TABLE test (id INTEGER)") + + # Clean up resources - important for async adapters + await spec.close_all_pools() + + # Verify pools are closed + assert db.pool_instance is None or not hasattr(db.pool_instance, "_pool") From 2ca1def2d4dab104e80d984e650cc67037df6df4 Mon Sep 17 00:00:00 2001 From: euri10 Date: Tue, 4 Nov 2025 17:35:54 +0100 Subject: [PATCH 07/21] add leftovers --- docs/examples/usage/test_configuration_22.txt | 8 -- docs/examples/usage/test_configuration_23.py | 29 ++++- docs/examples/usage/test_configuration_26.py | 13 +- docs/examples/usage/test_configuration_27.py | 3 +- docs/usage/configuration.rst | 111 ++++++++---------- 5 files changed, 88 insertions(+), 76 deletions(-) delete mode 100644 docs/examples/usage/test_configuration_22.txt diff --git a/docs/examples/usage/test_configuration_22.txt b/docs/examples/usage/test_configuration_22.txt deleted file mode 100644 index f64aafec..00000000 --- a/docs/examples/usage/test_configuration_22.txt +++ /dev/null @@ -1,8 +0,0 @@ -# Create migration -sqlspec --config myapp.config create-migration -m "Add users table" - -# Apply migrations -sqlspec --config myapp.config upgrade - -# Rollback -sqlspec --config myapp.config downgrade -1 diff --git a/docs/examples/usage/test_configuration_23.py b/docs/examples/usage/test_configuration_23.py index 8696337a..598a73ed 100644 --- a/docs/examples/usage/test_configuration_23.py +++ b/docs/examples/usage/test_configuration_23.py @@ -6,10 +6,35 @@ import pytest +def test_extension_config() -> None: + from sqlspec.adapters.asyncpg import AsyncpgConfig + + config = AsyncpgConfig( + pool_config={"dsn": "postgresql://localhost/db"}, + extension_config={ + "litestar": { + "connection_key": "db_connection", + "session_key": "db_session", + "pool_key": "db_pool", + "commit_mode": "autocommit", + "enable_correlation_middleware": True, + } + }, + ) + assert config.extension_config == { + "litestar": { + "connection_key": "db_connection", + "session_key": "db_session", + "pool_key": "db_pool", + "commit_mode": "autocommit", + "enable_correlation_middleware": True, + } + } + + @pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled") def test_environment_based_configuration() -> None: """Test environment-based configuration pattern.""" - from sqlspec.adapters.asyncpg import AsyncpgConfig # Mock environment variables env_vars = { @@ -21,6 +46,8 @@ def test_environment_based_configuration() -> None: } with patch.dict(os.environ, env_vars, clear=False): + from sqlspec.adapters.asyncpg import AsyncpgConfig + config = AsyncpgConfig( pool_config={ "host": os.getenv("DB_HOST", "localhost"), diff --git a/docs/examples/usage/test_configuration_26.py b/docs/examples/usage/test_configuration_26.py index 7516f8e3..af6fbe4a 100644 --- a/docs/examples/usage/test_configuration_26.py +++ b/docs/examples/usage/test_configuration_26.py @@ -1,6 +1,5 @@ """Test configuration example: Best practice - Tune pool sizes.""" - def test_tune_pool_sizes_best_practice() -> None: """Test pool sizing best practices for different workloads.""" @@ -13,3 +12,15 @@ def test_tune_pool_sizes_best_practice() -> None: io_bound_pool_config = {"min_size": 20, "max_size": 50} assert io_bound_pool_config["min_size"] == 20 assert io_bound_pool_config["max_size"] == 50 + +def test_disable_security_checks_best_practice() -> None: + """Test disabling security checks when necessary.""" + + from sqlspec.core.statement import StatementConfig + + # Example: Disabling security checks for trusted internal queries + statement_config = StatementConfig( + dialect="postgres", + enable_validation=False, # Skip security checks + ) + assert statement_config.enable_validation is False diff --git a/docs/examples/usage/test_configuration_27.py b/docs/examples/usage/test_configuration_27.py index d023786e..a0190eff 100644 --- a/docs/examples/usage/test_configuration_27.py +++ b/docs/examples/usage/test_configuration_27.py @@ -1,6 +1,5 @@ """Test configuration example: Best practice - Clean up resources.""" -import tempfile import pytest @@ -8,6 +7,8 @@ @pytest.mark.asyncio async def test_cleanup_resources_best_practice() -> None: """Test resource cleanup best practice.""" + import tempfile + from sqlspec import SQLSpec from sqlspec.adapters.aiosqlite import AiosqliteConfig diff --git a/docs/usage/configuration.rst b/docs/usage/configuration.rst index a0b920c7..fdd11572 100644 --- a/docs/usage/configuration.rst +++ b/docs/usage/configuration.rst @@ -251,9 +251,17 @@ Basic Migration Config **Migration CLI** -.. literalinclude:: /examples/usage/test_configuration_22.txt - :language: text - :caption: `migration CLI` +.. code-block:: bash + + # Create migration + sqlspec --config myapp.config create-migration -m "Add users table" + + # Apply migrations + sqlspec --config myapp.config upgrade + + # Rollback + sqlspec --config myapp.config downgrade -1 + Extension Migration Versioning @@ -281,42 +289,22 @@ Framework integrations can be configured via ``extension_config``. Litestar Plugin Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. code-block:: python - - from sqlspec.adapters.asyncpg import AsyncpgConfig - - config = AsyncpgConfig( - pool_config={"dsn": "postgresql://localhost/db"}, - extension_config={ - "litestar": { - "connection_key": "db_connection", - "session_key": "db_session", - "pool_key": "db_pool", - "commit_mode": "autocommit", - "enable_correlation_middleware": True, - } - } - ) +.. literalinclude:: /examples/usage/test_configuration_23.py + :language: python + :caption: `litestar plugin configuration` + :lines: 10-31 + :dedent: 2 Environment-Based Configuration ------------------------------- Use environment variables for configuration: -.. code-block:: python - - import os - from sqlspec.adapters.asyncpg import AsyncpgConfig - - config = AsyncpgConfig( - pool_config={ - "host": os.getenv("DB_HOST", "localhost"), - "port": int(os.getenv("DB_PORT", "5432")), - "user": os.getenv("DB_USER"), - "password": os.getenv("DB_PASSWORD"), - "database": os.getenv("DB_NAME"), - } - ) +.. literalinclude:: /examples/usage/test_configuration_23.py + :language: python + :caption: `environnment-based configuration` + :lines: 49-59 + :dedent: 4 Configuration Best Practices ----------------------------- @@ -325,59 +313,52 @@ Configuration Best Practices Always use pooling in production: -.. code-block:: python - - config = AsyncpgConfig( - pool_config={ - "dsn": "postgresql://localhost/db", - "min_size": 10, - "max_size": 20, - } - ) +.. literalinclude:: /examples/usage/test_configuration_24.py + :language: python + :caption: `connection pooling` + :lines: 11-13 + :dedent: 2 **2. Enable Caching** Enable caching to avoid recompiling SQL statements: -.. code-block:: python - - statement_config = StatementConfig( - dialect="postgres", - enable_caching=True - ) +.. literalinclude:: /examples/usage/test_configuration_25.py + :language: python + :caption: `enable caching` + :lines: 6-8 + :dedent: 2 **3. Tune Pool Sizes** Size pools based on your workload: -.. code-block:: python - - # CPU-bound workload - pool_config = {"min_size": 5, "max_size": 10} - - # I/O-bound workload - pool_config = {"min_size": 20, "max_size": 50} +.. literalinclude:: /examples/usage/test_configuration_26.py + :language: python + :caption: `tune pool sizes` + :lines: 6-14 + :dedent: 2 **4. Disable Validation in Production** For trusted, performance-critical queries: -.. code-block:: python +.. literalinclude:: /examples/usage/test_configuration_26.py + :language: python + :caption: `no validation` + :lines: 19-25 + :dedent: 2 - statement_config = StatementConfig( - dialect="postgres", - enable_validation=False, # Skip security checks - ) **5. Clean Up Resources** Always close pools on shutdown: -.. code-block:: python - - # Synchronous cleanup (automatic with atexit) - # Asynchronous cleanup (manual) - await spec.close_all_pools() +.. literalinclude:: /examples/usage/test_configuration_27.py + :language: python + :caption: `cleanup resources` + :lines: 10-27 + :dedent: 2 Next Steps ---------- From a6256a6e319e8617193ba471f1e8900dd922e475 Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 5 Nov 2025 07:35:16 +0100 Subject: [PATCH 08/21] fix tests, one not passing when using provide_session by bind_key --- docs/examples/usage/test_configuration_1.py | 2 +- docs/examples/usage/test_configuration_19.py | 6 +++--- docs/examples/usage/test_configuration_20.py | 8 ++++---- docs/examples/usage/test_configuration_26.py | 8 +++++--- docs/examples/usage/test_configuration_27.py | 1 - docs/examples/usage/test_configuration_3.py | 2 +- docs/examples/usage/test_configuration_7.py | 2 +- docs/examples/usage/test_configuration_9.py | 2 +- 8 files changed, 16 insertions(+), 15 deletions(-) diff --git a/docs/examples/usage/test_configuration_1.py b/docs/examples/usage/test_configuration_1.py index df9f5428..1e64acfd 100644 --- a/docs/examples/usage/test_configuration_1.py +++ b/docs/examples/usage/test_configuration_1.py @@ -11,4 +11,4 @@ def test_sqlite_memory_db() -> None: # Use the database with spec.provide_session(db) as session: result = session.execute("SELECT 1") - assert result.fetchone()[0] == 1 + assert result[0] == {"1": 1} diff --git a/docs/examples/usage/test_configuration_19.py b/docs/examples/usage/test_configuration_19.py index bc254e66..dc94d0a9 100644 --- a/docs/examples/usage/test_configuration_19.py +++ b/docs/examples/usage/test_configuration_19.py @@ -14,11 +14,11 @@ def test_binding_multiple_configs() -> None: # Add multiple configurations sqlite_db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) - postgres_db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."})) + spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."})) # Use specific configuration with spec.provide_session(sqlite_db) as session: session.execute("SELECT 1") - assert sqlite_db.pool_config["database"] == tmp.name - assert postgres_db.pool_config["dsn"] == "postgresql://..." + assert spec.configs[SqliteConfig].pool_config["database"] == tmp.name + assert spec.configs[AsyncpgConfig].pool_config["dsn"] == "postgresql://..." diff --git a/docs/examples/usage/test_configuration_20.py b/docs/examples/usage/test_configuration_20.py index 02d4fa6a..47763e08 100644 --- a/docs/examples/usage/test_configuration_20.py +++ b/docs/examples/usage/test_configuration_20.py @@ -13,12 +13,12 @@ def test_named_bindings() -> None: spec = SQLSpec() # Add with bind keys - cache_db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name}), bind_key="cache_db") - main_db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."}), bind_key="main_db") + spec.add_config(SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db")) + spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."}, bind_key="main_db")) # Access by bind key with spec.provide_session("cache_db") as session: session.execute("SELECT 1") - assert cache_db.pool_config["database"] == tmp.name - assert main_db.pool_config["dsn"] == "postgresql://..." + assert spec.configs[SqliteConfig].pool_config["database"] == tmp.name + assert spec.configs[AsyncpgConfig].pool_config["dsn"] == "postgresql://..." diff --git a/docs/examples/usage/test_configuration_26.py b/docs/examples/usage/test_configuration_26.py index af6fbe4a..780f724e 100644 --- a/docs/examples/usage/test_configuration_26.py +++ b/docs/examples/usage/test_configuration_26.py @@ -1,5 +1,6 @@ """Test configuration example: Best practice - Tune pool sizes.""" + def test_tune_pool_sizes_best_practice() -> None: """Test pool sizing best practices for different workloads.""" @@ -13,6 +14,7 @@ def test_tune_pool_sizes_best_practice() -> None: assert io_bound_pool_config["min_size"] == 20 assert io_bound_pool_config["max_size"] == 50 + def test_disable_security_checks_best_practice() -> None: """Test disabling security checks when necessary.""" @@ -20,7 +22,7 @@ def test_disable_security_checks_best_practice() -> None: # Example: Disabling security checks for trusted internal queries statement_config = StatementConfig( - dialect="postgres", - enable_validation=False, # Skip security checks - ) + dialect="postgres", + enable_validation=False, # Skip security checks + ) assert statement_config.enable_validation is False diff --git a/docs/examples/usage/test_configuration_27.py b/docs/examples/usage/test_configuration_27.py index a0190eff..ad871368 100644 --- a/docs/examples/usage/test_configuration_27.py +++ b/docs/examples/usage/test_configuration_27.py @@ -1,6 +1,5 @@ """Test configuration example: Best practice - Clean up resources.""" - import pytest diff --git a/docs/examples/usage/test_configuration_3.py b/docs/examples/usage/test_configuration_3.py index 646322f3..e53b38e3 100644 --- a/docs/examples/usage/test_configuration_3.py +++ b/docs/examples/usage/test_configuration_3.py @@ -3,7 +3,7 @@ def test_memory_databases() -> None: # In-memory database (isolated per connection) config = SqliteConfig(pool_config={"database": ":memory:"}) - assert config.pool_config["database"] == ":memory:" + assert ":memory_" in config.pool_config["database"] # Shared memory database shared_config = SqliteConfig(pool_config={"database": "file:memdb1?mode=memory&cache=shared", "uri": True}) diff --git a/docs/examples/usage/test_configuration_7.py b/docs/examples/usage/test_configuration_7.py index f8c2df3b..3152d6ed 100644 --- a/docs/examples/usage/test_configuration_7.py +++ b/docs/examples/usage/test_configuration_7.py @@ -2,7 +2,7 @@ def test_duckdb_config_setup() -> None: from sqlspec.adapters.duckdb import DuckDBConfig in_memory_config = DuckDBConfig() - assert in_memory_config.pool_config == {} + assert in_memory_config.pool_config.get("database") == ":memory:shared_db" persistent_config = DuckDBConfig(pool_config={"database": "analytics.duckdb", "read_only": False}) assert persistent_config.pool_config["read_only"] is False diff --git a/docs/examples/usage/test_configuration_9.py b/docs/examples/usage/test_configuration_9.py index da08c23c..2d365505 100644 --- a/docs/examples/usage/test_configuration_9.py +++ b/docs/examples/usage/test_configuration_9.py @@ -4,4 +4,4 @@ def test_pool_lifecycle() -> None: spec = SQLSpec() db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db"})) - assert db.pool_config["dsn"] == "postgresql://localhost/db" + assert spec.configs[AsyncpgConfig].pool_config["dsn"] == "postgresql://localhost/db" From b85c152839899558625fd2ef4846d7fac500cdc5 Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 5 Nov 2025 07:47:10 +0100 Subject: [PATCH 09/21] linter issues --- docs/examples/usage/test_configuration_10.py | 7 +++++-- docs/examples/usage/test_configuration_15.py | 6 ++++-- docs/examples/usage/test_configuration_23.py | 5 ++++- docs/examples/usage/test_configuration_24.py | 11 ++++++++--- docs/examples/usage/test_configuration_26.py | 17 +++++++++++------ docs/examples/usage/test_configuration_6.py | 7 +++++-- docs/examples/usage/test_configuration_8.py | 5 ++++- docs/examples/usage/test_configuration_9.py | 2 +- 8 files changed, 42 insertions(+), 18 deletions(-) diff --git a/docs/examples/usage/test_configuration_10.py b/docs/examples/usage/test_configuration_10.py index 0f7385db..e012888d 100644 --- a/docs/examples/usage/test_configuration_10.py +++ b/docs/examples/usage/test_configuration_10.py @@ -1,6 +1,9 @@ +POOL_INSTANCE = 20 + + def test_manual_pool() -> None: from sqlspec.adapters.asyncpg import AsyncpgConfig - pool = {"dsn": "postgresql://localhost/db", "min_size": 10, "max_size": 20} + pool = {"dsn": "postgresql://localhost/db", "min_size": 10, "max_size": POOL_INSTANCE} db = AsyncpgConfig(pool_instance=pool) - assert db.pool_instance["max_size"] == 20 + assert db.pool_instance["max_size"] == POOL_INSTANCE diff --git a/docs/examples/usage/test_configuration_15.py b/docs/examples/usage/test_configuration_15.py index b61246e8..359a4968 100644 --- a/docs/examples/usage/test_configuration_15.py +++ b/docs/examples/usage/test_configuration_15.py @@ -1,5 +1,7 @@ """Test configuration example: Global cache configuration.""" +SQL_CACHE_SIZE = 1000 + def test_global_cache_config() -> None: """Test global cache configuration.""" @@ -10,7 +12,7 @@ def test_global_cache_config() -> None: sql_cache_enabled=True, # Cache SQL strings fragment_cache_enabled=True, # Cache SQL fragments optimized_cache_enabled=True, # Cache optimized AST - sql_cache_size=1000, # Maximum cached SQL items + sql_cache_size=SQL_CACHE_SIZE, # Maximum cached SQL items ) # Update global cache configuration @@ -18,4 +20,4 @@ def test_global_cache_config() -> None: # Verify config applied assert cache_config.sql_cache_enabled is True - assert cache_config.sql_cache_size == 1000 + assert cache_config.sql_cache_size == SQL_CACHE_SIZE diff --git a/docs/examples/usage/test_configuration_23.py b/docs/examples/usage/test_configuration_23.py index 598a73ed..b4bb9dc1 100644 --- a/docs/examples/usage/test_configuration_23.py +++ b/docs/examples/usage/test_configuration_23.py @@ -32,6 +32,9 @@ def test_extension_config() -> None: } +POSTGRES_PORT = 5433 + + @pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled") def test_environment_based_configuration() -> None: """Test environment-based configuration pattern.""" @@ -59,7 +62,7 @@ def test_environment_based_configuration() -> None: ) assert config.pool_config["host"] == "testhost" - assert config.pool_config["port"] == 5433 + assert config.pool_config["port"] == POSTGRES_PORT assert config.pool_config["user"] == "testuser" assert config.pool_config["password"] == "testpass" assert config.pool_config["database"] == "testdb" diff --git a/docs/examples/usage/test_configuration_24.py b/docs/examples/usage/test_configuration_24.py index e5d9bc0e..1e012638 100644 --- a/docs/examples/usage/test_configuration_24.py +++ b/docs/examples/usage/test_configuration_24.py @@ -2,6 +2,9 @@ import pytest +MIN_POOL_SIZE = 10 +MAX_POOL_SIZE = 20 + @pytest.mark.skipif( not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" @@ -10,8 +13,10 @@ def test_connection_pooling_best_practice() -> None: """Test connection pooling best practice configuration.""" from sqlspec.adapters.asyncpg import AsyncpgConfig - config = AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db", "min_size": 10, "max_size": 20}) + config = AsyncpgConfig( + pool_config={"dsn": "postgresql://localhost/db", "min_size": MIN_POOL_SIZE, "max_size": MAX_POOL_SIZE} + ) - assert config.pool_config["min_size"] == 10 - assert config.pool_config["max_size"] == 20 + assert config.pool_config["min_size"] == MIN_POOL_SIZE + assert config.pool_config["max_size"] == MAX_POOL_SIZE assert config.supports_connection_pooling is True diff --git a/docs/examples/usage/test_configuration_26.py b/docs/examples/usage/test_configuration_26.py index 780f724e..7ffec874 100644 --- a/docs/examples/usage/test_configuration_26.py +++ b/docs/examples/usage/test_configuration_26.py @@ -1,18 +1,23 @@ """Test configuration example: Best practice - Tune pool sizes.""" +MIN_POOL_SIZE_CPU = 5 +MAX_POOL_SIZE_CPU = 10 +MIN_IO_BOUND_POOL_SIZE = 20 +MAX_IO_BOUND_POOL_SIZE = 50 + def test_tune_pool_sizes_best_practice() -> None: """Test pool sizing best practices for different workloads.""" # CPU-bound workload - smaller pool - cpu_bound_pool_config = {"min_size": 5, "max_size": 10} - assert cpu_bound_pool_config["min_size"] == 5 - assert cpu_bound_pool_config["max_size"] == 10 + cpu_bound_pool_config = {"min_size": MIN_POOL_SIZE_CPU, "max_size": MAX_POOL_SIZE_CPU} + assert cpu_bound_pool_config["min_size"] == MIN_POOL_SIZE_CPU + assert cpu_bound_pool_config["max_size"] == MAX_POOL_SIZE_CPU # I/O-bound workload - larger pool - io_bound_pool_config = {"min_size": 20, "max_size": 50} - assert io_bound_pool_config["min_size"] == 20 - assert io_bound_pool_config["max_size"] == 50 + io_bound_pool_config = {"min_size": MIN_IO_BOUND_POOL_SIZE, "max_size": MAX_IO_BOUND_POOL_SIZE} + assert io_bound_pool_config["min_size"] == MIN_IO_BOUND_POOL_SIZE + assert io_bound_pool_config["max_size"] == MAX_IO_BOUND_POOL_SIZE def test_disable_security_checks_best_practice() -> None: diff --git a/docs/examples/usage/test_configuration_6.py b/docs/examples/usage/test_configuration_6.py index 8bade4d6..9355cb4c 100644 --- a/docs/examples/usage/test_configuration_6.py +++ b/docs/examples/usage/test_configuration_6.py @@ -1,10 +1,13 @@ +MYSQL_PORT = 3306 + + def test_asyncmy_config_setup() -> None: from sqlspec.adapters.asyncmy import AsyncmyConfig config = AsyncmyConfig( pool_config={ "host": "localhost", - "port": 3306, + "port": MYSQL_PORT, "user": "myuser", "password": "mypassword", "database": "mydb", @@ -14,4 +17,4 @@ def test_asyncmy_config_setup() -> None: "pool_recycle": 3600, } ) - assert config.pool_config["port"] == 3306 + assert config.pool_config["port"] == MYSQL_PORT diff --git a/docs/examples/usage/test_configuration_8.py b/docs/examples/usage/test_configuration_8.py index 6dff6ad5..6cd0a16d 100644 --- a/docs/examples/usage/test_configuration_8.py +++ b/docs/examples/usage/test_configuration_8.py @@ -1,3 +1,6 @@ +MIN_POOL_SIZE = 10 + + def test_asyncpg_pool_setup() -> None: from sqlspec.adapters.asyncpg import AsyncpgConfig @@ -10,4 +13,4 @@ def test_asyncpg_pool_setup() -> None: "max_inactive_connection_lifetime": 300.0, } ) - assert config.pool_config["min_size"] == 10 + assert config.pool_config["min_size"] == MIN_POOL_SIZE diff --git a/docs/examples/usage/test_configuration_9.py b/docs/examples/usage/test_configuration_9.py index 2d365505..6133ab2b 100644 --- a/docs/examples/usage/test_configuration_9.py +++ b/docs/examples/usage/test_configuration_9.py @@ -3,5 +3,5 @@ def test_pool_lifecycle() -> None: from sqlspec.adapters.asyncpg import AsyncpgConfig spec = SQLSpec() - db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db"})) + spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db"})) assert spec.configs[AsyncpgConfig].pool_config["dsn"] == "postgresql://localhost/db" From 642816ac7d5d1e7703574076b234017729e3446e Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 5 Nov 2025 09:37:08 +0100 Subject: [PATCH 10/21] 12 --- docs/usage/configuration.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage/configuration.rst b/docs/usage/configuration.rst index fdd11572..933d1f7b 100644 --- a/docs/usage/configuration.rst +++ b/docs/usage/configuration.rst @@ -132,7 +132,7 @@ Basic Statement Config .. literalinclude:: /examples/usage/test_configuration_12.py :language: python :caption: `basic statement config` - :lines: 2-16 + :lines: 2-14 :dedent: 2 Parameter Style Configuration From 5dd81faa6879e01a7c6de9ef0a3a6ebbf89b24ca Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 5 Nov 2025 09:45:53 +0100 Subject: [PATCH 11/21] one test per block --- docs/examples/usage/test_configuration_23.py | 41 ---------------- docs/examples/usage/test_configuration_24.py | 49 ++++++++++++++------ docs/examples/usage/test_configuration_25.py | 25 +++++++--- docs/examples/usage/test_configuration_26.py | 36 +++----------- docs/examples/usage/test_configuration_27.py | 36 ++++++-------- docs/examples/usage/test_configuration_28.py | 13 ++++++ docs/examples/usage/test_configuration_29.py | 26 +++++++++++ docs/usage/configuration.rst | 20 ++++---- 8 files changed, 123 insertions(+), 123 deletions(-) create mode 100644 docs/examples/usage/test_configuration_28.py create mode 100644 docs/examples/usage/test_configuration_29.py diff --git a/docs/examples/usage/test_configuration_23.py b/docs/examples/usage/test_configuration_23.py index b4bb9dc1..0333748c 100644 --- a/docs/examples/usage/test_configuration_23.py +++ b/docs/examples/usage/test_configuration_23.py @@ -1,10 +1,5 @@ """Test configuration example: Environment-based configuration.""" -import os -from unittest.mock import patch - -import pytest - def test_extension_config() -> None: from sqlspec.adapters.asyncpg import AsyncpgConfig @@ -30,39 +25,3 @@ def test_extension_config() -> None: "enable_correlation_middleware": True, } } - - -POSTGRES_PORT = 5433 - - -@pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled") -def test_environment_based_configuration() -> None: - """Test environment-based configuration pattern.""" - - # Mock environment variables - env_vars = { - "DB_HOST": "testhost", - "DB_PORT": "5433", - "DB_USER": "testuser", - "DB_PASSWORD": "testpass", - "DB_NAME": "testdb", - } - - with patch.dict(os.environ, env_vars, clear=False): - from sqlspec.adapters.asyncpg import AsyncpgConfig - - config = AsyncpgConfig( - pool_config={ - "host": os.getenv("DB_HOST", "localhost"), - "port": int(os.getenv("DB_PORT", "5432")), - "user": os.getenv("DB_USER"), - "password": os.getenv("DB_PASSWORD"), - "database": os.getenv("DB_NAME"), - } - ) - - assert config.pool_config["host"] == "testhost" - assert config.pool_config["port"] == POSTGRES_PORT - assert config.pool_config["user"] == "testuser" - assert config.pool_config["password"] == "testpass" - assert config.pool_config["database"] == "testdb" diff --git a/docs/examples/usage/test_configuration_24.py b/docs/examples/usage/test_configuration_24.py index 1e012638..f38b551e 100644 --- a/docs/examples/usage/test_configuration_24.py +++ b/docs/examples/usage/test_configuration_24.py @@ -1,22 +1,41 @@ -"""Test configuration example: Best practice - Use connection pooling.""" +"""Test configuration example: Environment-based configuration.""" + +import os +from unittest.mock import patch import pytest -MIN_POOL_SIZE = 10 -MAX_POOL_SIZE = 20 +POSTGRES_PORT = 5433 + + +@pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled") +def test_environment_based_configuration() -> None: + """Test environment-based configuration pattern.""" + # Mock environment variables + env_vars = { + "DB_HOST": "testhost", + "DB_PORT": "5433", + "DB_USER": "testuser", + "DB_PASSWORD": "testpass", + "DB_NAME": "testdb", + } -@pytest.mark.skipif( - not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" -) -def test_connection_pooling_best_practice() -> None: - """Test connection pooling best practice configuration.""" - from sqlspec.adapters.asyncpg import AsyncpgConfig + with patch.dict(os.environ, env_vars, clear=False): + from sqlspec.adapters.asyncpg import AsyncpgConfig - config = AsyncpgConfig( - pool_config={"dsn": "postgresql://localhost/db", "min_size": MIN_POOL_SIZE, "max_size": MAX_POOL_SIZE} - ) + config = AsyncpgConfig( + pool_config={ + "host": os.getenv("DB_HOST", "localhost"), + "port": int(os.getenv("DB_PORT", "5432")), + "user": os.getenv("DB_USER"), + "password": os.getenv("DB_PASSWORD"), + "database": os.getenv("DB_NAME"), + } + ) - assert config.pool_config["min_size"] == MIN_POOL_SIZE - assert config.pool_config["max_size"] == MAX_POOL_SIZE - assert config.supports_connection_pooling is True + assert config.pool_config["host"] == "testhost" + assert config.pool_config["port"] == POSTGRES_PORT + assert config.pool_config["user"] == "testuser" + assert config.pool_config["password"] == "testpass" + assert config.pool_config["database"] == "testdb" diff --git a/docs/examples/usage/test_configuration_25.py b/docs/examples/usage/test_configuration_25.py index bab5d292..1e012638 100644 --- a/docs/examples/usage/test_configuration_25.py +++ b/docs/examples/usage/test_configuration_25.py @@ -1,11 +1,22 @@ -"""Test configuration example: Best practice - Enable caching.""" +"""Test configuration example: Best practice - Use connection pooling.""" +import pytest -def test_enable_caching_best_practice() -> None: - """Test caching best practice configuration.""" - from sqlspec.core.statement import StatementConfig +MIN_POOL_SIZE = 10 +MAX_POOL_SIZE = 20 - statement_config = StatementConfig(dialect="postgres", enable_caching=True) - assert statement_config.enable_caching is True - assert statement_config.dialect == "postgres" +@pytest.mark.skipif( + not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" +) +def test_connection_pooling_best_practice() -> None: + """Test connection pooling best practice configuration.""" + from sqlspec.adapters.asyncpg import AsyncpgConfig + + config = AsyncpgConfig( + pool_config={"dsn": "postgresql://localhost/db", "min_size": MIN_POOL_SIZE, "max_size": MAX_POOL_SIZE} + ) + + assert config.pool_config["min_size"] == MIN_POOL_SIZE + assert config.pool_config["max_size"] == MAX_POOL_SIZE + assert config.supports_connection_pooling is True diff --git a/docs/examples/usage/test_configuration_26.py b/docs/examples/usage/test_configuration_26.py index 7ffec874..bab5d292 100644 --- a/docs/examples/usage/test_configuration_26.py +++ b/docs/examples/usage/test_configuration_26.py @@ -1,33 +1,11 @@ -"""Test configuration example: Best practice - Tune pool sizes.""" +"""Test configuration example: Best practice - Enable caching.""" -MIN_POOL_SIZE_CPU = 5 -MAX_POOL_SIZE_CPU = 10 -MIN_IO_BOUND_POOL_SIZE = 20 -MAX_IO_BOUND_POOL_SIZE = 50 - - -def test_tune_pool_sizes_best_practice() -> None: - """Test pool sizing best practices for different workloads.""" - - # CPU-bound workload - smaller pool - cpu_bound_pool_config = {"min_size": MIN_POOL_SIZE_CPU, "max_size": MAX_POOL_SIZE_CPU} - assert cpu_bound_pool_config["min_size"] == MIN_POOL_SIZE_CPU - assert cpu_bound_pool_config["max_size"] == MAX_POOL_SIZE_CPU - - # I/O-bound workload - larger pool - io_bound_pool_config = {"min_size": MIN_IO_BOUND_POOL_SIZE, "max_size": MAX_IO_BOUND_POOL_SIZE} - assert io_bound_pool_config["min_size"] == MIN_IO_BOUND_POOL_SIZE - assert io_bound_pool_config["max_size"] == MAX_IO_BOUND_POOL_SIZE - - -def test_disable_security_checks_best_practice() -> None: - """Test disabling security checks when necessary.""" +def test_enable_caching_best_practice() -> None: + """Test caching best practice configuration.""" from sqlspec.core.statement import StatementConfig - # Example: Disabling security checks for trusted internal queries - statement_config = StatementConfig( - dialect="postgres", - enable_validation=False, # Skip security checks - ) - assert statement_config.enable_validation is False + statement_config = StatementConfig(dialect="postgres", enable_caching=True) + + assert statement_config.enable_caching is True + assert statement_config.dialect == "postgres" diff --git a/docs/examples/usage/test_configuration_27.py b/docs/examples/usage/test_configuration_27.py index ad871368..921c0cad 100644 --- a/docs/examples/usage/test_configuration_27.py +++ b/docs/examples/usage/test_configuration_27.py @@ -1,26 +1,20 @@ -"""Test configuration example: Best practice - Clean up resources.""" +"""Test configuration example: Best practice - Tune pool sizes.""" -import pytest +MIN_POOL_SIZE_CPU = 5 +MAX_POOL_SIZE_CPU = 10 +MIN_IO_BOUND_POOL_SIZE = 20 +MAX_IO_BOUND_POOL_SIZE = 50 -@pytest.mark.asyncio -async def test_cleanup_resources_best_practice() -> None: - """Test resource cleanup best practice.""" - import tempfile +def test_tune_pool_sizes_best_practice() -> None: + """Test pool sizing best practices for different workloads.""" - from sqlspec import SQLSpec - from sqlspec.adapters.aiosqlite import AiosqliteConfig + # CPU-bound workload - smaller pool + cpu_bound_pool_config = {"min_size": MIN_POOL_SIZE_CPU, "max_size": MAX_POOL_SIZE_CPU} + assert cpu_bound_pool_config["min_size"] == MIN_POOL_SIZE_CPU + assert cpu_bound_pool_config["max_size"] == MAX_POOL_SIZE_CPU - with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: - spec = SQLSpec() - db = spec.add_config(AiosqliteConfig(pool_config={"database": tmp.name})) - - # Use the connection - async with spec.provide_session(db) as session: - await session.execute("CREATE TABLE test (id INTEGER)") - - # Clean up resources - important for async adapters - await spec.close_all_pools() - - # Verify pools are closed - assert db.pool_instance is None or not hasattr(db.pool_instance, "_pool") + # I/O-bound workload - larger pool + io_bound_pool_config = {"min_size": MIN_IO_BOUND_POOL_SIZE, "max_size": MAX_IO_BOUND_POOL_SIZE} + assert io_bound_pool_config["min_size"] == MIN_IO_BOUND_POOL_SIZE + assert io_bound_pool_config["max_size"] == MAX_IO_BOUND_POOL_SIZE diff --git a/docs/examples/usage/test_configuration_28.py b/docs/examples/usage/test_configuration_28.py new file mode 100644 index 00000000..dce71f58 --- /dev/null +++ b/docs/examples/usage/test_configuration_28.py @@ -0,0 +1,13 @@ +"""Test configuration example: Best practice - Tune pool sizes.""" + +def test_disable_security_checks_best_practice() -> None: + """Test disabling security checks when necessary.""" + + from sqlspec.core.statement import StatementConfig + + # Example: Disabling security checks for trusted internal queries + statement_config = StatementConfig( + dialect="postgres", + enable_validation=False, # Skip security checks + ) + assert statement_config.enable_validation is False diff --git a/docs/examples/usage/test_configuration_29.py b/docs/examples/usage/test_configuration_29.py new file mode 100644 index 00000000..ad871368 --- /dev/null +++ b/docs/examples/usage/test_configuration_29.py @@ -0,0 +1,26 @@ +"""Test configuration example: Best practice - Clean up resources.""" + +import pytest + + +@pytest.mark.asyncio +async def test_cleanup_resources_best_practice() -> None: + """Test resource cleanup best practice.""" + import tempfile + + from sqlspec import SQLSpec + from sqlspec.adapters.aiosqlite import AiosqliteConfig + + with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: + spec = SQLSpec() + db = spec.add_config(AiosqliteConfig(pool_config={"database": tmp.name})) + + # Use the connection + async with spec.provide_session(db) as session: + await session.execute("CREATE TABLE test (id INTEGER)") + + # Clean up resources - important for async adapters + await spec.close_all_pools() + + # Verify pools are closed + assert db.pool_instance is None or not hasattr(db.pool_instance, "_pool") diff --git a/docs/usage/configuration.rst b/docs/usage/configuration.rst index 933d1f7b..916bc599 100644 --- a/docs/usage/configuration.rst +++ b/docs/usage/configuration.rst @@ -292,7 +292,7 @@ Litestar Plugin Configuration .. literalinclude:: /examples/usage/test_configuration_23.py :language: python :caption: `litestar plugin configuration` - :lines: 10-31 + :lines: 2-26 :dedent: 2 Environment-Based Configuration @@ -300,10 +300,10 @@ Environment-Based Configuration Use environment variables for configuration: -.. literalinclude:: /examples/usage/test_configuration_23.py +.. literalinclude:: /examples/usage/test_configuration_24.py :language: python :caption: `environnment-based configuration` - :lines: 49-59 + :lines: 25-41 :dedent: 4 Configuration Best Practices @@ -313,7 +313,7 @@ Configuration Best Practices Always use pooling in production: -.. literalinclude:: /examples/usage/test_configuration_24.py +.. literalinclude:: /examples/usage/test_configuration_25.py :language: python :caption: `connection pooling` :lines: 11-13 @@ -323,7 +323,7 @@ Always use pooling in production: Enable caching to avoid recompiling SQL statements: -.. literalinclude:: /examples/usage/test_configuration_25.py +.. literalinclude:: /examples/usage/test_configuration_26.py :language: python :caption: `enable caching` :lines: 6-8 @@ -333,20 +333,20 @@ Enable caching to avoid recompiling SQL statements: Size pools based on your workload: -.. literalinclude:: /examples/usage/test_configuration_26.py +.. literalinclude:: /examples/usage/test_configuration_27.py :language: python :caption: `tune pool sizes` - :lines: 6-14 + :lines: 3-20 :dedent: 2 **4. Disable Validation in Production** For trusted, performance-critical queries: -.. literalinclude:: /examples/usage/test_configuration_26.py +.. literalinclude:: /examples/usage/test_configuration_28.py :language: python :caption: `no validation` - :lines: 19-25 + :lines: 6-12 :dedent: 2 @@ -354,7 +354,7 @@ For trusted, performance-critical queries: Always close pools on shutdown: -.. literalinclude:: /examples/usage/test_configuration_27.py +.. literalinclude:: /examples/usage/test_configuration_29.py :language: python :caption: `cleanup resources` :lines: 10-27 From 9c7c68917fb2c1e6095b7cba8c42220e99b14d32 Mon Sep 17 00:00:00 2001 From: euri10 Date: Wed, 5 Nov 2025 09:56:40 +0100 Subject: [PATCH 12/21] wrong psygopg note, replaced by correct config --- docs/examples/usage/test_configuration_28.py | 1 + docs/examples/usage/test_configuration_5.py | 24 ++++++++++++++++---- docs/usage/configuration.rst | 9 ++++---- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/docs/examples/usage/test_configuration_28.py b/docs/examples/usage/test_configuration_28.py index dce71f58..88e27089 100644 --- a/docs/examples/usage/test_configuration_28.py +++ b/docs/examples/usage/test_configuration_28.py @@ -1,5 +1,6 @@ """Test configuration example: Best practice - Tune pool sizes.""" + def test_disable_security_checks_best_practice() -> None: """Test disabling security checks when necessary.""" diff --git a/docs/examples/usage/test_configuration_5.py b/docs/examples/usage/test_configuration_5.py index 3f5ac8b7..a2a14b9f 100644 --- a/docs/examples/usage/test_configuration_5.py +++ b/docs/examples/usage/test_configuration_5.py @@ -1,6 +1,20 @@ -import pytest - - -@pytest.mark.skip(reason="PsycopgConfig does not exist in the codebase.") def test_psycopg_config_setup() -> None: - pass + from sqlspec.adapters.psycopg import PsycopgAsyncConfig + + # Async version + config = PsycopgAsyncConfig( + pool_config={ + "conninfo": "postgresql://user:pass@localhost/db", + # Or keyword arguments: + "host": "localhost", + "port": 5432, + "dbname": "mydb", + "user": "myuser", + "password": "mypassword", + # Pool settings + "min_size": 5, + "max_size": 10, + "timeout": 30.0, + } + ) + assert config.pool_config is not None diff --git a/docs/usage/configuration.rst b/docs/usage/configuration.rst index 916bc599..075014f3 100644 --- a/docs/usage/configuration.rst +++ b/docs/usage/configuration.rst @@ -58,10 +58,11 @@ PostgreSQL Configuration (asyncpg) PostgreSQL Configuration (psycopg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. note:: - - The `PsycopgConfig` class referenced here is for documentation purposes only and may not be present in the codebase. Future releases may include this feature if demand warrants. - +.. literalinclude:: /examples/usage/test_configuration_5.py + :language: python + :caption: `psycopg async configuration` + :lines: 2-19 + :dedent: 2 MySQL Configuration (asyncmy) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ From f6d748f21af5bd0dc7f152beaf853e88409e5ba5 Mon Sep 17 00:00:00 2001 From: euri10 Date: Fri, 7 Nov 2025 07:23:15 +0100 Subject: [PATCH 13/21] Update docs/examples/usage/test_configuration_12.py Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com> --- docs/examples/usage/test_configuration_12.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/usage/test_configuration_12.py b/docs/examples/usage/test_configuration_12.py index a85a9c4b..584dd2c3 100644 --- a/docs/examples/usage/test_configuration_12.py +++ b/docs/examples/usage/test_configuration_12.py @@ -1,6 +1,6 @@ def test_basic_statement_config() -> None: from sqlspec.adapters.asyncpg import AsyncpgConfig - from sqlspec.core.statement import StatementConfig + from sqlspec.core import StatementConfig statement_config = StatementConfig( dialect="postgres", # SQLGlot dialect From fffec01df46d18712700455231031d4cdce2e63e Mon Sep 17 00:00:00 2001 From: euri10 Date: Fri, 7 Nov 2025 07:23:24 +0100 Subject: [PATCH 14/21] Update docs/examples/usage/test_configuration_13.py Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com> --- docs/examples/usage/test_configuration_13.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/examples/usage/test_configuration_13.py b/docs/examples/usage/test_configuration_13.py index f95775d1..1cd9fd4d 100644 --- a/docs/examples/usage/test_configuration_13.py +++ b/docs/examples/usage/test_configuration_13.py @@ -1,6 +1,5 @@ def test_parameter_style_config() -> None: - from sqlspec.core.parameters import ParameterStyle, ParameterStyleConfig - from sqlspec.core.statement import StatementConfig + from sqlspec.core import ParameterStyle, ParameterStyleConfig, StatementConfig param_config = ParameterStyleConfig( default_parameter_style=ParameterStyle.NUMERIC, # $1, $2, ... From 91c6d0eecbfe9efaa2db5eadfb4d63358e94693c Mon Sep 17 00:00:00 2001 From: euri10 Date: Fri, 7 Nov 2025 07:23:41 +0100 Subject: [PATCH 15/21] Update docs/examples/usage/test_configuration_19.py Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com> --- docs/examples/usage/test_configuration_19.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/usage/test_configuration_19.py b/docs/examples/usage/test_configuration_19.py index dc94d0a9..cdd77248 100644 --- a/docs/examples/usage/test_configuration_19.py +++ b/docs/examples/usage/test_configuration_19.py @@ -14,7 +14,7 @@ def test_binding_multiple_configs() -> None: # Add multiple configurations sqlite_db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) - spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."})) + pg_db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."})) # Use specific configuration with spec.provide_session(sqlite_db) as session: From 8d2c24c31eeb253cbbc4843da0d90e39f9453937 Mon Sep 17 00:00:00 2001 From: Cody Fincher Date: Mon, 10 Nov 2025 03:09:16 +0000 Subject: [PATCH 16/21] Refactor example configurations and tests - Deleted outdated test configuration examples for asyncpg and psycopg. - Added new example configurations for SQLite, asyncpg, psycopg, asyncmy, and DuckDB. - Introduced environment-based configuration examples for better flexibility. - Updated documentation references to point to the new example files. - Enhanced test cases to cover various best practices, including connection pooling, caching, and resource cleanup. - Improved clarity and organization of configuration examples for better usability. --- AGENTS.md | 1 - docs/examples/usage/conftest.py | 27 ++++++ docs/examples/usage/test_configuration_19.py | 24 ----- docs/examples/usage/test_configuration_20.py | 24 ----- docs/examples/usage/test_configuration_23.py | 27 ------ docs/examples/usage/test_configuration_4.py | 17 ---- docs/examples/usage/test_configuration_5.py | 20 ---- docs/examples/usage/test_configuration_9.py | 7 -- ...guration_1.py => usage_configuration_1.py} | 6 +- ...ration_10.py => usage_configuration_10.py} | 5 +- ...ration_11.py => usage_configuration_11.py} | 0 ...ration_12.py => usage_configuration_12.py} | 7 +- ...ration_13.py => usage_configuration_13.py} | 2 +- ...ration_14.py => usage_configuration_14.py} | 2 +- ...ration_15.py => usage_configuration_15.py} | 0 ...ration_16.py => usage_configuration_16.py} | 8 +- ...ration_17.py => usage_configuration_17.py} | 6 +- ...ration_18.py => usage_configuration_18.py} | 0 docs/examples/usage/usage_configuration_19.py | 29 ++++++ ...guration_2.py => usage_configuration_2.py} | 0 docs/examples/usage/usage_configuration_20.py | 31 +++++++ ...ration_21.py => usage_configuration_21.py} | 5 +- ...ration_22.py => usage_configuration_22.py} | 5 +- docs/examples/usage/usage_configuration_23.py | 31 +++++++ ...ration_24.py => usage_configuration_24.py} | 3 - ...ration_25.py => usage_configuration_25.py} | 7 +- ...ration_26.py => usage_configuration_26.py} | 2 +- ...ration_27.py => usage_configuration_27.py} | 0 ...ration_28.py => usage_configuration_28.py} | 2 +- ...ration_29.py => usage_configuration_29.py} | 8 +- ...guration_3.py => usage_configuration_3.py} | 0 docs/examples/usage/usage_configuration_30.py | 17 ++++ docs/examples/usage/usage_configuration_4.py | 25 +++++ docs/examples/usage/usage_configuration_5.py | 29 ++++++ ...guration_6.py => usage_configuration_6.py} | 0 ...guration_7.py => usage_configuration_7.py} | 0 ...guration_8.py => usage_configuration_8.py} | 6 +- docs/examples/usage/usage_configuration_9.py | 12 +++ docs/usage/configuration.rst | 91 +++++++++++-------- 39 files changed, 297 insertions(+), 189 deletions(-) create mode 100644 docs/examples/usage/conftest.py delete mode 100644 docs/examples/usage/test_configuration_19.py delete mode 100644 docs/examples/usage/test_configuration_20.py delete mode 100644 docs/examples/usage/test_configuration_23.py delete mode 100644 docs/examples/usage/test_configuration_4.py delete mode 100644 docs/examples/usage/test_configuration_5.py delete mode 100644 docs/examples/usage/test_configuration_9.py rename docs/examples/usage/{test_configuration_1.py => usage_configuration_1.py} (64%) rename docs/examples/usage/{test_configuration_10.py => usage_configuration_10.py} (57%) rename docs/examples/usage/{test_configuration_11.py => usage_configuration_11.py} (100%) rename docs/examples/usage/{test_configuration_12.py => usage_configuration_12.py} (72%) rename docs/examples/usage/{test_configuration_13.py => usage_configuration_13.py} (87%) rename docs/examples/usage/{test_configuration_14.py => usage_configuration_14.py} (90%) rename docs/examples/usage/{test_configuration_15.py => usage_configuration_15.py} (100%) rename docs/examples/usage/{test_configuration_16.py => usage_configuration_16.py} (69%) rename docs/examples/usage/{test_configuration_17.py => usage_configuration_17.py} (81%) rename docs/examples/usage/{test_configuration_18.py => usage_configuration_18.py} (100%) create mode 100644 docs/examples/usage/usage_configuration_19.py rename docs/examples/usage/{test_configuration_2.py => usage_configuration_2.py} (100%) create mode 100644 docs/examples/usage/usage_configuration_20.py rename docs/examples/usage/{test_configuration_21.py => usage_configuration_21.py} (88%) rename docs/examples/usage/{test_configuration_22.py => usage_configuration_22.py} (84%) create mode 100644 docs/examples/usage/usage_configuration_23.py rename docs/examples/usage/{test_configuration_24.py => usage_configuration_24.py} (91%) rename docs/examples/usage/{test_configuration_25.py => usage_configuration_25.py} (75%) rename docs/examples/usage/{test_configuration_26.py => usage_configuration_26.py} (86%) rename docs/examples/usage/{test_configuration_27.py => usage_configuration_27.py} (100%) rename docs/examples/usage/{test_configuration_28.py => usage_configuration_28.py} (88%) rename docs/examples/usage/{test_configuration_29.py => usage_configuration_29.py} (75%) rename docs/examples/usage/{test_configuration_3.py => usage_configuration_3.py} (100%) create mode 100644 docs/examples/usage/usage_configuration_30.py create mode 100644 docs/examples/usage/usage_configuration_4.py create mode 100644 docs/examples/usage/usage_configuration_5.py rename docs/examples/usage/{test_configuration_6.py => usage_configuration_6.py} (100%) rename docs/examples/usage/{test_configuration_7.py => usage_configuration_7.py} (100%) rename docs/examples/usage/{test_configuration_8.py => usage_configuration_8.py} (77%) create mode 100644 docs/examples/usage/usage_configuration_9.py diff --git a/AGENTS.md b/AGENTS.md index 33baea25..1134fa17 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -524,7 +524,6 @@ This summary documents the small documentation and example maintenance performed on the configuration usage guide and can be expanded into a longer changelog entry if desired. - ```python def parse_user_input(content: str, source: str) -> "dict[str, Result]": """Parse user input with two-tier error handling. diff --git a/docs/examples/usage/conftest.py b/docs/examples/usage/conftest.py new file mode 100644 index 00000000..4f247707 --- /dev/null +++ b/docs/examples/usage/conftest.py @@ -0,0 +1,27 @@ +from __future__ import annotations + +from collections.abc import Generator + +import pytest +from pytest_databases.docker.postgres import PostgresService + +pytest_plugins = ["pytest_databases.docker.postgres"] + + +@pytest.fixture(scope="session", autouse=True) +def usage_postgres_env(postgres_service: PostgresService) -> Generator[None, None, None]: + """Expose Postgres connection settings via env vars for docs examples.""" + + patcher = pytest.MonkeyPatch() + dsn = ( + f"postgresql://{postgres_service.user}:{postgres_service.password}" + f"@{postgres_service.host}:{postgres_service.port}/{postgres_service.database}" + ) + patcher.setenv("SQLSPEC_USAGE_PG_DSN", dsn) + patcher.setenv("SQLSPEC_USAGE_PG_HOST", postgres_service.host) + patcher.setenv("SQLSPEC_USAGE_PG_PORT", str(postgres_service.port)) + patcher.setenv("SQLSPEC_USAGE_PG_USER", postgres_service.user) + patcher.setenv("SQLSPEC_USAGE_PG_PASSWORD", postgres_service.password) + patcher.setenv("SQLSPEC_USAGE_PG_DATABASE", postgres_service.database) + yield + patcher.undo() diff --git a/docs/examples/usage/test_configuration_19.py b/docs/examples/usage/test_configuration_19.py deleted file mode 100644 index cdd77248..00000000 --- a/docs/examples/usage/test_configuration_19.py +++ /dev/null @@ -1,24 +0,0 @@ -"""Test configuration example: Binding multiple database configurations.""" - - -def test_binding_multiple_configs() -> None: - """Test binding multiple database configurations.""" - import tempfile - - from sqlspec import SQLSpec - from sqlspec.adapters.asyncpg import AsyncpgConfig - from sqlspec.adapters.sqlite import SqliteConfig - - with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: - spec = SQLSpec() - - # Add multiple configurations - sqlite_db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) - pg_db = spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."})) - - # Use specific configuration - with spec.provide_session(sqlite_db) as session: - session.execute("SELECT 1") - - assert spec.configs[SqliteConfig].pool_config["database"] == tmp.name - assert spec.configs[AsyncpgConfig].pool_config["dsn"] == "postgresql://..." diff --git a/docs/examples/usage/test_configuration_20.py b/docs/examples/usage/test_configuration_20.py deleted file mode 100644 index 47763e08..00000000 --- a/docs/examples/usage/test_configuration_20.py +++ /dev/null @@ -1,24 +0,0 @@ -"""Test configuration example: Named database bindings.""" - - -def test_named_bindings() -> None: - """Test named database bindings.""" - import tempfile - - from sqlspec import SQLSpec - from sqlspec.adapters.asyncpg import AsyncpgConfig - from sqlspec.adapters.sqlite import SqliteConfig - - with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: - spec = SQLSpec() - - # Add with bind keys - spec.add_config(SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db")) - spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://..."}, bind_key="main_db")) - - # Access by bind key - with spec.provide_session("cache_db") as session: - session.execute("SELECT 1") - - assert spec.configs[SqliteConfig].pool_config["database"] == tmp.name - assert spec.configs[AsyncpgConfig].pool_config["dsn"] == "postgresql://..." diff --git a/docs/examples/usage/test_configuration_23.py b/docs/examples/usage/test_configuration_23.py deleted file mode 100644 index 0333748c..00000000 --- a/docs/examples/usage/test_configuration_23.py +++ /dev/null @@ -1,27 +0,0 @@ -"""Test configuration example: Environment-based configuration.""" - - -def test_extension_config() -> None: - from sqlspec.adapters.asyncpg import AsyncpgConfig - - config = AsyncpgConfig( - pool_config={"dsn": "postgresql://localhost/db"}, - extension_config={ - "litestar": { - "connection_key": "db_connection", - "session_key": "db_session", - "pool_key": "db_pool", - "commit_mode": "autocommit", - "enable_correlation_middleware": True, - } - }, - ) - assert config.extension_config == { - "litestar": { - "connection_key": "db_connection", - "session_key": "db_session", - "pool_key": "db_pool", - "commit_mode": "autocommit", - "enable_correlation_middleware": True, - } - } diff --git a/docs/examples/usage/test_configuration_4.py b/docs/examples/usage/test_configuration_4.py deleted file mode 100644 index a521d5d3..00000000 --- a/docs/examples/usage/test_configuration_4.py +++ /dev/null @@ -1,17 +0,0 @@ -def test_asyncpg_config_setup() -> None: - from sqlspec.adapters.asyncpg import AsyncpgConfig - - config = AsyncpgConfig( - pool_config={ - "dsn": "postgresql://user:pass@localhost:5432/dbname", - # Other parameters - "host": "localhost", - "port": 5432, - "user": "myuser", - "password": "mypassword", - "database": "mydb", - "min_size": 10, - "max_size": 20, - } - ) - assert config.pool_config["host"] == "localhost" diff --git a/docs/examples/usage/test_configuration_5.py b/docs/examples/usage/test_configuration_5.py deleted file mode 100644 index a2a14b9f..00000000 --- a/docs/examples/usage/test_configuration_5.py +++ /dev/null @@ -1,20 +0,0 @@ -def test_psycopg_config_setup() -> None: - from sqlspec.adapters.psycopg import PsycopgAsyncConfig - - # Async version - config = PsycopgAsyncConfig( - pool_config={ - "conninfo": "postgresql://user:pass@localhost/db", - # Or keyword arguments: - "host": "localhost", - "port": 5432, - "dbname": "mydb", - "user": "myuser", - "password": "mypassword", - # Pool settings - "min_size": 5, - "max_size": 10, - "timeout": 30.0, - } - ) - assert config.pool_config is not None diff --git a/docs/examples/usage/test_configuration_9.py b/docs/examples/usage/test_configuration_9.py deleted file mode 100644 index 6133ab2b..00000000 --- a/docs/examples/usage/test_configuration_9.py +++ /dev/null @@ -1,7 +0,0 @@ -def test_pool_lifecycle() -> None: - from sqlspec import SQLSpec - from sqlspec.adapters.asyncpg import AsyncpgConfig - - spec = SQLSpec() - spec.add_config(AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db"})) - assert spec.configs[AsyncpgConfig].pool_config["dsn"] == "postgresql://localhost/db" diff --git a/docs/examples/usage/test_configuration_1.py b/docs/examples/usage/usage_configuration_1.py similarity index 64% rename from docs/examples/usage/test_configuration_1.py rename to docs/examples/usage/usage_configuration_1.py index 1e64acfd..dbe871f5 100644 --- a/docs/examples/usage/test_configuration_1.py +++ b/docs/examples/usage/usage_configuration_1.py @@ -3,12 +3,12 @@ def test_sqlite_memory_db() -> None: from sqlspec.adapters.sqlite import SqliteConfig # Create SQLSpec instance - spec = SQLSpec() + db_manager = SQLSpec() # Add database configuration - db = spec.add_config(SqliteConfig(pool_config={"database": ":memory:"})) + db = db_manager.add_config(SqliteConfig(pool_config={"database": ":memory:"})) # Use the database - with spec.provide_session(db) as session: + with db_manager.provide_session(db) as session: result = session.execute("SELECT 1") assert result[0] == {"1": 1} diff --git a/docs/examples/usage/test_configuration_10.py b/docs/examples/usage/usage_configuration_10.py similarity index 57% rename from docs/examples/usage/test_configuration_10.py rename to docs/examples/usage/usage_configuration_10.py index e012888d..0e78544a 100644 --- a/docs/examples/usage/test_configuration_10.py +++ b/docs/examples/usage/usage_configuration_10.py @@ -2,8 +2,11 @@ def test_manual_pool() -> None: + import os + from sqlspec.adapters.asyncpg import AsyncpgConfig - pool = {"dsn": "postgresql://localhost/db", "min_size": 10, "max_size": POOL_INSTANCE} + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") + pool = {"dsn": dsn, "min_size": 10, "max_size": POOL_INSTANCE} db = AsyncpgConfig(pool_instance=pool) assert db.pool_instance["max_size"] == POOL_INSTANCE diff --git a/docs/examples/usage/test_configuration_11.py b/docs/examples/usage/usage_configuration_11.py similarity index 100% rename from docs/examples/usage/test_configuration_11.py rename to docs/examples/usage/usage_configuration_11.py diff --git a/docs/examples/usage/test_configuration_12.py b/docs/examples/usage/usage_configuration_12.py similarity index 72% rename from docs/examples/usage/test_configuration_12.py rename to docs/examples/usage/usage_configuration_12.py index 584dd2c3..42ea8a86 100644 --- a/docs/examples/usage/test_configuration_12.py +++ b/docs/examples/usage/usage_configuration_12.py @@ -1,6 +1,8 @@ def test_basic_statement_config() -> None: + import os + + from sqlspec import StatementConfig from sqlspec.adapters.asyncpg import AsyncpgConfig - from sqlspec.core import StatementConfig statement_config = StatementConfig( dialect="postgres", # SQLGlot dialect @@ -11,6 +13,7 @@ def test_basic_statement_config() -> None: ) # Apply to adapter - config = AsyncpgConfig(pool_config={"dsn": "postgresql://localhost/db"}, statement_config=statement_config) + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") + config = AsyncpgConfig(pool_config={"dsn": dsn}, statement_config=statement_config) assert config.statement_config.dialect == "postgres" assert config.statement_config.enable_parsing is True diff --git a/docs/examples/usage/test_configuration_13.py b/docs/examples/usage/usage_configuration_13.py similarity index 87% rename from docs/examples/usage/test_configuration_13.py rename to docs/examples/usage/usage_configuration_13.py index 1cd9fd4d..4c5e8eb9 100644 --- a/docs/examples/usage/test_configuration_13.py +++ b/docs/examples/usage/usage_configuration_13.py @@ -1,5 +1,5 @@ def test_parameter_style_config() -> None: - from sqlspec.core import ParameterStyle, ParameterStyleConfig, StatementConfig + from sqlspec import ParameterStyle, ParameterStyleConfig, StatementConfig param_config = ParameterStyleConfig( default_parameter_style=ParameterStyle.NUMERIC, # $1, $2, ... diff --git a/docs/examples/usage/test_configuration_14.py b/docs/examples/usage/usage_configuration_14.py similarity index 90% rename from docs/examples/usage/test_configuration_14.py rename to docs/examples/usage/usage_configuration_14.py index 8b30ec37..0bfda1f2 100644 --- a/docs/examples/usage/test_configuration_14.py +++ b/docs/examples/usage/usage_configuration_14.py @@ -1,5 +1,5 @@ def test_parameter_styles() -> None: - from sqlspec.core.parameters import ParameterStyle + from sqlspec import ParameterStyle # Question mark (SQLite, DuckDB) qmark = ParameterStyle.QMARK # WHERE id = ? diff --git a/docs/examples/usage/test_configuration_15.py b/docs/examples/usage/usage_configuration_15.py similarity index 100% rename from docs/examples/usage/test_configuration_15.py rename to docs/examples/usage/usage_configuration_15.py diff --git a/docs/examples/usage/test_configuration_16.py b/docs/examples/usage/usage_configuration_16.py similarity index 69% rename from docs/examples/usage/test_configuration_16.py rename to docs/examples/usage/usage_configuration_16.py index 4245e8e9..f37009eb 100644 --- a/docs/examples/usage/test_configuration_16.py +++ b/docs/examples/usage/usage_configuration_16.py @@ -11,13 +11,13 @@ def test_per_instance_cache_config() -> None: with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: # Configure cache for specific SQLSpec instance - spec = SQLSpec() - spec.update_cache_config(CacheConfig(sql_cache_enabled=True, sql_cache_size=500)) + db_manager = SQLSpec() + db_manager.update_cache_config(CacheConfig(sql_cache_enabled=True, sql_cache_size=500)) # Add database config - db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) + db = db_manager.add_config(SqliteConfig(pool_config={"database": tmp.name})) # Use the configured spec - with spec.provide_session(db) as session: + with db_manager.provide_session(db) as session: result = session.execute("SELECT 1") assert result is not None diff --git a/docs/examples/usage/test_configuration_17.py b/docs/examples/usage/usage_configuration_17.py similarity index 81% rename from docs/examples/usage/test_configuration_17.py rename to docs/examples/usage/usage_configuration_17.py index 4a3ac587..c3a988c4 100644 --- a/docs/examples/usage/test_configuration_17.py +++ b/docs/examples/usage/usage_configuration_17.py @@ -10,11 +10,11 @@ def test_cache_statistics() -> None: from sqlspec.core.cache import get_cache_statistics, log_cache_stats with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: - spec = SQLSpec() - db = spec.add_config(SqliteConfig(pool_config={"database": tmp.name})) + db_manager = SQLSpec() + db = db_manager.add_config(SqliteConfig(pool_config={"database": tmp.name})) # Execute some queries to generate cache activity - with spec.provide_session(db) as session: + with db_manager.provide_session(db) as session: session.execute("SELECT 1") session.execute("SELECT 1") # Should hit cache diff --git a/docs/examples/usage/test_configuration_18.py b/docs/examples/usage/usage_configuration_18.py similarity index 100% rename from docs/examples/usage/test_configuration_18.py rename to docs/examples/usage/usage_configuration_18.py diff --git a/docs/examples/usage/usage_configuration_19.py b/docs/examples/usage/usage_configuration_19.py new file mode 100644 index 00000000..6020d20c --- /dev/null +++ b/docs/examples/usage/usage_configuration_19.py @@ -0,0 +1,29 @@ +"""Test configuration example: Binding multiple database configurations.""" + + +def test_binding_multiple_configs() -> None: + """Test binding multiple database configurations.""" + import os + import tempfile + + from sqlspec import SQLSpec + from sqlspec.adapters.asyncpg import AsyncpgConfig + from sqlspec.adapters.sqlite import SqliteConfig + + with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: + db_manager = SQLSpec() + + # Add multiple configurations + sqlite_key = db_manager.add_config(SqliteConfig(pool_config={"database": tmp.name})) + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") + asyncpg_key = db_manager.add_config(AsyncpgConfig(pool_config={"dsn": dsn})) + + # Use specific configuration + with db_manager.provide_session(sqlite_key) as session: + session.execute("SELECT 1") + + sqlite_config = db_manager.get_config(sqlite_key) + pg_config = db_manager.get_config(asyncpg_key) + + assert sqlite_config.pool_config["database"] == tmp.name + assert pg_config.pool_config["dsn"] == os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") diff --git a/docs/examples/usage/test_configuration_2.py b/docs/examples/usage/usage_configuration_2.py similarity index 100% rename from docs/examples/usage/test_configuration_2.py rename to docs/examples/usage/usage_configuration_2.py diff --git a/docs/examples/usage/usage_configuration_20.py b/docs/examples/usage/usage_configuration_20.py new file mode 100644 index 00000000..06a26634 --- /dev/null +++ b/docs/examples/usage/usage_configuration_20.py @@ -0,0 +1,31 @@ +"""Test configuration example: Named database bindings.""" + + +def test_named_bindings() -> None: + """Test named database bindings.""" + import os + import tempfile + + from sqlspec import SQLSpec + from sqlspec.adapters.asyncpg import AsyncpgConfig + from sqlspec.adapters.sqlite import SqliteConfig + + with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: + db_manager = SQLSpec() + + # Add with bind keys + cache_key = db_manager.add_config( + SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db") + ) + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") + main_key = db_manager.add_config(AsyncpgConfig(pool_config={"dsn": dsn}, bind_key="main_db")) + + # Access by bind key + with db_manager.provide_session(cache_key) as session: + session.execute("SELECT 1") + + cache_config = db_manager.get_config(cache_key) + main_config = db_manager.get_config(main_key) + + assert cache_config.bind_key == "cache_db" + assert main_config.bind_key == "main_db" diff --git a/docs/examples/usage/test_configuration_21.py b/docs/examples/usage/usage_configuration_21.py similarity index 88% rename from docs/examples/usage/test_configuration_21.py rename to docs/examples/usage/usage_configuration_21.py index 7c53e1a8..183a9b5f 100644 --- a/docs/examples/usage/test_configuration_21.py +++ b/docs/examples/usage/usage_configuration_21.py @@ -8,10 +8,13 @@ ) def test_basic_migration_config() -> None: """Test basic migration configuration.""" + import os + from sqlspec.adapters.asyncpg import AsyncpgConfig + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") config = AsyncpgConfig( - pool_config={"dsn": "postgresql://localhost/db"}, + pool_config={"dsn": dsn}, extension_config={ "litestar": {"session_table": "custom_sessions"} # Extension settings }, diff --git a/docs/examples/usage/test_configuration_22.py b/docs/examples/usage/usage_configuration_22.py similarity index 84% rename from docs/examples/usage/test_configuration_22.py rename to docs/examples/usage/usage_configuration_22.py index 93ef2185..d6986916 100644 --- a/docs/examples/usage/test_configuration_22.py +++ b/docs/examples/usage/usage_configuration_22.py @@ -1,8 +1,11 @@ def test_basic_migration_config() -> None: + import os + from sqlspec.adapters.asyncpg import AsyncpgConfig + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") config = AsyncpgConfig( - pool_config={"dsn": "postgresql://localhost/db"}, + pool_config={"dsn": dsn}, extension_config={ "litestar": {"session_table": "custom_sessions"} # Extension settings }, diff --git a/docs/examples/usage/usage_configuration_23.py b/docs/examples/usage/usage_configuration_23.py new file mode 100644 index 00000000..a04f57c3 --- /dev/null +++ b/docs/examples/usage/usage_configuration_23.py @@ -0,0 +1,31 @@ +"""Test configuration example: Environment-based configuration.""" + + +def test_extension_config() -> None: + import os + + from sqlspec.adapters.asyncpg import AsyncpgConfig + + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") + config = AsyncpgConfig( + pool_config={"dsn": dsn}, + extension_config={ + "litestar": { + "connection_key": "db_connection", + "session_key": "db_session", + "pool_key": "db_pool", + "commit_mode": "autocommit_include_redirect", + "extra_commit_statuses": {201}, + "extra_rollback_statuses": {422}, + "enable_correlation_middleware": True, + "correlation_header": "x-request-id", + "correlation_headers": ["traceparent", "x-correlation-id"], + "auto_trace_headers": False, + "disable_di": False, + } + }, + ) + + assert config.extension_config["litestar"]["commit_mode"] == "autocommit_include_redirect" + assert config.extension_config["litestar"]["extra_commit_statuses"] == {201} + assert config.extension_config["litestar"]["correlation_header"] == "x-request-id" diff --git a/docs/examples/usage/test_configuration_24.py b/docs/examples/usage/usage_configuration_24.py similarity index 91% rename from docs/examples/usage/test_configuration_24.py rename to docs/examples/usage/usage_configuration_24.py index f38b551e..bcad9ddf 100644 --- a/docs/examples/usage/test_configuration_24.py +++ b/docs/examples/usage/usage_configuration_24.py @@ -3,12 +3,9 @@ import os from unittest.mock import patch -import pytest - POSTGRES_PORT = 5433 -@pytest.mark.skipif(os.getenv("TEST_ASYNCPG", "0") != "1", reason="AsyncPG integration tests disabled") def test_environment_based_configuration() -> None: """Test environment-based configuration pattern.""" diff --git a/docs/examples/usage/test_configuration_25.py b/docs/examples/usage/usage_configuration_25.py similarity index 75% rename from docs/examples/usage/test_configuration_25.py rename to docs/examples/usage/usage_configuration_25.py index 1e012638..c4e2110a 100644 --- a/docs/examples/usage/test_configuration_25.py +++ b/docs/examples/usage/usage_configuration_25.py @@ -11,11 +11,12 @@ ) def test_connection_pooling_best_practice() -> None: """Test connection pooling best practice configuration.""" + import os + from sqlspec.adapters.asyncpg import AsyncpgConfig - config = AsyncpgConfig( - pool_config={"dsn": "postgresql://localhost/db", "min_size": MIN_POOL_SIZE, "max_size": MAX_POOL_SIZE} - ) + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") + config = AsyncpgConfig(pool_config={"dsn": dsn, "min_size": MIN_POOL_SIZE, "max_size": MAX_POOL_SIZE}) assert config.pool_config["min_size"] == MIN_POOL_SIZE assert config.pool_config["max_size"] == MAX_POOL_SIZE diff --git a/docs/examples/usage/test_configuration_26.py b/docs/examples/usage/usage_configuration_26.py similarity index 86% rename from docs/examples/usage/test_configuration_26.py rename to docs/examples/usage/usage_configuration_26.py index bab5d292..ad19e70c 100644 --- a/docs/examples/usage/test_configuration_26.py +++ b/docs/examples/usage/usage_configuration_26.py @@ -3,7 +3,7 @@ def test_enable_caching_best_practice() -> None: """Test caching best practice configuration.""" - from sqlspec.core.statement import StatementConfig + from sqlspec import StatementConfig statement_config = StatementConfig(dialect="postgres", enable_caching=True) diff --git a/docs/examples/usage/test_configuration_27.py b/docs/examples/usage/usage_configuration_27.py similarity index 100% rename from docs/examples/usage/test_configuration_27.py rename to docs/examples/usage/usage_configuration_27.py diff --git a/docs/examples/usage/test_configuration_28.py b/docs/examples/usage/usage_configuration_28.py similarity index 88% rename from docs/examples/usage/test_configuration_28.py rename to docs/examples/usage/usage_configuration_28.py index 88e27089..cc0ece0a 100644 --- a/docs/examples/usage/test_configuration_28.py +++ b/docs/examples/usage/usage_configuration_28.py @@ -4,7 +4,7 @@ def test_disable_security_checks_best_practice() -> None: """Test disabling security checks when necessary.""" - from sqlspec.core.statement import StatementConfig + from sqlspec import StatementConfig # Example: Disabling security checks for trusted internal queries statement_config = StatementConfig( diff --git a/docs/examples/usage/test_configuration_29.py b/docs/examples/usage/usage_configuration_29.py similarity index 75% rename from docs/examples/usage/test_configuration_29.py rename to docs/examples/usage/usage_configuration_29.py index ad871368..120c81a7 100644 --- a/docs/examples/usage/test_configuration_29.py +++ b/docs/examples/usage/usage_configuration_29.py @@ -12,15 +12,15 @@ async def test_cleanup_resources_best_practice() -> None: from sqlspec.adapters.aiosqlite import AiosqliteConfig with tempfile.NamedTemporaryFile(suffix=".db", delete=True) as tmp: - spec = SQLSpec() - db = spec.add_config(AiosqliteConfig(pool_config={"database": tmp.name})) + db_manager = SQLSpec() + db = db_manager.add_config(AiosqliteConfig(pool_config={"database": tmp.name})) # Use the connection - async with spec.provide_session(db) as session: + async with db_manager.provide_session(db) as session: await session.execute("CREATE TABLE test (id INTEGER)") # Clean up resources - important for async adapters - await spec.close_all_pools() + await db_manager.close_all_pools() # Verify pools are closed assert db.pool_instance is None or not hasattr(db.pool_instance, "_pool") diff --git a/docs/examples/usage/test_configuration_3.py b/docs/examples/usage/usage_configuration_3.py similarity index 100% rename from docs/examples/usage/test_configuration_3.py rename to docs/examples/usage/usage_configuration_3.py diff --git a/docs/examples/usage/usage_configuration_30.py b/docs/examples/usage/usage_configuration_30.py new file mode 100644 index 00000000..9b653400 --- /dev/null +++ b/docs/examples/usage/usage_configuration_30.py @@ -0,0 +1,17 @@ +"""Telemetry snapshot example.""" + + +def test_telemetry_snapshot() -> None: + """Demonstrate SQLSpec.telemetry_snapshot().""" + from sqlspec import SQLSpec + from sqlspec.adapters.sqlite import SqliteConfig + + db_manager = SQLSpec() + db = db_manager.add_config(SqliteConfig(pool_config={"database": ":memory:"})) + + with db_manager.provide_session(db) as session: + session.execute("SELECT 1") + + snapshot = db_manager.telemetry_snapshot() + assert "SqliteConfig.lifecycle.query_start" in snapshot + _ = snapshot.get("storage_bridge.bytes_written", 0) diff --git a/docs/examples/usage/usage_configuration_4.py b/docs/examples/usage/usage_configuration_4.py new file mode 100644 index 00000000..a8de0ded --- /dev/null +++ b/docs/examples/usage/usage_configuration_4.py @@ -0,0 +1,25 @@ +def test_asyncpg_config_setup() -> None: + import os + + from sqlspec.adapters.asyncpg import AsyncpgConfig + + host = os.getenv("SQLSPEC_USAGE_PG_HOST", "localhost") + port = int(os.getenv("SQLSPEC_USAGE_PG_PORT", "5432")) + user = os.getenv("SQLSPEC_USAGE_PG_USER", "user") + password = os.getenv("SQLSPEC_USAGE_PG_PASSWORD", "password") + database = os.getenv("SQLSPEC_USAGE_PG_DATABASE", "db" ) + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", f"postgresql://{user}:{password}@{host}:{port}/{database}") + + config = AsyncpgConfig( + pool_config={ + "dsn": dsn, + "min_size": 10, + "max_size": 20, + "host": host, + "port": port, + "user": user, + "password": password, + "database": database, + } + ) + assert config.pool_config["host"] == host diff --git a/docs/examples/usage/usage_configuration_5.py b/docs/examples/usage/usage_configuration_5.py new file mode 100644 index 00000000..9cd3377e --- /dev/null +++ b/docs/examples/usage/usage_configuration_5.py @@ -0,0 +1,29 @@ +def test_psycopg_config_setup() -> None: + import os + + from sqlspec.adapters.psycopg import PsycopgAsyncConfig + + host = os.getenv("SQLSPEC_USAGE_PG_HOST", "localhost") + port = int(os.getenv("SQLSPEC_USAGE_PG_PORT", "5432")) + database = os.getenv("SQLSPEC_USAGE_PG_DATABASE", "db") + user = os.getenv("SQLSPEC_USAGE_PG_USER", "user") + password = os.getenv("SQLSPEC_USAGE_PG_PASSWORD", "password") + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", f"postgresql://{user}:{password}@{host}:{port}/{database}") + + # Async version + config = PsycopgAsyncConfig( + pool_config={ + "conninfo": dsn, + # Or keyword arguments: + "host": host, + "port": port, + "dbname": database, + "user": user, + "password": password, + # Pool settings + "min_size": 5, + "max_size": 10, + "timeout": 30.0, + } + ) + assert config.pool_config is not None diff --git a/docs/examples/usage/test_configuration_6.py b/docs/examples/usage/usage_configuration_6.py similarity index 100% rename from docs/examples/usage/test_configuration_6.py rename to docs/examples/usage/usage_configuration_6.py diff --git a/docs/examples/usage/test_configuration_7.py b/docs/examples/usage/usage_configuration_7.py similarity index 100% rename from docs/examples/usage/test_configuration_7.py rename to docs/examples/usage/usage_configuration_7.py diff --git a/docs/examples/usage/test_configuration_8.py b/docs/examples/usage/usage_configuration_8.py similarity index 77% rename from docs/examples/usage/test_configuration_8.py rename to docs/examples/usage/usage_configuration_8.py index 6cd0a16d..acf7624a 100644 --- a/docs/examples/usage/test_configuration_8.py +++ b/docs/examples/usage/usage_configuration_8.py @@ -2,11 +2,15 @@ def test_asyncpg_pool_setup() -> None: + import os + from sqlspec.adapters.asyncpg import AsyncpgConfig + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") + config = AsyncpgConfig( pool_config={ - "dsn": "postgresql://localhost/db", + "dsn": dsn, "min_size": 10, "max_size": 20, "max_queries": 50000, diff --git a/docs/examples/usage/usage_configuration_9.py b/docs/examples/usage/usage_configuration_9.py new file mode 100644 index 00000000..0e254495 --- /dev/null +++ b/docs/examples/usage/usage_configuration_9.py @@ -0,0 +1,12 @@ +def test_pool_lifecycle() -> None: + import os + + from sqlspec import SQLSpec + from sqlspec.adapters.asyncpg import AsyncpgConfig + + db_manager = SQLSpec() + dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") + asyncpg_key = db_manager.add_config(AsyncpgConfig(pool_config={"dsn": dsn})) + + asyncpg_config = db_manager.get_config(asyncpg_key) + assert asyncpg_config.pool_config["dsn"] == dsn diff --git a/docs/usage/configuration.rst b/docs/usage/configuration.rst index 45685f62..dfb81ed0 100644 --- a/docs/usage/configuration.rst +++ b/docs/usage/configuration.rst @@ -18,7 +18,7 @@ Basic Configuration The simplest way to use SQLSpec is with default configuration: -.. literalinclude:: /examples/usage/test_configuration_1.py +.. literalinclude:: /examples/usage/usage_configuration_1.py :language: python :caption: `basic configuration` :lines: 2-14 @@ -29,10 +29,17 @@ Database Configurations Each database adapter has its own configuration class with adapter-specific settings. +.. note:: + + Async PostgreSQL examples in this guide read their connection details from + ``SQLSPEC_USAGE_PG_*`` environment variables. The test suite populates these + variables (host, port, user, password, database, DSN) automatically so the + literalincluded snippets can stay identical to the documentation. + SQLite Configuration ^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_2.py +.. literalinclude:: /examples/usage/usage_configuration_2.py :language: python :caption: `sqlite configuration` :lines: 2-11 @@ -40,7 +47,7 @@ SQLite Configuration **Memory Databases** -.. literalinclude:: /examples/usage/test_configuration_3.py +.. literalinclude:: /examples/usage/usage_configuration_3.py :language: python :caption: `memory sqlite configuration` :lines: 2-13 @@ -49,7 +56,7 @@ SQLite Configuration PostgreSQL Configuration (asyncpg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_4.py +.. literalinclude:: /examples/usage/usage_configuration_4.py :language: python :caption: `postgres asyncpg configuration` :lines: 2-16 @@ -58,7 +65,7 @@ PostgreSQL Configuration (asyncpg) PostgreSQL Configuration (psycopg) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_5.py +.. literalinclude:: /examples/usage/usage_configuration_5.py :language: python :caption: `psycopg async configuration` :lines: 2-19 @@ -67,7 +74,7 @@ PostgreSQL Configuration (psycopg) MySQL Configuration (asyncmy) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_6.py +.. literalinclude:: /examples/usage/usage_configuration_6.py :language: python :caption: `mysql asyncmy configuration` :lines: 2-15 @@ -76,7 +83,7 @@ MySQL Configuration (asyncmy) DuckDB Configuration ^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_7.py +.. literalinclude:: /examples/usage/usage_configuration_7.py :language: python :caption: `duckdb configuration` :lines: 2-11 @@ -90,7 +97,7 @@ Connection pooling improves performance by reusing database connections. SQLSpec Pool Configuration ^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_8.py +.. literalinclude:: /examples/usage/usage_configuration_8.py :language: python :caption: `pool configuration` :lines: 2-11 @@ -98,7 +105,7 @@ Pool Configuration **Pool Lifecycle Management** -.. literalinclude:: /examples/usage/test_configuration_9.py +.. literalinclude:: /examples/usage/usage_configuration_9.py :language: python :caption: `pool lifecycle management` :lines: 2-7 @@ -107,7 +114,7 @@ Pool Configuration Using Pre-Created Pools ^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_10.py +.. literalinclude:: /examples/usage/usage_configuration_10.py :language: python :caption: `using pre-created pools` :lines: 2-9 @@ -116,7 +123,7 @@ Using Pre-Created Pools No-Pooling Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_11.py +.. literalinclude:: /examples/usage/usage_configuration_11.py :language: python :caption: `no-pooling configuration` :lines: 2-4 @@ -130,7 +137,7 @@ Statement configuration controls SQL processing pipeline behavior. Basic Statement Config ^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_12.py +.. literalinclude:: /examples/usage/usage_configuration_12.py :language: python :caption: `basic statement config` :lines: 2-14 @@ -139,7 +146,7 @@ Basic Statement Config Parameter Style Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_13.py +.. literalinclude:: /examples/usage/usage_configuration_13.py :language: python :caption: `parameter style configuration` :lines: 2-17 @@ -149,7 +156,7 @@ Parameter Style Configuration SQLSpec supports multiple parameter placeholder styles: -.. literalinclude:: /examples/usage/test_configuration_14.py +.. literalinclude:: /examples/usage/usage_configuration_14.py :language: python :caption: `parameter styles` :lines: 2-21 @@ -162,7 +169,7 @@ Configure security and performance validation. Disable validation for performance-critical paths where input is trusted: -.. literalinclude:: /examples/usage/test_configuration_15.py +.. literalinclude:: /examples/usage/usage_configuration_15.py :language: python :caption: `validation configuration` :lines: 5-17 @@ -176,7 +183,7 @@ SQLSpec uses multi-tier caching to avoid recompiling SQL statements. Global Cache Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_16.py +.. literalinclude:: /examples/usage/usage_configuration_16.py :language: python :caption: `global cache configuration` :lines: 6-28 @@ -185,7 +192,7 @@ Global Cache Configuration Per-Instance Cache Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_17.py +.. literalinclude:: /examples/usage/usage_configuration_17.py :language: python :caption: `per-instance cache configuration` :lines: 6-27 @@ -196,7 +203,7 @@ Cache Statistics Monitor cache statistics: -.. literalinclude:: /examples/usage/test_configuration_18.py +.. literalinclude:: /examples/usage/usage_configuration_18.py :language: python :caption: `cache statistics` :lines: 6-18 @@ -205,7 +212,7 @@ Monitor cache statistics: Clear Cache ^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_19.py +.. literalinclude:: /examples/usage/usage_configuration_19.py :language: python :caption: `clear cache` :lines: 6-24 @@ -219,10 +226,10 @@ SQLSpec supports multiple database configurations in a single application. Binding Multiple Configs ^^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_20.py +.. literalinclude:: /examples/usage/usage_configuration_19.py :language: python :caption: `binding multiple configurations` - :lines: 6-24 + :lines: 4-27 :dedent: 2 Named Bindings @@ -230,12 +237,19 @@ Named Bindings Use bind keys for clearer configuration management: -.. literalinclude:: /examples/usage/test_configuration_21.py +.. literalinclude:: /examples/usage/usage_configuration_20.py :language: python :caption: `named bindings` - :lines: 11-26 + :lines: 6-25 :dedent: 2 +``SQLSpec.add_config()`` returns a key for the registered configuration. +Store that value (as shown above) and reuse it when calling +``provide_session()`` or ``get_config()``. The config registry holds a single +instance per config type, so creating multiple variants of the same adapter +requires defining lightweight subclasses or binding unique config classes for +each database. + Migration Configuration ----------------------- @@ -244,7 +258,7 @@ SQLSpec includes a migration system for schema management. Basic Migration Config ^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_22.py +.. literalinclude:: /examples/usage/usage_configuration_22.py :language: python :caption: `basic migration config` :lines: 2-15 @@ -290,10 +304,10 @@ Framework integrations can be configured via ``extension_config``. Litestar Plugin Configuration ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -.. literalinclude:: /examples/usage/test_configuration_23.py +.. literalinclude:: /examples/usage/usage_configuration_23.py :language: python :caption: `litestar plugin configuration` - :lines: 2-26 + :lines: 4-28 :dedent: 2 Telemetry Snapshot @@ -301,21 +315,20 @@ Telemetry Snapshot Call ``SQLSpec.telemetry_snapshot()`` to inspect lifecycle counters, serializer metrics, and recent storage jobs: -.. code-block:: python - - snapshot = spec.telemetry_snapshot() - print(snapshot["storage_bridge.bytes_written"]) - for job in snapshot.get("storage_bridge.recent_jobs", []): - print(job["destination"], job.get("correlation_id")) +.. literalinclude:: /examples/usage/usage_configuration_30.py + :language: python + :caption: `telemetry snapshot` + :lines: 1-15 + :dedent: 0 Environment-Based Configuration ------------------------------- Use environment variables for configuration: -.. literalinclude:: /examples/usage/test_configuration_24.py +.. literalinclude:: /examples/usage/usage_configuration_24.py :language: python - :caption: `environnment-based configuration` + :caption: `environment-based configuration` :lines: 25-41 :dedent: 4 @@ -326,7 +339,7 @@ Configuration Best Practices Always use pooling in production: -.. literalinclude:: /examples/usage/test_configuration_25.py +.. literalinclude:: /examples/usage/usage_configuration_25.py :language: python :caption: `connection pooling` :lines: 11-13 @@ -336,7 +349,7 @@ Always use pooling in production: Enable caching to avoid recompiling SQL statements: -.. literalinclude:: /examples/usage/test_configuration_26.py +.. literalinclude:: /examples/usage/usage_configuration_26.py :language: python :caption: `enable caching` :lines: 6-8 @@ -346,7 +359,7 @@ Enable caching to avoid recompiling SQL statements: Size pools based on your workload: -.. literalinclude:: /examples/usage/test_configuration_27.py +.. literalinclude:: /examples/usage/usage_configuration_27.py :language: python :caption: `tune pool sizes` :lines: 3-20 @@ -356,7 +369,7 @@ Size pools based on your workload: For trusted, performance-critical queries: -.. literalinclude:: /examples/usage/test_configuration_28.py +.. literalinclude:: /examples/usage/usage_configuration_28.py :language: python :caption: `no validation` :lines: 6-12 @@ -367,7 +380,7 @@ For trusted, performance-critical queries: Always close pools on shutdown: -.. literalinclude:: /examples/usage/test_configuration_29.py +.. literalinclude:: /examples/usage/usage_configuration_29.py :language: python :caption: `cleanup resources` :lines: 10-27 From 4629000ea75d17d15e3e22cf9674f523d90f4c03 Mon Sep 17 00:00:00 2001 From: euri10 Date: Mon, 10 Nov 2025 15:36:51 +0100 Subject: [PATCH 17/21] ruff --- docs/examples/usage/usage_configuration_1.py | 3 +++ docs/examples/usage/usage_configuration_10.py | 2 ++ docs/examples/usage/usage_configuration_11.py | 3 +++ docs/examples/usage/usage_configuration_12.py | 3 +++ docs/examples/usage/usage_configuration_13.py | 3 +++ docs/examples/usage/usage_configuration_14.py | 3 +++ docs/examples/usage/usage_configuration_15.py | 3 +++ docs/examples/usage/usage_configuration_16.py | 2 ++ docs/examples/usage/usage_configuration_17.py | 2 ++ docs/examples/usage/usage_configuration_18.py | 2 ++ docs/examples/usage/usage_configuration_19.py | 2 ++ docs/examples/usage/usage_configuration_2.py | 3 +++ docs/examples/usage/usage_configuration_20.py | 6 +++--- docs/examples/usage/usage_configuration_21.py | 2 ++ docs/examples/usage/usage_configuration_22.py | 3 +++ docs/examples/usage/usage_configuration_23.py | 2 ++ docs/examples/usage/usage_configuration_24.py | 3 +++ docs/examples/usage/usage_configuration_25.py | 3 +++ docs/examples/usage/usage_configuration_26.py | 2 ++ docs/examples/usage/usage_configuration_27.py | 3 +++ docs/examples/usage/usage_configuration_28.py | 2 ++ docs/examples/usage/usage_configuration_29.py | 2 ++ docs/examples/usage/usage_configuration_3.py | 3 +++ docs/examples/usage/usage_configuration_30.py | 2 ++ docs/examples/usage/usage_configuration_4.py | 5 ++++- docs/examples/usage/usage_configuration_5.py | 3 +++ docs/examples/usage/usage_configuration_6.py | 2 ++ docs/examples/usage/usage_configuration_7.py | 3 +++ docs/examples/usage/usage_configuration_8.py | 2 ++ docs/examples/usage/usage_configuration_9.py | 3 +++ 30 files changed, 78 insertions(+), 4 deletions(-) diff --git a/docs/examples/usage/usage_configuration_1.py b/docs/examples/usage/usage_configuration_1.py index dbe871f5..578f06b4 100644 --- a/docs/examples/usage/usage_configuration_1.py +++ b/docs/examples/usage/usage_configuration_1.py @@ -1,4 +1,7 @@ def test_sqlite_memory_db() -> None: +__all__ = ("test_sqlite_memory_db", ) + + from sqlspec import SQLSpec from sqlspec.adapters.sqlite import SqliteConfig diff --git a/docs/examples/usage/usage_configuration_10.py b/docs/examples/usage/usage_configuration_10.py index 0e78544a..ab8b0e4d 100644 --- a/docs/examples/usage/usage_configuration_10.py +++ b/docs/examples/usage/usage_configuration_10.py @@ -1,4 +1,6 @@ POOL_INSTANCE = 20 +__all__ = ("test_manual_pool", ) + def test_manual_pool() -> None: diff --git a/docs/examples/usage/usage_configuration_11.py b/docs/examples/usage/usage_configuration_11.py index e1545362..b2e414c9 100644 --- a/docs/examples/usage/usage_configuration_11.py +++ b/docs/examples/usage/usage_configuration_11.py @@ -1,4 +1,7 @@ def test_thread_local_connections() -> None: +__all__ = ("test_thread_local_connections", ) + + from sqlspec.adapters.sqlite import SqliteConfig config = SqliteConfig(pool_config={"database": "test.db"}) diff --git a/docs/examples/usage/usage_configuration_12.py b/docs/examples/usage/usage_configuration_12.py index 42ea8a86..8466106b 100644 --- a/docs/examples/usage/usage_configuration_12.py +++ b/docs/examples/usage/usage_configuration_12.py @@ -1,4 +1,7 @@ def test_basic_statement_config() -> None: +__all__ = ("test_basic_statement_config", ) + + import os from sqlspec import StatementConfig diff --git a/docs/examples/usage/usage_configuration_13.py b/docs/examples/usage/usage_configuration_13.py index 4c5e8eb9..62de10fd 100644 --- a/docs/examples/usage/usage_configuration_13.py +++ b/docs/examples/usage/usage_configuration_13.py @@ -1,4 +1,7 @@ def test_parameter_style_config() -> None: +__all__ = ("test_parameter_style_config", ) + + from sqlspec import ParameterStyle, ParameterStyleConfig, StatementConfig param_config = ParameterStyleConfig( diff --git a/docs/examples/usage/usage_configuration_14.py b/docs/examples/usage/usage_configuration_14.py index 0bfda1f2..01d12988 100644 --- a/docs/examples/usage/usage_configuration_14.py +++ b/docs/examples/usage/usage_configuration_14.py @@ -1,4 +1,7 @@ def test_parameter_styles() -> None: +__all__ = ("test_parameter_styles", ) + + from sqlspec import ParameterStyle # Question mark (SQLite, DuckDB) diff --git a/docs/examples/usage/usage_configuration_15.py b/docs/examples/usage/usage_configuration_15.py index 359a4968..330d7a2a 100644 --- a/docs/examples/usage/usage_configuration_15.py +++ b/docs/examples/usage/usage_configuration_15.py @@ -1,5 +1,8 @@ """Test configuration example: Global cache configuration.""" +__all__ = ("test_global_cache_config", ) + + SQL_CACHE_SIZE = 1000 diff --git a/docs/examples/usage/usage_configuration_16.py b/docs/examples/usage/usage_configuration_16.py index f37009eb..584ec649 100644 --- a/docs/examples/usage/usage_configuration_16.py +++ b/docs/examples/usage/usage_configuration_16.py @@ -1,5 +1,7 @@ """Test configuration example: Per-instance cache configuration.""" +__all__ = ("test_per_instance_cache_config", ) + def test_per_instance_cache_config() -> None: """Test per-instance cache configuration.""" diff --git a/docs/examples/usage/usage_configuration_17.py b/docs/examples/usage/usage_configuration_17.py index c3a988c4..bcf3012a 100644 --- a/docs/examples/usage/usage_configuration_17.py +++ b/docs/examples/usage/usage_configuration_17.py @@ -1,5 +1,7 @@ """Test configuration example: Cache statistics tracking.""" +__all__ = ("test_cache_statistics", ) + def test_cache_statistics() -> None: """Test cache statistics tracking.""" diff --git a/docs/examples/usage/usage_configuration_18.py b/docs/examples/usage/usage_configuration_18.py index d17b693d..fb1edda7 100644 --- a/docs/examples/usage/usage_configuration_18.py +++ b/docs/examples/usage/usage_configuration_18.py @@ -1,5 +1,7 @@ """Test configuration example: Cache clearing operations.""" +__all__ = ("test_clear_cache", ) + def test_clear_cache() -> None: """Test cache clearing operations.""" diff --git a/docs/examples/usage/usage_configuration_19.py b/docs/examples/usage/usage_configuration_19.py index 6020d20c..90f99de3 100644 --- a/docs/examples/usage/usage_configuration_19.py +++ b/docs/examples/usage/usage_configuration_19.py @@ -1,5 +1,7 @@ """Test configuration example: Binding multiple database configurations.""" +__all__ = ("test_binding_multiple_configs", ) + def test_binding_multiple_configs() -> None: """Test binding multiple database configurations.""" diff --git a/docs/examples/usage/usage_configuration_2.py b/docs/examples/usage/usage_configuration_2.py index 2cc4e66e..a84ea5cc 100644 --- a/docs/examples/usage/usage_configuration_2.py +++ b/docs/examples/usage/usage_configuration_2.py @@ -1,4 +1,7 @@ def test_sqlite_config_setup() -> None: +__all__ = ("test_sqlite_config_setup", ) + + from sqlspec.adapters.sqlite import SqliteConfig config = SqliteConfig( diff --git a/docs/examples/usage/usage_configuration_20.py b/docs/examples/usage/usage_configuration_20.py index 06a26634..fa29494f 100644 --- a/docs/examples/usage/usage_configuration_20.py +++ b/docs/examples/usage/usage_configuration_20.py @@ -1,5 +1,7 @@ """Test configuration example: Named database bindings.""" +__all__ = ("test_named_bindings", ) + def test_named_bindings() -> None: """Test named database bindings.""" @@ -14,9 +16,7 @@ def test_named_bindings() -> None: db_manager = SQLSpec() # Add with bind keys - cache_key = db_manager.add_config( - SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db") - ) + cache_key = db_manager.add_config(SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db")) dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") main_key = db_manager.add_config(AsyncpgConfig(pool_config={"dsn": dsn}, bind_key="main_db")) diff --git a/docs/examples/usage/usage_configuration_21.py b/docs/examples/usage/usage_configuration_21.py index 183a9b5f..c4ea71fe 100644 --- a/docs/examples/usage/usage_configuration_21.py +++ b/docs/examples/usage/usage_configuration_21.py @@ -2,6 +2,8 @@ import pytest +__all__ = ("test_basic_migration_config", ) + @pytest.mark.skipif( not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" diff --git a/docs/examples/usage/usage_configuration_22.py b/docs/examples/usage/usage_configuration_22.py index d6986916..1300a372 100644 --- a/docs/examples/usage/usage_configuration_22.py +++ b/docs/examples/usage/usage_configuration_22.py @@ -1,4 +1,7 @@ def test_basic_migration_config() -> None: +__all__ = ("test_basic_migration_config", ) + + import os from sqlspec.adapters.asyncpg import AsyncpgConfig diff --git a/docs/examples/usage/usage_configuration_23.py b/docs/examples/usage/usage_configuration_23.py index a04f57c3..327e2f79 100644 --- a/docs/examples/usage/usage_configuration_23.py +++ b/docs/examples/usage/usage_configuration_23.py @@ -1,5 +1,7 @@ """Test configuration example: Environment-based configuration.""" +__all__ = ("test_extension_config", ) + def test_extension_config() -> None: import os diff --git a/docs/examples/usage/usage_configuration_24.py b/docs/examples/usage/usage_configuration_24.py index bcad9ddf..d0b84c37 100644 --- a/docs/examples/usage/usage_configuration_24.py +++ b/docs/examples/usage/usage_configuration_24.py @@ -3,6 +3,9 @@ import os from unittest.mock import patch +__all__ = ("test_environment_based_configuration", ) + + POSTGRES_PORT = 5433 diff --git a/docs/examples/usage/usage_configuration_25.py b/docs/examples/usage/usage_configuration_25.py index c4e2110a..94afe85d 100644 --- a/docs/examples/usage/usage_configuration_25.py +++ b/docs/examples/usage/usage_configuration_25.py @@ -2,6 +2,9 @@ import pytest +__all__ = ("test_connection_pooling_best_practice", ) + + MIN_POOL_SIZE = 10 MAX_POOL_SIZE = 20 diff --git a/docs/examples/usage/usage_configuration_26.py b/docs/examples/usage/usage_configuration_26.py index ad19e70c..af1efe3d 100644 --- a/docs/examples/usage/usage_configuration_26.py +++ b/docs/examples/usage/usage_configuration_26.py @@ -1,5 +1,7 @@ """Test configuration example: Best practice - Enable caching.""" +__all__ = ("test_enable_caching_best_practice", ) + def test_enable_caching_best_practice() -> None: """Test caching best practice configuration.""" diff --git a/docs/examples/usage/usage_configuration_27.py b/docs/examples/usage/usage_configuration_27.py index 921c0cad..b7a6389e 100644 --- a/docs/examples/usage/usage_configuration_27.py +++ b/docs/examples/usage/usage_configuration_27.py @@ -1,5 +1,8 @@ """Test configuration example: Best practice - Tune pool sizes.""" +__all__ = ("test_tune_pool_sizes_best_practice", ) + + MIN_POOL_SIZE_CPU = 5 MAX_POOL_SIZE_CPU = 10 MIN_IO_BOUND_POOL_SIZE = 20 diff --git a/docs/examples/usage/usage_configuration_28.py b/docs/examples/usage/usage_configuration_28.py index cc0ece0a..796de7c9 100644 --- a/docs/examples/usage/usage_configuration_28.py +++ b/docs/examples/usage/usage_configuration_28.py @@ -1,5 +1,7 @@ """Test configuration example: Best practice - Tune pool sizes.""" +__all__ = ("test_disable_security_checks_best_practice", ) + def test_disable_security_checks_best_practice() -> None: """Test disabling security checks when necessary.""" diff --git a/docs/examples/usage/usage_configuration_29.py b/docs/examples/usage/usage_configuration_29.py index 120c81a7..0dada143 100644 --- a/docs/examples/usage/usage_configuration_29.py +++ b/docs/examples/usage/usage_configuration_29.py @@ -2,6 +2,8 @@ import pytest +__all__ = ("test_cleanup_resources_best_practice", ) + @pytest.mark.asyncio async def test_cleanup_resources_best_practice() -> None: diff --git a/docs/examples/usage/usage_configuration_3.py b/docs/examples/usage/usage_configuration_3.py index e53b38e3..7fad9375 100644 --- a/docs/examples/usage/usage_configuration_3.py +++ b/docs/examples/usage/usage_configuration_3.py @@ -1,4 +1,7 @@ def test_memory_databases() -> None: +__all__ = ("test_memory_databases", ) + + from sqlspec.adapters.sqlite import SqliteConfig # In-memory database (isolated per connection) diff --git a/docs/examples/usage/usage_configuration_30.py b/docs/examples/usage/usage_configuration_30.py index 9b653400..4ed177b2 100644 --- a/docs/examples/usage/usage_configuration_30.py +++ b/docs/examples/usage/usage_configuration_30.py @@ -1,5 +1,7 @@ """Telemetry snapshot example.""" +__all__ = ("test_telemetry_snapshot", ) + def test_telemetry_snapshot() -> None: """Demonstrate SQLSpec.telemetry_snapshot().""" diff --git a/docs/examples/usage/usage_configuration_4.py b/docs/examples/usage/usage_configuration_4.py index a8de0ded..99c2dc55 100644 --- a/docs/examples/usage/usage_configuration_4.py +++ b/docs/examples/usage/usage_configuration_4.py @@ -1,4 +1,7 @@ def test_asyncpg_config_setup() -> None: +__all__ = ("test_asyncpg_config_setup", ) + + import os from sqlspec.adapters.asyncpg import AsyncpgConfig @@ -7,7 +10,7 @@ def test_asyncpg_config_setup() -> None: port = int(os.getenv("SQLSPEC_USAGE_PG_PORT", "5432")) user = os.getenv("SQLSPEC_USAGE_PG_USER", "user") password = os.getenv("SQLSPEC_USAGE_PG_PASSWORD", "password") - database = os.getenv("SQLSPEC_USAGE_PG_DATABASE", "db" ) + database = os.getenv("SQLSPEC_USAGE_PG_DATABASE", "db") dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", f"postgresql://{user}:{password}@{host}:{port}/{database}") config = AsyncpgConfig( diff --git a/docs/examples/usage/usage_configuration_5.py b/docs/examples/usage/usage_configuration_5.py index 9cd3377e..3ad67c3e 100644 --- a/docs/examples/usage/usage_configuration_5.py +++ b/docs/examples/usage/usage_configuration_5.py @@ -1,4 +1,7 @@ def test_psycopg_config_setup() -> None: +__all__ = ("test_psycopg_config_setup", ) + + import os from sqlspec.adapters.psycopg import PsycopgAsyncConfig diff --git a/docs/examples/usage/usage_configuration_6.py b/docs/examples/usage/usage_configuration_6.py index 9355cb4c..f73f089a 100644 --- a/docs/examples/usage/usage_configuration_6.py +++ b/docs/examples/usage/usage_configuration_6.py @@ -1,4 +1,6 @@ MYSQL_PORT = 3306 +__all__ = ("test_asyncmy_config_setup", ) + def test_asyncmy_config_setup() -> None: diff --git a/docs/examples/usage/usage_configuration_7.py b/docs/examples/usage/usage_configuration_7.py index 3152d6ed..7083b5da 100644 --- a/docs/examples/usage/usage_configuration_7.py +++ b/docs/examples/usage/usage_configuration_7.py @@ -1,4 +1,7 @@ def test_duckdb_config_setup() -> None: +__all__ = ("test_duckdb_config_setup", ) + + from sqlspec.adapters.duckdb import DuckDBConfig in_memory_config = DuckDBConfig() diff --git a/docs/examples/usage/usage_configuration_8.py b/docs/examples/usage/usage_configuration_8.py index acf7624a..7862451c 100644 --- a/docs/examples/usage/usage_configuration_8.py +++ b/docs/examples/usage/usage_configuration_8.py @@ -1,4 +1,6 @@ MIN_POOL_SIZE = 10 +__all__ = ("test_asyncpg_pool_setup", ) + def test_asyncpg_pool_setup() -> None: diff --git a/docs/examples/usage/usage_configuration_9.py b/docs/examples/usage/usage_configuration_9.py index 0e254495..553fb83e 100644 --- a/docs/examples/usage/usage_configuration_9.py +++ b/docs/examples/usage/usage_configuration_9.py @@ -1,4 +1,7 @@ def test_pool_lifecycle() -> None: +__all__ = ("test_pool_lifecycle", ) + + import os from sqlspec import SQLSpec From 05a1b8f587386619ad48ffde1a4ee18d2b246e24 Mon Sep 17 00:00:00 2001 From: euri10 Date: Mon, 10 Nov 2025 15:37:29 +0100 Subject: [PATCH 18/21] Revert "ruff" This reverts commit 4629000ea75d17d15e3e22cf9674f523d90f4c03. --- docs/examples/usage/usage_configuration_1.py | 3 --- docs/examples/usage/usage_configuration_10.py | 2 -- docs/examples/usage/usage_configuration_11.py | 3 --- docs/examples/usage/usage_configuration_12.py | 3 --- docs/examples/usage/usage_configuration_13.py | 3 --- docs/examples/usage/usage_configuration_14.py | 3 --- docs/examples/usage/usage_configuration_15.py | 3 --- docs/examples/usage/usage_configuration_16.py | 2 -- docs/examples/usage/usage_configuration_17.py | 2 -- docs/examples/usage/usage_configuration_18.py | 2 -- docs/examples/usage/usage_configuration_19.py | 2 -- docs/examples/usage/usage_configuration_2.py | 3 --- docs/examples/usage/usage_configuration_20.py | 6 +++--- docs/examples/usage/usage_configuration_21.py | 2 -- docs/examples/usage/usage_configuration_22.py | 3 --- docs/examples/usage/usage_configuration_23.py | 2 -- docs/examples/usage/usage_configuration_24.py | 3 --- docs/examples/usage/usage_configuration_25.py | 3 --- docs/examples/usage/usage_configuration_26.py | 2 -- docs/examples/usage/usage_configuration_27.py | 3 --- docs/examples/usage/usage_configuration_28.py | 2 -- docs/examples/usage/usage_configuration_29.py | 2 -- docs/examples/usage/usage_configuration_3.py | 3 --- docs/examples/usage/usage_configuration_30.py | 2 -- docs/examples/usage/usage_configuration_4.py | 5 +---- docs/examples/usage/usage_configuration_5.py | 3 --- docs/examples/usage/usage_configuration_6.py | 2 -- docs/examples/usage/usage_configuration_7.py | 3 --- docs/examples/usage/usage_configuration_8.py | 2 -- docs/examples/usage/usage_configuration_9.py | 3 --- 30 files changed, 4 insertions(+), 78 deletions(-) diff --git a/docs/examples/usage/usage_configuration_1.py b/docs/examples/usage/usage_configuration_1.py index 578f06b4..dbe871f5 100644 --- a/docs/examples/usage/usage_configuration_1.py +++ b/docs/examples/usage/usage_configuration_1.py @@ -1,7 +1,4 @@ def test_sqlite_memory_db() -> None: -__all__ = ("test_sqlite_memory_db", ) - - from sqlspec import SQLSpec from sqlspec.adapters.sqlite import SqliteConfig diff --git a/docs/examples/usage/usage_configuration_10.py b/docs/examples/usage/usage_configuration_10.py index ab8b0e4d..0e78544a 100644 --- a/docs/examples/usage/usage_configuration_10.py +++ b/docs/examples/usage/usage_configuration_10.py @@ -1,6 +1,4 @@ POOL_INSTANCE = 20 -__all__ = ("test_manual_pool", ) - def test_manual_pool() -> None: diff --git a/docs/examples/usage/usage_configuration_11.py b/docs/examples/usage/usage_configuration_11.py index b2e414c9..e1545362 100644 --- a/docs/examples/usage/usage_configuration_11.py +++ b/docs/examples/usage/usage_configuration_11.py @@ -1,7 +1,4 @@ def test_thread_local_connections() -> None: -__all__ = ("test_thread_local_connections", ) - - from sqlspec.adapters.sqlite import SqliteConfig config = SqliteConfig(pool_config={"database": "test.db"}) diff --git a/docs/examples/usage/usage_configuration_12.py b/docs/examples/usage/usage_configuration_12.py index 8466106b..42ea8a86 100644 --- a/docs/examples/usage/usage_configuration_12.py +++ b/docs/examples/usage/usage_configuration_12.py @@ -1,7 +1,4 @@ def test_basic_statement_config() -> None: -__all__ = ("test_basic_statement_config", ) - - import os from sqlspec import StatementConfig diff --git a/docs/examples/usage/usage_configuration_13.py b/docs/examples/usage/usage_configuration_13.py index 62de10fd..4c5e8eb9 100644 --- a/docs/examples/usage/usage_configuration_13.py +++ b/docs/examples/usage/usage_configuration_13.py @@ -1,7 +1,4 @@ def test_parameter_style_config() -> None: -__all__ = ("test_parameter_style_config", ) - - from sqlspec import ParameterStyle, ParameterStyleConfig, StatementConfig param_config = ParameterStyleConfig( diff --git a/docs/examples/usage/usage_configuration_14.py b/docs/examples/usage/usage_configuration_14.py index 01d12988..0bfda1f2 100644 --- a/docs/examples/usage/usage_configuration_14.py +++ b/docs/examples/usage/usage_configuration_14.py @@ -1,7 +1,4 @@ def test_parameter_styles() -> None: -__all__ = ("test_parameter_styles", ) - - from sqlspec import ParameterStyle # Question mark (SQLite, DuckDB) diff --git a/docs/examples/usage/usage_configuration_15.py b/docs/examples/usage/usage_configuration_15.py index 330d7a2a..359a4968 100644 --- a/docs/examples/usage/usage_configuration_15.py +++ b/docs/examples/usage/usage_configuration_15.py @@ -1,8 +1,5 @@ """Test configuration example: Global cache configuration.""" -__all__ = ("test_global_cache_config", ) - - SQL_CACHE_SIZE = 1000 diff --git a/docs/examples/usage/usage_configuration_16.py b/docs/examples/usage/usage_configuration_16.py index 584ec649..f37009eb 100644 --- a/docs/examples/usage/usage_configuration_16.py +++ b/docs/examples/usage/usage_configuration_16.py @@ -1,7 +1,5 @@ """Test configuration example: Per-instance cache configuration.""" -__all__ = ("test_per_instance_cache_config", ) - def test_per_instance_cache_config() -> None: """Test per-instance cache configuration.""" diff --git a/docs/examples/usage/usage_configuration_17.py b/docs/examples/usage/usage_configuration_17.py index bcf3012a..c3a988c4 100644 --- a/docs/examples/usage/usage_configuration_17.py +++ b/docs/examples/usage/usage_configuration_17.py @@ -1,7 +1,5 @@ """Test configuration example: Cache statistics tracking.""" -__all__ = ("test_cache_statistics", ) - def test_cache_statistics() -> None: """Test cache statistics tracking.""" diff --git a/docs/examples/usage/usage_configuration_18.py b/docs/examples/usage/usage_configuration_18.py index fb1edda7..d17b693d 100644 --- a/docs/examples/usage/usage_configuration_18.py +++ b/docs/examples/usage/usage_configuration_18.py @@ -1,7 +1,5 @@ """Test configuration example: Cache clearing operations.""" -__all__ = ("test_clear_cache", ) - def test_clear_cache() -> None: """Test cache clearing operations.""" diff --git a/docs/examples/usage/usage_configuration_19.py b/docs/examples/usage/usage_configuration_19.py index 90f99de3..6020d20c 100644 --- a/docs/examples/usage/usage_configuration_19.py +++ b/docs/examples/usage/usage_configuration_19.py @@ -1,7 +1,5 @@ """Test configuration example: Binding multiple database configurations.""" -__all__ = ("test_binding_multiple_configs", ) - def test_binding_multiple_configs() -> None: """Test binding multiple database configurations.""" diff --git a/docs/examples/usage/usage_configuration_2.py b/docs/examples/usage/usage_configuration_2.py index a84ea5cc..2cc4e66e 100644 --- a/docs/examples/usage/usage_configuration_2.py +++ b/docs/examples/usage/usage_configuration_2.py @@ -1,7 +1,4 @@ def test_sqlite_config_setup() -> None: -__all__ = ("test_sqlite_config_setup", ) - - from sqlspec.adapters.sqlite import SqliteConfig config = SqliteConfig( diff --git a/docs/examples/usage/usage_configuration_20.py b/docs/examples/usage/usage_configuration_20.py index fa29494f..06a26634 100644 --- a/docs/examples/usage/usage_configuration_20.py +++ b/docs/examples/usage/usage_configuration_20.py @@ -1,7 +1,5 @@ """Test configuration example: Named database bindings.""" -__all__ = ("test_named_bindings", ) - def test_named_bindings() -> None: """Test named database bindings.""" @@ -16,7 +14,9 @@ def test_named_bindings() -> None: db_manager = SQLSpec() # Add with bind keys - cache_key = db_manager.add_config(SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db")) + cache_key = db_manager.add_config( + SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db") + ) dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") main_key = db_manager.add_config(AsyncpgConfig(pool_config={"dsn": dsn}, bind_key="main_db")) diff --git a/docs/examples/usage/usage_configuration_21.py b/docs/examples/usage/usage_configuration_21.py index c4ea71fe..183a9b5f 100644 --- a/docs/examples/usage/usage_configuration_21.py +++ b/docs/examples/usage/usage_configuration_21.py @@ -2,8 +2,6 @@ import pytest -__all__ = ("test_basic_migration_config", ) - @pytest.mark.skipif( not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" diff --git a/docs/examples/usage/usage_configuration_22.py b/docs/examples/usage/usage_configuration_22.py index 1300a372..d6986916 100644 --- a/docs/examples/usage/usage_configuration_22.py +++ b/docs/examples/usage/usage_configuration_22.py @@ -1,7 +1,4 @@ def test_basic_migration_config() -> None: -__all__ = ("test_basic_migration_config", ) - - import os from sqlspec.adapters.asyncpg import AsyncpgConfig diff --git a/docs/examples/usage/usage_configuration_23.py b/docs/examples/usage/usage_configuration_23.py index 327e2f79..a04f57c3 100644 --- a/docs/examples/usage/usage_configuration_23.py +++ b/docs/examples/usage/usage_configuration_23.py @@ -1,7 +1,5 @@ """Test configuration example: Environment-based configuration.""" -__all__ = ("test_extension_config", ) - def test_extension_config() -> None: import os diff --git a/docs/examples/usage/usage_configuration_24.py b/docs/examples/usage/usage_configuration_24.py index d0b84c37..bcad9ddf 100644 --- a/docs/examples/usage/usage_configuration_24.py +++ b/docs/examples/usage/usage_configuration_24.py @@ -3,9 +3,6 @@ import os from unittest.mock import patch -__all__ = ("test_environment_based_configuration", ) - - POSTGRES_PORT = 5433 diff --git a/docs/examples/usage/usage_configuration_25.py b/docs/examples/usage/usage_configuration_25.py index 94afe85d..c4e2110a 100644 --- a/docs/examples/usage/usage_configuration_25.py +++ b/docs/examples/usage/usage_configuration_25.py @@ -2,9 +2,6 @@ import pytest -__all__ = ("test_connection_pooling_best_practice", ) - - MIN_POOL_SIZE = 10 MAX_POOL_SIZE = 20 diff --git a/docs/examples/usage/usage_configuration_26.py b/docs/examples/usage/usage_configuration_26.py index af1efe3d..ad19e70c 100644 --- a/docs/examples/usage/usage_configuration_26.py +++ b/docs/examples/usage/usage_configuration_26.py @@ -1,7 +1,5 @@ """Test configuration example: Best practice - Enable caching.""" -__all__ = ("test_enable_caching_best_practice", ) - def test_enable_caching_best_practice() -> None: """Test caching best practice configuration.""" diff --git a/docs/examples/usage/usage_configuration_27.py b/docs/examples/usage/usage_configuration_27.py index b7a6389e..921c0cad 100644 --- a/docs/examples/usage/usage_configuration_27.py +++ b/docs/examples/usage/usage_configuration_27.py @@ -1,8 +1,5 @@ """Test configuration example: Best practice - Tune pool sizes.""" -__all__ = ("test_tune_pool_sizes_best_practice", ) - - MIN_POOL_SIZE_CPU = 5 MAX_POOL_SIZE_CPU = 10 MIN_IO_BOUND_POOL_SIZE = 20 diff --git a/docs/examples/usage/usage_configuration_28.py b/docs/examples/usage/usage_configuration_28.py index 796de7c9..cc0ece0a 100644 --- a/docs/examples/usage/usage_configuration_28.py +++ b/docs/examples/usage/usage_configuration_28.py @@ -1,7 +1,5 @@ """Test configuration example: Best practice - Tune pool sizes.""" -__all__ = ("test_disable_security_checks_best_practice", ) - def test_disable_security_checks_best_practice() -> None: """Test disabling security checks when necessary.""" diff --git a/docs/examples/usage/usage_configuration_29.py b/docs/examples/usage/usage_configuration_29.py index 0dada143..120c81a7 100644 --- a/docs/examples/usage/usage_configuration_29.py +++ b/docs/examples/usage/usage_configuration_29.py @@ -2,8 +2,6 @@ import pytest -__all__ = ("test_cleanup_resources_best_practice", ) - @pytest.mark.asyncio async def test_cleanup_resources_best_practice() -> None: diff --git a/docs/examples/usage/usage_configuration_3.py b/docs/examples/usage/usage_configuration_3.py index 7fad9375..e53b38e3 100644 --- a/docs/examples/usage/usage_configuration_3.py +++ b/docs/examples/usage/usage_configuration_3.py @@ -1,7 +1,4 @@ def test_memory_databases() -> None: -__all__ = ("test_memory_databases", ) - - from sqlspec.adapters.sqlite import SqliteConfig # In-memory database (isolated per connection) diff --git a/docs/examples/usage/usage_configuration_30.py b/docs/examples/usage/usage_configuration_30.py index 4ed177b2..9b653400 100644 --- a/docs/examples/usage/usage_configuration_30.py +++ b/docs/examples/usage/usage_configuration_30.py @@ -1,7 +1,5 @@ """Telemetry snapshot example.""" -__all__ = ("test_telemetry_snapshot", ) - def test_telemetry_snapshot() -> None: """Demonstrate SQLSpec.telemetry_snapshot().""" diff --git a/docs/examples/usage/usage_configuration_4.py b/docs/examples/usage/usage_configuration_4.py index 99c2dc55..a8de0ded 100644 --- a/docs/examples/usage/usage_configuration_4.py +++ b/docs/examples/usage/usage_configuration_4.py @@ -1,7 +1,4 @@ def test_asyncpg_config_setup() -> None: -__all__ = ("test_asyncpg_config_setup", ) - - import os from sqlspec.adapters.asyncpg import AsyncpgConfig @@ -10,7 +7,7 @@ def test_asyncpg_config_setup() -> None: port = int(os.getenv("SQLSPEC_USAGE_PG_PORT", "5432")) user = os.getenv("SQLSPEC_USAGE_PG_USER", "user") password = os.getenv("SQLSPEC_USAGE_PG_PASSWORD", "password") - database = os.getenv("SQLSPEC_USAGE_PG_DATABASE", "db") + database = os.getenv("SQLSPEC_USAGE_PG_DATABASE", "db" ) dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", f"postgresql://{user}:{password}@{host}:{port}/{database}") config = AsyncpgConfig( diff --git a/docs/examples/usage/usage_configuration_5.py b/docs/examples/usage/usage_configuration_5.py index 3ad67c3e..9cd3377e 100644 --- a/docs/examples/usage/usage_configuration_5.py +++ b/docs/examples/usage/usage_configuration_5.py @@ -1,7 +1,4 @@ def test_psycopg_config_setup() -> None: -__all__ = ("test_psycopg_config_setup", ) - - import os from sqlspec.adapters.psycopg import PsycopgAsyncConfig diff --git a/docs/examples/usage/usage_configuration_6.py b/docs/examples/usage/usage_configuration_6.py index f73f089a..9355cb4c 100644 --- a/docs/examples/usage/usage_configuration_6.py +++ b/docs/examples/usage/usage_configuration_6.py @@ -1,6 +1,4 @@ MYSQL_PORT = 3306 -__all__ = ("test_asyncmy_config_setup", ) - def test_asyncmy_config_setup() -> None: diff --git a/docs/examples/usage/usage_configuration_7.py b/docs/examples/usage/usage_configuration_7.py index 7083b5da..3152d6ed 100644 --- a/docs/examples/usage/usage_configuration_7.py +++ b/docs/examples/usage/usage_configuration_7.py @@ -1,7 +1,4 @@ def test_duckdb_config_setup() -> None: -__all__ = ("test_duckdb_config_setup", ) - - from sqlspec.adapters.duckdb import DuckDBConfig in_memory_config = DuckDBConfig() diff --git a/docs/examples/usage/usage_configuration_8.py b/docs/examples/usage/usage_configuration_8.py index 7862451c..acf7624a 100644 --- a/docs/examples/usage/usage_configuration_8.py +++ b/docs/examples/usage/usage_configuration_8.py @@ -1,6 +1,4 @@ MIN_POOL_SIZE = 10 -__all__ = ("test_asyncpg_pool_setup", ) - def test_asyncpg_pool_setup() -> None: diff --git a/docs/examples/usage/usage_configuration_9.py b/docs/examples/usage/usage_configuration_9.py index 553fb83e..0e254495 100644 --- a/docs/examples/usage/usage_configuration_9.py +++ b/docs/examples/usage/usage_configuration_9.py @@ -1,7 +1,4 @@ def test_pool_lifecycle() -> None: -__all__ = ("test_pool_lifecycle", ) - - import os from sqlspec import SQLSpec From c254d3238d0b3469225cfdafc9ceddf583a471aa Mon Sep 17 00:00:00 2001 From: euri10 Date: Mon, 10 Nov 2025 15:41:17 +0100 Subject: [PATCH 19/21] ruff --- docs/examples/usage/usage_configuration_1.py | 3 +++ docs/examples/usage/usage_configuration_10.py | 2 ++ docs/examples/usage/usage_configuration_11.py | 3 +++ docs/examples/usage/usage_configuration_12.py | 3 +++ docs/examples/usage/usage_configuration_13.py | 3 +++ docs/examples/usage/usage_configuration_14.py | 3 +++ docs/examples/usage/usage_configuration_15.py | 3 +++ docs/examples/usage/usage_configuration_16.py | 2 ++ docs/examples/usage/usage_configuration_17.py | 2 ++ docs/examples/usage/usage_configuration_18.py | 2 ++ docs/examples/usage/usage_configuration_19.py | 2 ++ docs/examples/usage/usage_configuration_2.py | 3 +++ docs/examples/usage/usage_configuration_20.py | 6 +++--- docs/examples/usage/usage_configuration_21.py | 2 ++ docs/examples/usage/usage_configuration_22.py | 3 +++ docs/examples/usage/usage_configuration_23.py | 2 ++ docs/examples/usage/usage_configuration_24.py | 3 +++ docs/examples/usage/usage_configuration_25.py | 3 +++ docs/examples/usage/usage_configuration_26.py | 2 ++ docs/examples/usage/usage_configuration_27.py | 3 +++ docs/examples/usage/usage_configuration_28.py | 2 ++ docs/examples/usage/usage_configuration_29.py | 2 ++ docs/examples/usage/usage_configuration_3.py | 3 +++ docs/examples/usage/usage_configuration_30.py | 2 ++ docs/examples/usage/usage_configuration_4.py | 5 ++++- docs/examples/usage/usage_configuration_5.py | 3 +++ docs/examples/usage/usage_configuration_6.py | 2 ++ docs/examples/usage/usage_configuration_7.py | 3 +++ docs/examples/usage/usage_configuration_8.py | 2 ++ docs/examples/usage/usage_configuration_9.py | 3 +++ 30 files changed, 78 insertions(+), 4 deletions(-) diff --git a/docs/examples/usage/usage_configuration_1.py b/docs/examples/usage/usage_configuration_1.py index dbe871f5..578f06b4 100644 --- a/docs/examples/usage/usage_configuration_1.py +++ b/docs/examples/usage/usage_configuration_1.py @@ -1,4 +1,7 @@ def test_sqlite_memory_db() -> None: +__all__ = ("test_sqlite_memory_db", ) + + from sqlspec import SQLSpec from sqlspec.adapters.sqlite import SqliteConfig diff --git a/docs/examples/usage/usage_configuration_10.py b/docs/examples/usage/usage_configuration_10.py index 0e78544a..ab8b0e4d 100644 --- a/docs/examples/usage/usage_configuration_10.py +++ b/docs/examples/usage/usage_configuration_10.py @@ -1,4 +1,6 @@ POOL_INSTANCE = 20 +__all__ = ("test_manual_pool", ) + def test_manual_pool() -> None: diff --git a/docs/examples/usage/usage_configuration_11.py b/docs/examples/usage/usage_configuration_11.py index e1545362..b2e414c9 100644 --- a/docs/examples/usage/usage_configuration_11.py +++ b/docs/examples/usage/usage_configuration_11.py @@ -1,4 +1,7 @@ def test_thread_local_connections() -> None: +__all__ = ("test_thread_local_connections", ) + + from sqlspec.adapters.sqlite import SqliteConfig config = SqliteConfig(pool_config={"database": "test.db"}) diff --git a/docs/examples/usage/usage_configuration_12.py b/docs/examples/usage/usage_configuration_12.py index 42ea8a86..8466106b 100644 --- a/docs/examples/usage/usage_configuration_12.py +++ b/docs/examples/usage/usage_configuration_12.py @@ -1,4 +1,7 @@ def test_basic_statement_config() -> None: +__all__ = ("test_basic_statement_config", ) + + import os from sqlspec import StatementConfig diff --git a/docs/examples/usage/usage_configuration_13.py b/docs/examples/usage/usage_configuration_13.py index 4c5e8eb9..62de10fd 100644 --- a/docs/examples/usage/usage_configuration_13.py +++ b/docs/examples/usage/usage_configuration_13.py @@ -1,4 +1,7 @@ def test_parameter_style_config() -> None: +__all__ = ("test_parameter_style_config", ) + + from sqlspec import ParameterStyle, ParameterStyleConfig, StatementConfig param_config = ParameterStyleConfig( diff --git a/docs/examples/usage/usage_configuration_14.py b/docs/examples/usage/usage_configuration_14.py index 0bfda1f2..01d12988 100644 --- a/docs/examples/usage/usage_configuration_14.py +++ b/docs/examples/usage/usage_configuration_14.py @@ -1,4 +1,7 @@ def test_parameter_styles() -> None: +__all__ = ("test_parameter_styles", ) + + from sqlspec import ParameterStyle # Question mark (SQLite, DuckDB) diff --git a/docs/examples/usage/usage_configuration_15.py b/docs/examples/usage/usage_configuration_15.py index 359a4968..330d7a2a 100644 --- a/docs/examples/usage/usage_configuration_15.py +++ b/docs/examples/usage/usage_configuration_15.py @@ -1,5 +1,8 @@ """Test configuration example: Global cache configuration.""" +__all__ = ("test_global_cache_config", ) + + SQL_CACHE_SIZE = 1000 diff --git a/docs/examples/usage/usage_configuration_16.py b/docs/examples/usage/usage_configuration_16.py index f37009eb..584ec649 100644 --- a/docs/examples/usage/usage_configuration_16.py +++ b/docs/examples/usage/usage_configuration_16.py @@ -1,5 +1,7 @@ """Test configuration example: Per-instance cache configuration.""" +__all__ = ("test_per_instance_cache_config", ) + def test_per_instance_cache_config() -> None: """Test per-instance cache configuration.""" diff --git a/docs/examples/usage/usage_configuration_17.py b/docs/examples/usage/usage_configuration_17.py index c3a988c4..bcf3012a 100644 --- a/docs/examples/usage/usage_configuration_17.py +++ b/docs/examples/usage/usage_configuration_17.py @@ -1,5 +1,7 @@ """Test configuration example: Cache statistics tracking.""" +__all__ = ("test_cache_statistics", ) + def test_cache_statistics() -> None: """Test cache statistics tracking.""" diff --git a/docs/examples/usage/usage_configuration_18.py b/docs/examples/usage/usage_configuration_18.py index d17b693d..fb1edda7 100644 --- a/docs/examples/usage/usage_configuration_18.py +++ b/docs/examples/usage/usage_configuration_18.py @@ -1,5 +1,7 @@ """Test configuration example: Cache clearing operations.""" +__all__ = ("test_clear_cache", ) + def test_clear_cache() -> None: """Test cache clearing operations.""" diff --git a/docs/examples/usage/usage_configuration_19.py b/docs/examples/usage/usage_configuration_19.py index 6020d20c..90f99de3 100644 --- a/docs/examples/usage/usage_configuration_19.py +++ b/docs/examples/usage/usage_configuration_19.py @@ -1,5 +1,7 @@ """Test configuration example: Binding multiple database configurations.""" +__all__ = ("test_binding_multiple_configs", ) + def test_binding_multiple_configs() -> None: """Test binding multiple database configurations.""" diff --git a/docs/examples/usage/usage_configuration_2.py b/docs/examples/usage/usage_configuration_2.py index 2cc4e66e..a84ea5cc 100644 --- a/docs/examples/usage/usage_configuration_2.py +++ b/docs/examples/usage/usage_configuration_2.py @@ -1,4 +1,7 @@ def test_sqlite_config_setup() -> None: +__all__ = ("test_sqlite_config_setup", ) + + from sqlspec.adapters.sqlite import SqliteConfig config = SqliteConfig( diff --git a/docs/examples/usage/usage_configuration_20.py b/docs/examples/usage/usage_configuration_20.py index 06a26634..fa29494f 100644 --- a/docs/examples/usage/usage_configuration_20.py +++ b/docs/examples/usage/usage_configuration_20.py @@ -1,5 +1,7 @@ """Test configuration example: Named database bindings.""" +__all__ = ("test_named_bindings", ) + def test_named_bindings() -> None: """Test named database bindings.""" @@ -14,9 +16,7 @@ def test_named_bindings() -> None: db_manager = SQLSpec() # Add with bind keys - cache_key = db_manager.add_config( - SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db") - ) + cache_key = db_manager.add_config(SqliteConfig(pool_config={"database": tmp.name}, bind_key="cache_db")) dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", "postgresql://localhost/db") main_key = db_manager.add_config(AsyncpgConfig(pool_config={"dsn": dsn}, bind_key="main_db")) diff --git a/docs/examples/usage/usage_configuration_21.py b/docs/examples/usage/usage_configuration_21.py index 183a9b5f..c4ea71fe 100644 --- a/docs/examples/usage/usage_configuration_21.py +++ b/docs/examples/usage/usage_configuration_21.py @@ -2,6 +2,8 @@ import pytest +__all__ = ("test_basic_migration_config", ) + @pytest.mark.skipif( not pytest.importorskip("asyncpg", reason="AsyncPG not installed"), reason="AsyncPG integration tests disabled" diff --git a/docs/examples/usage/usage_configuration_22.py b/docs/examples/usage/usage_configuration_22.py index d6986916..1300a372 100644 --- a/docs/examples/usage/usage_configuration_22.py +++ b/docs/examples/usage/usage_configuration_22.py @@ -1,4 +1,7 @@ def test_basic_migration_config() -> None: +__all__ = ("test_basic_migration_config", ) + + import os from sqlspec.adapters.asyncpg import AsyncpgConfig diff --git a/docs/examples/usage/usage_configuration_23.py b/docs/examples/usage/usage_configuration_23.py index a04f57c3..327e2f79 100644 --- a/docs/examples/usage/usage_configuration_23.py +++ b/docs/examples/usage/usage_configuration_23.py @@ -1,5 +1,7 @@ """Test configuration example: Environment-based configuration.""" +__all__ = ("test_extension_config", ) + def test_extension_config() -> None: import os diff --git a/docs/examples/usage/usage_configuration_24.py b/docs/examples/usage/usage_configuration_24.py index bcad9ddf..d0b84c37 100644 --- a/docs/examples/usage/usage_configuration_24.py +++ b/docs/examples/usage/usage_configuration_24.py @@ -3,6 +3,9 @@ import os from unittest.mock import patch +__all__ = ("test_environment_based_configuration", ) + + POSTGRES_PORT = 5433 diff --git a/docs/examples/usage/usage_configuration_25.py b/docs/examples/usage/usage_configuration_25.py index c4e2110a..94afe85d 100644 --- a/docs/examples/usage/usage_configuration_25.py +++ b/docs/examples/usage/usage_configuration_25.py @@ -2,6 +2,9 @@ import pytest +__all__ = ("test_connection_pooling_best_practice", ) + + MIN_POOL_SIZE = 10 MAX_POOL_SIZE = 20 diff --git a/docs/examples/usage/usage_configuration_26.py b/docs/examples/usage/usage_configuration_26.py index ad19e70c..af1efe3d 100644 --- a/docs/examples/usage/usage_configuration_26.py +++ b/docs/examples/usage/usage_configuration_26.py @@ -1,5 +1,7 @@ """Test configuration example: Best practice - Enable caching.""" +__all__ = ("test_enable_caching_best_practice", ) + def test_enable_caching_best_practice() -> None: """Test caching best practice configuration.""" diff --git a/docs/examples/usage/usage_configuration_27.py b/docs/examples/usage/usage_configuration_27.py index 921c0cad..b7a6389e 100644 --- a/docs/examples/usage/usage_configuration_27.py +++ b/docs/examples/usage/usage_configuration_27.py @@ -1,5 +1,8 @@ """Test configuration example: Best practice - Tune pool sizes.""" +__all__ = ("test_tune_pool_sizes_best_practice", ) + + MIN_POOL_SIZE_CPU = 5 MAX_POOL_SIZE_CPU = 10 MIN_IO_BOUND_POOL_SIZE = 20 diff --git a/docs/examples/usage/usage_configuration_28.py b/docs/examples/usage/usage_configuration_28.py index cc0ece0a..796de7c9 100644 --- a/docs/examples/usage/usage_configuration_28.py +++ b/docs/examples/usage/usage_configuration_28.py @@ -1,5 +1,7 @@ """Test configuration example: Best practice - Tune pool sizes.""" +__all__ = ("test_disable_security_checks_best_practice", ) + def test_disable_security_checks_best_practice() -> None: """Test disabling security checks when necessary.""" diff --git a/docs/examples/usage/usage_configuration_29.py b/docs/examples/usage/usage_configuration_29.py index 120c81a7..0dada143 100644 --- a/docs/examples/usage/usage_configuration_29.py +++ b/docs/examples/usage/usage_configuration_29.py @@ -2,6 +2,8 @@ import pytest +__all__ = ("test_cleanup_resources_best_practice", ) + @pytest.mark.asyncio async def test_cleanup_resources_best_practice() -> None: diff --git a/docs/examples/usage/usage_configuration_3.py b/docs/examples/usage/usage_configuration_3.py index e53b38e3..7fad9375 100644 --- a/docs/examples/usage/usage_configuration_3.py +++ b/docs/examples/usage/usage_configuration_3.py @@ -1,4 +1,7 @@ def test_memory_databases() -> None: +__all__ = ("test_memory_databases", ) + + from sqlspec.adapters.sqlite import SqliteConfig # In-memory database (isolated per connection) diff --git a/docs/examples/usage/usage_configuration_30.py b/docs/examples/usage/usage_configuration_30.py index 9b653400..4ed177b2 100644 --- a/docs/examples/usage/usage_configuration_30.py +++ b/docs/examples/usage/usage_configuration_30.py @@ -1,5 +1,7 @@ """Telemetry snapshot example.""" +__all__ = ("test_telemetry_snapshot", ) + def test_telemetry_snapshot() -> None: """Demonstrate SQLSpec.telemetry_snapshot().""" diff --git a/docs/examples/usage/usage_configuration_4.py b/docs/examples/usage/usage_configuration_4.py index a8de0ded..99c2dc55 100644 --- a/docs/examples/usage/usage_configuration_4.py +++ b/docs/examples/usage/usage_configuration_4.py @@ -1,4 +1,7 @@ def test_asyncpg_config_setup() -> None: +__all__ = ("test_asyncpg_config_setup", ) + + import os from sqlspec.adapters.asyncpg import AsyncpgConfig @@ -7,7 +10,7 @@ def test_asyncpg_config_setup() -> None: port = int(os.getenv("SQLSPEC_USAGE_PG_PORT", "5432")) user = os.getenv("SQLSPEC_USAGE_PG_USER", "user") password = os.getenv("SQLSPEC_USAGE_PG_PASSWORD", "password") - database = os.getenv("SQLSPEC_USAGE_PG_DATABASE", "db" ) + database = os.getenv("SQLSPEC_USAGE_PG_DATABASE", "db") dsn = os.getenv("SQLSPEC_USAGE_PG_DSN", f"postgresql://{user}:{password}@{host}:{port}/{database}") config = AsyncpgConfig( diff --git a/docs/examples/usage/usage_configuration_5.py b/docs/examples/usage/usage_configuration_5.py index 9cd3377e..3ad67c3e 100644 --- a/docs/examples/usage/usage_configuration_5.py +++ b/docs/examples/usage/usage_configuration_5.py @@ -1,4 +1,7 @@ def test_psycopg_config_setup() -> None: +__all__ = ("test_psycopg_config_setup", ) + + import os from sqlspec.adapters.psycopg import PsycopgAsyncConfig diff --git a/docs/examples/usage/usage_configuration_6.py b/docs/examples/usage/usage_configuration_6.py index 9355cb4c..f73f089a 100644 --- a/docs/examples/usage/usage_configuration_6.py +++ b/docs/examples/usage/usage_configuration_6.py @@ -1,4 +1,6 @@ MYSQL_PORT = 3306 +__all__ = ("test_asyncmy_config_setup", ) + def test_asyncmy_config_setup() -> None: diff --git a/docs/examples/usage/usage_configuration_7.py b/docs/examples/usage/usage_configuration_7.py index 3152d6ed..7083b5da 100644 --- a/docs/examples/usage/usage_configuration_7.py +++ b/docs/examples/usage/usage_configuration_7.py @@ -1,4 +1,7 @@ def test_duckdb_config_setup() -> None: +__all__ = ("test_duckdb_config_setup", ) + + from sqlspec.adapters.duckdb import DuckDBConfig in_memory_config = DuckDBConfig() diff --git a/docs/examples/usage/usage_configuration_8.py b/docs/examples/usage/usage_configuration_8.py index acf7624a..7862451c 100644 --- a/docs/examples/usage/usage_configuration_8.py +++ b/docs/examples/usage/usage_configuration_8.py @@ -1,4 +1,6 @@ MIN_POOL_SIZE = 10 +__all__ = ("test_asyncpg_pool_setup", ) + def test_asyncpg_pool_setup() -> None: diff --git a/docs/examples/usage/usage_configuration_9.py b/docs/examples/usage/usage_configuration_9.py index 0e254495..553fb83e 100644 --- a/docs/examples/usage/usage_configuration_9.py +++ b/docs/examples/usage/usage_configuration_9.py @@ -1,4 +1,7 @@ def test_pool_lifecycle() -> None: +__all__ = ("test_pool_lifecycle", ) + + import os from sqlspec import SQLSpec From 93154b926583e80483cd13cf3706438b7e78d35f Mon Sep 17 00:00:00 2001 From: euri10 Date: Mon, 10 Nov 2025 16:07:02 +0100 Subject: [PATCH 20/21] make fix --- docs/examples/usage/usage_configuration_1.py | 3 --- docs/examples/usage/usage_configuration_10.py | 3 +-- docs/examples/usage/usage_configuration_11.py | 3 --- docs/examples/usage/usage_configuration_12.py | 3 --- docs/examples/usage/usage_configuration_13.py | 3 --- docs/examples/usage/usage_configuration_14.py | 3 --- docs/examples/usage/usage_configuration_15.py | 2 +- docs/examples/usage/usage_configuration_16.py | 2 +- docs/examples/usage/usage_configuration_17.py | 2 +- docs/examples/usage/usage_configuration_18.py | 2 +- docs/examples/usage/usage_configuration_19.py | 2 +- docs/examples/usage/usage_configuration_2.py | 3 --- docs/examples/usage/usage_configuration_20.py | 2 +- docs/examples/usage/usage_configuration_21.py | 2 +- docs/examples/usage/usage_configuration_22.py | 3 --- docs/examples/usage/usage_configuration_23.py | 2 +- docs/examples/usage/usage_configuration_24.py | 2 +- docs/examples/usage/usage_configuration_25.py | 2 +- docs/examples/usage/usage_configuration_26.py | 2 +- docs/examples/usage/usage_configuration_27.py | 2 +- docs/examples/usage/usage_configuration_28.py | 2 +- docs/examples/usage/usage_configuration_29.py | 2 +- docs/examples/usage/usage_configuration_3.py | 3 --- docs/examples/usage/usage_configuration_30.py | 2 +- docs/examples/usage/usage_configuration_4.py | 3 --- docs/examples/usage/usage_configuration_5.py | 3 --- docs/examples/usage/usage_configuration_6.py | 3 +-- docs/examples/usage/usage_configuration_7.py | 3 --- docs/examples/usage/usage_configuration_8.py | 3 +-- docs/examples/usage/usage_configuration_9.py | 3 --- 30 files changed, 18 insertions(+), 57 deletions(-) diff --git a/docs/examples/usage/usage_configuration_1.py b/docs/examples/usage/usage_configuration_1.py index 578f06b4..dbe871f5 100644 --- a/docs/examples/usage/usage_configuration_1.py +++ b/docs/examples/usage/usage_configuration_1.py @@ -1,7 +1,4 @@ def test_sqlite_memory_db() -> None: -__all__ = ("test_sqlite_memory_db", ) - - from sqlspec import SQLSpec from sqlspec.adapters.sqlite import SqliteConfig diff --git a/docs/examples/usage/usage_configuration_10.py b/docs/examples/usage/usage_configuration_10.py index ab8b0e4d..9e209517 100644 --- a/docs/examples/usage/usage_configuration_10.py +++ b/docs/examples/usage/usage_configuration_10.py @@ -1,6 +1,5 @@ POOL_INSTANCE = 20 -__all__ = ("test_manual_pool", ) - +__all__ = ("test_manual_pool",) def test_manual_pool() -> None: diff --git a/docs/examples/usage/usage_configuration_11.py b/docs/examples/usage/usage_configuration_11.py index b2e414c9..e1545362 100644 --- a/docs/examples/usage/usage_configuration_11.py +++ b/docs/examples/usage/usage_configuration_11.py @@ -1,7 +1,4 @@ def test_thread_local_connections() -> None: -__all__ = ("test_thread_local_connections", ) - - from sqlspec.adapters.sqlite import SqliteConfig config = SqliteConfig(pool_config={"database": "test.db"}) diff --git a/docs/examples/usage/usage_configuration_12.py b/docs/examples/usage/usage_configuration_12.py index 8466106b..42ea8a86 100644 --- a/docs/examples/usage/usage_configuration_12.py +++ b/docs/examples/usage/usage_configuration_12.py @@ -1,7 +1,4 @@ def test_basic_statement_config() -> None: -__all__ = ("test_basic_statement_config", ) - - import os from sqlspec import StatementConfig diff --git a/docs/examples/usage/usage_configuration_13.py b/docs/examples/usage/usage_configuration_13.py index 62de10fd..4c5e8eb9 100644 --- a/docs/examples/usage/usage_configuration_13.py +++ b/docs/examples/usage/usage_configuration_13.py @@ -1,7 +1,4 @@ def test_parameter_style_config() -> None: -__all__ = ("test_parameter_style_config", ) - - from sqlspec import ParameterStyle, ParameterStyleConfig, StatementConfig param_config = ParameterStyleConfig( diff --git a/docs/examples/usage/usage_configuration_14.py b/docs/examples/usage/usage_configuration_14.py index 01d12988..0bfda1f2 100644 --- a/docs/examples/usage/usage_configuration_14.py +++ b/docs/examples/usage/usage_configuration_14.py @@ -1,7 +1,4 @@ def test_parameter_styles() -> None: -__all__ = ("test_parameter_styles", ) - - from sqlspec import ParameterStyle # Question mark (SQLite, DuckDB) diff --git a/docs/examples/usage/usage_configuration_15.py b/docs/examples/usage/usage_configuration_15.py index 330d7a2a..04c69abf 100644 --- a/docs/examples/usage/usage_configuration_15.py +++ b/docs/examples/usage/usage_configuration_15.py @@ -1,6 +1,6 @@ """Test configuration example: Global cache configuration.""" -__all__ = ("test_global_cache_config", ) +__all__ = ("test_global_cache_config",) SQL_CACHE_SIZE = 1000 diff --git a/docs/examples/usage/usage_configuration_16.py b/docs/examples/usage/usage_configuration_16.py index 584ec649..9950931f 100644 --- a/docs/examples/usage/usage_configuration_16.py +++ b/docs/examples/usage/usage_configuration_16.py @@ -1,6 +1,6 @@ """Test configuration example: Per-instance cache configuration.""" -__all__ = ("test_per_instance_cache_config", ) +__all__ = ("test_per_instance_cache_config",) def test_per_instance_cache_config() -> None: diff --git a/docs/examples/usage/usage_configuration_17.py b/docs/examples/usage/usage_configuration_17.py index bcf3012a..bd12eca3 100644 --- a/docs/examples/usage/usage_configuration_17.py +++ b/docs/examples/usage/usage_configuration_17.py @@ -1,6 +1,6 @@ """Test configuration example: Cache statistics tracking.""" -__all__ = ("test_cache_statistics", ) +__all__ = ("test_cache_statistics",) def test_cache_statistics() -> None: diff --git a/docs/examples/usage/usage_configuration_18.py b/docs/examples/usage/usage_configuration_18.py index fb1edda7..51c5ab23 100644 --- a/docs/examples/usage/usage_configuration_18.py +++ b/docs/examples/usage/usage_configuration_18.py @@ -1,6 +1,6 @@ """Test configuration example: Cache clearing operations.""" -__all__ = ("test_clear_cache", ) +__all__ = ("test_clear_cache",) def test_clear_cache() -> None: diff --git a/docs/examples/usage/usage_configuration_19.py b/docs/examples/usage/usage_configuration_19.py index 90f99de3..1270830e 100644 --- a/docs/examples/usage/usage_configuration_19.py +++ b/docs/examples/usage/usage_configuration_19.py @@ -1,6 +1,6 @@ """Test configuration example: Binding multiple database configurations.""" -__all__ = ("test_binding_multiple_configs", ) +__all__ = ("test_binding_multiple_configs",) def test_binding_multiple_configs() -> None: diff --git a/docs/examples/usage/usage_configuration_2.py b/docs/examples/usage/usage_configuration_2.py index a84ea5cc..2cc4e66e 100644 --- a/docs/examples/usage/usage_configuration_2.py +++ b/docs/examples/usage/usage_configuration_2.py @@ -1,7 +1,4 @@ def test_sqlite_config_setup() -> None: -__all__ = ("test_sqlite_config_setup", ) - - from sqlspec.adapters.sqlite import SqliteConfig config = SqliteConfig( diff --git a/docs/examples/usage/usage_configuration_20.py b/docs/examples/usage/usage_configuration_20.py index fa29494f..92bf3c01 100644 --- a/docs/examples/usage/usage_configuration_20.py +++ b/docs/examples/usage/usage_configuration_20.py @@ -1,6 +1,6 @@ """Test configuration example: Named database bindings.""" -__all__ = ("test_named_bindings", ) +__all__ = ("test_named_bindings",) def test_named_bindings() -> None: diff --git a/docs/examples/usage/usage_configuration_21.py b/docs/examples/usage/usage_configuration_21.py index c4ea71fe..e625d437 100644 --- a/docs/examples/usage/usage_configuration_21.py +++ b/docs/examples/usage/usage_configuration_21.py @@ -2,7 +2,7 @@ import pytest -__all__ = ("test_basic_migration_config", ) +__all__ = ("test_basic_migration_config",) @pytest.mark.skipif( diff --git a/docs/examples/usage/usage_configuration_22.py b/docs/examples/usage/usage_configuration_22.py index 1300a372..d6986916 100644 --- a/docs/examples/usage/usage_configuration_22.py +++ b/docs/examples/usage/usage_configuration_22.py @@ -1,7 +1,4 @@ def test_basic_migration_config() -> None: -__all__ = ("test_basic_migration_config", ) - - import os from sqlspec.adapters.asyncpg import AsyncpgConfig diff --git a/docs/examples/usage/usage_configuration_23.py b/docs/examples/usage/usage_configuration_23.py index 327e2f79..8857da9f 100644 --- a/docs/examples/usage/usage_configuration_23.py +++ b/docs/examples/usage/usage_configuration_23.py @@ -1,6 +1,6 @@ """Test configuration example: Environment-based configuration.""" -__all__ = ("test_extension_config", ) +__all__ = ("test_extension_config",) def test_extension_config() -> None: diff --git a/docs/examples/usage/usage_configuration_24.py b/docs/examples/usage/usage_configuration_24.py index d0b84c37..4fad09b2 100644 --- a/docs/examples/usage/usage_configuration_24.py +++ b/docs/examples/usage/usage_configuration_24.py @@ -3,7 +3,7 @@ import os from unittest.mock import patch -__all__ = ("test_environment_based_configuration", ) +__all__ = ("test_environment_based_configuration",) POSTGRES_PORT = 5433 diff --git a/docs/examples/usage/usage_configuration_25.py b/docs/examples/usage/usage_configuration_25.py index 94afe85d..5244efbd 100644 --- a/docs/examples/usage/usage_configuration_25.py +++ b/docs/examples/usage/usage_configuration_25.py @@ -2,7 +2,7 @@ import pytest -__all__ = ("test_connection_pooling_best_practice", ) +__all__ = ("test_connection_pooling_best_practice",) MIN_POOL_SIZE = 10 diff --git a/docs/examples/usage/usage_configuration_26.py b/docs/examples/usage/usage_configuration_26.py index af1efe3d..8e9fd2fe 100644 --- a/docs/examples/usage/usage_configuration_26.py +++ b/docs/examples/usage/usage_configuration_26.py @@ -1,6 +1,6 @@ """Test configuration example: Best practice - Enable caching.""" -__all__ = ("test_enable_caching_best_practice", ) +__all__ = ("test_enable_caching_best_practice",) def test_enable_caching_best_practice() -> None: diff --git a/docs/examples/usage/usage_configuration_27.py b/docs/examples/usage/usage_configuration_27.py index b7a6389e..12d4b58d 100644 --- a/docs/examples/usage/usage_configuration_27.py +++ b/docs/examples/usage/usage_configuration_27.py @@ -1,6 +1,6 @@ """Test configuration example: Best practice - Tune pool sizes.""" -__all__ = ("test_tune_pool_sizes_best_practice", ) +__all__ = ("test_tune_pool_sizes_best_practice",) MIN_POOL_SIZE_CPU = 5 diff --git a/docs/examples/usage/usage_configuration_28.py b/docs/examples/usage/usage_configuration_28.py index 796de7c9..7c8a4607 100644 --- a/docs/examples/usage/usage_configuration_28.py +++ b/docs/examples/usage/usage_configuration_28.py @@ -1,6 +1,6 @@ """Test configuration example: Best practice - Tune pool sizes.""" -__all__ = ("test_disable_security_checks_best_practice", ) +__all__ = ("test_disable_security_checks_best_practice",) def test_disable_security_checks_best_practice() -> None: diff --git a/docs/examples/usage/usage_configuration_29.py b/docs/examples/usage/usage_configuration_29.py index 0dada143..db042b4f 100644 --- a/docs/examples/usage/usage_configuration_29.py +++ b/docs/examples/usage/usage_configuration_29.py @@ -2,7 +2,7 @@ import pytest -__all__ = ("test_cleanup_resources_best_practice", ) +__all__ = ("test_cleanup_resources_best_practice",) @pytest.mark.asyncio diff --git a/docs/examples/usage/usage_configuration_3.py b/docs/examples/usage/usage_configuration_3.py index 7fad9375..e53b38e3 100644 --- a/docs/examples/usage/usage_configuration_3.py +++ b/docs/examples/usage/usage_configuration_3.py @@ -1,7 +1,4 @@ def test_memory_databases() -> None: -__all__ = ("test_memory_databases", ) - - from sqlspec.adapters.sqlite import SqliteConfig # In-memory database (isolated per connection) diff --git a/docs/examples/usage/usage_configuration_30.py b/docs/examples/usage/usage_configuration_30.py index 4ed177b2..8a78596d 100644 --- a/docs/examples/usage/usage_configuration_30.py +++ b/docs/examples/usage/usage_configuration_30.py @@ -1,6 +1,6 @@ """Telemetry snapshot example.""" -__all__ = ("test_telemetry_snapshot", ) +__all__ = ("test_telemetry_snapshot",) def test_telemetry_snapshot() -> None: diff --git a/docs/examples/usage/usage_configuration_4.py b/docs/examples/usage/usage_configuration_4.py index 99c2dc55..7f3c8c5e 100644 --- a/docs/examples/usage/usage_configuration_4.py +++ b/docs/examples/usage/usage_configuration_4.py @@ -1,7 +1,4 @@ def test_asyncpg_config_setup() -> None: -__all__ = ("test_asyncpg_config_setup", ) - - import os from sqlspec.adapters.asyncpg import AsyncpgConfig diff --git a/docs/examples/usage/usage_configuration_5.py b/docs/examples/usage/usage_configuration_5.py index 3ad67c3e..9cd3377e 100644 --- a/docs/examples/usage/usage_configuration_5.py +++ b/docs/examples/usage/usage_configuration_5.py @@ -1,7 +1,4 @@ def test_psycopg_config_setup() -> None: -__all__ = ("test_psycopg_config_setup", ) - - import os from sqlspec.adapters.psycopg import PsycopgAsyncConfig diff --git a/docs/examples/usage/usage_configuration_6.py b/docs/examples/usage/usage_configuration_6.py index f73f089a..13251e26 100644 --- a/docs/examples/usage/usage_configuration_6.py +++ b/docs/examples/usage/usage_configuration_6.py @@ -1,6 +1,5 @@ MYSQL_PORT = 3306 -__all__ = ("test_asyncmy_config_setup", ) - +__all__ = ("test_asyncmy_config_setup",) def test_asyncmy_config_setup() -> None: diff --git a/docs/examples/usage/usage_configuration_7.py b/docs/examples/usage/usage_configuration_7.py index 7083b5da..3152d6ed 100644 --- a/docs/examples/usage/usage_configuration_7.py +++ b/docs/examples/usage/usage_configuration_7.py @@ -1,7 +1,4 @@ def test_duckdb_config_setup() -> None: -__all__ = ("test_duckdb_config_setup", ) - - from sqlspec.adapters.duckdb import DuckDBConfig in_memory_config = DuckDBConfig() diff --git a/docs/examples/usage/usage_configuration_8.py b/docs/examples/usage/usage_configuration_8.py index 7862451c..52524219 100644 --- a/docs/examples/usage/usage_configuration_8.py +++ b/docs/examples/usage/usage_configuration_8.py @@ -1,6 +1,5 @@ MIN_POOL_SIZE = 10 -__all__ = ("test_asyncpg_pool_setup", ) - +__all__ = ("test_asyncpg_pool_setup",) def test_asyncpg_pool_setup() -> None: diff --git a/docs/examples/usage/usage_configuration_9.py b/docs/examples/usage/usage_configuration_9.py index 553fb83e..0e254495 100644 --- a/docs/examples/usage/usage_configuration_9.py +++ b/docs/examples/usage/usage_configuration_9.py @@ -1,7 +1,4 @@ def test_pool_lifecycle() -> None: -__all__ = ("test_pool_lifecycle", ) - - import os from sqlspec import SQLSpec From 93250d45e7e82387e4b427993653666f57c1eada Mon Sep 17 00:00:00 2001 From: Cody Fincher Date: Mon, 10 Nov 2025 15:49:59 +0000 Subject: [PATCH 21/21] chore: update package versions for pydantic-settings, shibuya, and sphinx-docsearch --- uv.lock | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/uv.lock b/uv.lock index 56ed2a62..ae7fbacb 100644 --- a/uv.lock +++ b/uv.lock @@ -4438,16 +4438,16 @@ wheels = [ [[package]] name = "pydantic-settings" -version = "2.11.0" +version = "2.12.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "pydantic" }, { name = "python-dotenv" }, { name = "typing-inspection" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/20/c5/dbbc27b814c71676593d1c3f718e6cd7d4f00652cefa24b75f7aa3efb25e/pydantic_settings-2.11.0.tar.gz", hash = "sha256:d0e87a1c7d33593beb7194adb8470fc426e95ba02af83a0f23474a04c9a08180", size = 188394, upload-time = "2025-09-24T14:19:11.764Z" } +sdist = { url = "https://files.pythonhosted.org/packages/43/4b/ac7e0aae12027748076d72a8764ff1c9d82ca75a7a52622e67ed3f765c54/pydantic_settings-2.12.0.tar.gz", hash = "sha256:005538ef951e3c2a68e1c08b292b5f2e71490def8589d4221b95dab00dafcfd0", size = 194184, upload-time = "2025-11-10T14:25:47.013Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/83/d6/887a1ff844e64aa823fb4905978d882a633cfe295c32eacad582b78a7d8b/pydantic_settings-2.11.0-py3-none-any.whl", hash = "sha256:fe2cea3413b9530d10f3a5875adffb17ada5c1e1bab0b2885546d7310415207c", size = 48608, upload-time = "2025-09-24T14:19:10.015Z" }, + { url = "https://files.pythonhosted.org/packages/c1/60/5d4751ba3f4a40a6891f24eec885f51afd78d208498268c734e256fb13c4/pydantic_settings-2.12.0-py3-none-any.whl", hash = "sha256:fddb9fd99a5b18da837b29710391e945b1e30c135477f484084ee513adb93809", size = 51880, upload-time = "2025-11-10T14:25:45.546Z" }, ] [[package]] @@ -5264,16 +5264,16 @@ wheels = [ [[package]] name = "shibuya" -version = "2025.11.4" +version = "2025.11.10" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "pygments-styles" }, { name = "sphinx", version = "8.1.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" }, { name = "sphinx", version = "8.2.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/fe/d5/d3e76a4cc05801106899a40e2103142cf3a98f480aa92e5a5ab430784115/shibuya-2025.11.4.tar.gz", hash = "sha256:8cae36c339c6c2d4d93ad1bb7241c4cf2549f192a43364de3d5e721955ca3d20", size = 82227, upload-time = "2025-11-04T06:22:44.193Z" } +sdist = { url = "https://files.pythonhosted.org/packages/cf/e3/53fe8d84008aa0f0baea769973a3e8baa2fe2895e6994ba94767ba52bb14/shibuya-2025.11.10.tar.gz", hash = "sha256:7a8a2b4ac6c3fb8d2f75a15098458e376f5af33cfff45b21e5816e41dfcc5dd3", size = 82201, upload-time = "2025-11-10T09:13:54.308Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/ee/29/28fe85dded5f52a1a9e559c676c5397433c17d491590c755eb37c5716269/shibuya-2025.11.4-py3-none-any.whl", hash = "sha256:ef4d4b96b817f4d46b5969a5960d14fc163978478eb5a4f66fc2aa5b0fae3960", size = 98128, upload-time = "2025-11-04T06:22:42.509Z" }, + { url = "https://files.pythonhosted.org/packages/3c/ca/1f713c8cbf777e9b2716a2c19a90e2c3fb91a111399dc05ec0b8082a8312/shibuya-2025.11.10-py3-none-any.whl", hash = "sha256:7d9116bcd14977e689c6048232bb42d739832c0dd861dc7be34dfb183edb511e", size = 98175, upload-time = "2025-11-10T09:13:52.612Z" }, ] [[package]] @@ -5539,15 +5539,15 @@ wheels = [ [[package]] name = "sphinx-docsearch" -version = "0.1.0" +version = "0.2.0" source = { registry = "https://pypi.org/simple" } dependencies = [ { name = "sphinx", version = "8.1.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" }, { name = "sphinx", version = "8.2.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" }, ] -sdist = { url = "https://files.pythonhosted.org/packages/7c/3c/52eb72d3e837f269aed90b0ef1b4f7dd437d26cc6812209f839c91053d8f/sphinx_docsearch-0.1.0.tar.gz", hash = "sha256:76fd0695e115b76ebb9633acffc263bfd894caad7aa29e3c40bfe3171da41535", size = 40008, upload-time = "2024-10-04T12:04:40.757Z" } +sdist = { url = "https://files.pythonhosted.org/packages/7b/3e/32849cb1154f936cfa387a29207a552162c8d6da9dfceb0aa1cfd8bbb04e/sphinx_docsearch-0.2.0.tar.gz", hash = "sha256:a96c97649453567c89ce2b37d714c10c4e829311e260e4b4d9ea980385aab117", size = 238214, upload-time = "2025-11-10T10:22:26.404Z" } wheels = [ - { url = "https://files.pythonhosted.org/packages/2b/44/4e59ba1820e8190d1cb3bceb491d875da5f79fb3f61f5c2fe82037c3546e/sphinx_docsearch-0.1.0-py3-none-any.whl", hash = "sha256:799221b0b962e3d86d0e0f084d4998c3d9227ef0eb2883d70e41d6bd08b616dd", size = 41852, upload-time = "2024-10-04T12:04:38.933Z" }, + { url = "https://files.pythonhosted.org/packages/7b/53/d5a027db36576f515411353cf6be8b2b2aa74a65d520c706dab2cc2fc3d8/sphinx_docsearch-0.2.0-py3-none-any.whl", hash = "sha256:60e6062c2e4eb0a7320fec0ff6f014cba37f3b51c74f6a6f6a9212262f2fca2f", size = 140882, upload-time = "2025-11-10T10:22:23.218Z" }, ] [[package]]