docs(python): add deeper type system coverage to best practices#2
Open
agent-kurouto[bot] wants to merge 1 commit intodevfrom
Open
Conversation
Expands Section 2 (Type Hints & Static Analysis) with 9 new subsections covering advanced Python type features: - TypedDict for typed dictionary structures - Literal types for value-constrained parameters - NewType for domain-modelled primitives - Final and ClassVar for immutability and class-level annotations - @overload for multiple call signatures - TypeGuard and type narrowing (including assert_never) - Self type for fluent interfaces and subclass-safe returns - TYPE_CHECKING guard for avoiding circular imports - Annotated for attaching metadata to types (Pydantic v2, FastAPI) Also adds relevant PEPs to the Further Reading section. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Addresses PLT-1013 — expands Section 2 (Type Hints & Static Analysis) with 9 new subsections that dive deeper into Python's type system:
TypedDict— typed dict structures with optional keys; comparison table vs dataclasses/Pydantic/NamedTupleLiteral— value-constrained types for function parameters; exhaustive narrowing with PyrightNewType— zero-runtime-cost domain primitives to prevent mixing semantically different IDs/valuesFinalandClassVar— immutability annotations for constants, frozen instance attributes, and class-level vs instance-level fields@overload— multiple call signatures when return type varies by argument type; rules and anti-patternsTypeGuardand type narrowing — custom predicates, built-inisinstancenarrowing, andassert_neverfor exhaustive matchingSelf— subclass-safe returns for fluent/builder patterns and classmethods; replacesTypeVar(bound=...)boilerplateTYPE_CHECKINGguard — avoiding circular imports by deferring type-only imports; paired withfrom __future__ import annotationsAnnotated— attaching validation/documentation metadata to types; Pydantic v2 and FastAPI integration examplesAlso adds 8 new PEP references to the Further Reading section (PEP 544, 586, 589, 591, 647, 673, 593, and the mypy type narrowing docs).
Test plan
🤖 Generated with Claude Code