Skip to content

Conversation

@cofin
Copy link
Member

@cofin cofin commented Oct 11, 2025

Summary

Removes the redundant total=False parameter from all TypedDict class definitions throughout the codebase. When total=False is used with NotRequired[...] wrappers on all fields, it creates unnecessary redundancy and confusion.

Problem

The anti-pattern was pervasive across the entire codebase:

# BEFORE (redundant and confusing)
class MyConfig(TypedDict, total=False):
    field1: NotRequired[str]  # Already optional due to total=False
    field2: NotRequired[int]  # NotRequired is redundant here

When total=False is specified, all fields are implicitly optional, making the NotRequired[...] wrapper redundant. This pattern was:

  • Confusing to readers
  • Against Python typing best practices
  • Inconsistent with the purpose of NotRequired

Solution

Use the correct TypedDict pattern where total=True (the default) with explicit NotRequired[...] for optional fields:

# AFTER (correct and clear)
class MyConfig(TypedDict):
    field1: NotRequired[str]  # Explicitly optional
    field2: NotRequired[int]  # Clear intent

Changes Made

Python Files (15 files, 30+ TypedDict classes)

  • sqlspec/config.py - LifecycleConfig, MigrationConfig, LitestarConfig
  • sqlspec/adapters/adbc/config.py - AdbcConnectionParams, AdbcDriverFeatures
  • sqlspec/adapters/aiosqlite/config.py - AiosqliteConnectionParams, AiosqlitePoolParams, AiosqliteDriverFeatures
  • sqlspec/adapters/asyncmy/config.py - AsyncmyConnectionParams, AsyncmyPoolParams, AsyncmyDriverFeatures
  • sqlspec/adapters/asyncpg/config.py - AsyncpgConnectionConfig, AsyncpgPoolConfig, AsyncpgDriverFeatures
  • sqlspec/adapters/bigquery/config.py - BigQueryConnectionParams, BigQueryDriverFeatures
  • sqlspec/adapters/duckdb/config.py - DuckDBConnectionParams, DuckDBExtensionConfig, DuckDBSecretConfig, DuckDBDriverFeatures
  • sqlspec/adapters/oracledb/config.py - OracleConnectionParams, OraclePoolParams, OracleDriverFeatures
  • sqlspec/adapters/psqlpy/config.py - PsqlpyConnectionParams, PsqlpyPoolParams, PsqlpyDriverFeatures
  • sqlspec/adapters/psycopg/config.py - PsycopgConnectionParams, PsycopgPoolParams, PsycopgDriverFeatures
  • sqlspec/adapters/sqlite/config.py - SqliteConnectionParams, SqliteDriverFeatures
  • sqlspec/adapters/sqlite/pool.py - SqliteConnectionParams
  • sqlspec/extensions/adk/config.py - ADKConfig
  • sqlspec/extensions/litestar/config.py - LitestarConfig

Documentation (1 file, 8 examples)

  • AGENTS.md - Updated all TypedDict examples in driver_features pattern documentation

Benefits

Clarity: Fields are explicitly marked as optional with NotRequired
Consistency: One canonical way to define optional fields
Best Practices: Aligns with Python typing standards
IDE Support: Better type checking and autocomplete
Maintainability: Less confusion for contributors

Testing

  • make fix - All linting passes
  • pyright - 0 errors, 0 warnings
  • ✅ No functional changes to type behavior
  • ✅ All NotRequired[...] wrappers preserved exactly

Impact

No breaking changes - This is purely a refactor of type definitions. The runtime behavior and type checking behavior remain identical.

References

Remove the redundant `total=False` parameter from all TypedDict class
definitions throughout the codebase. When `total=False` is used, all
fields are implicitly optional, making the `NotRequired[...]` wrapper
redundant and confusing.

The correct pattern is to use `TypedDict` (which defaults to total=True)
with explicit `NotRequired[...]` wrappers on optional fields. This provides:
- Clear intent: Fields explicitly marked as optional
- Better IDE support: Type checkers understand NotRequired
- No redundancy: One way to mark fields as optional
- Consistent pattern: Aligns with Python typing best practices

Changes:
- Removed `, total=False` from 30+ TypedDict class definitions
- Updated all examples in AGENTS.md documentation
- Kept all `NotRequired[...]` wrappers intact
- No functional changes to type behavior

Files affected:
- sqlspec/config.py (3 TypedDicts)
- sqlspec/adapters/*/config.py (13 adapters, 30+ TypedDicts)
- sqlspec/extensions/*/config.py (2 extensions, 2 TypedDicts)
- sqlspec/adapters/sqlite/pool.py (1 TypedDict)
- AGENTS.md (8 documentation examples)

All linting and type checking passes:
- make fix: ✓
- pyright: 0 errors, 0 warnings

Fixes the TypedDict anti-pattern identified across the codebase.
@cofin cofin merged commit cc2dcb3 into main Oct 11, 2025
10 checks passed
@cofin cofin deleted the fix/typeddict-notrequired-antipattern branch October 11, 2025 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants