Skip to content

docs(python): add deeper type system coverage to best practices#2

Open
agent-kurouto[bot] wants to merge 1 commit intodevfrom
eywalker/plt-1013-add-more-content-to-python-best-practices-on-nauticalab
Open

docs(python): add deeper type system coverage to best practices#2
agent-kurouto[bot] wants to merge 1 commit intodevfrom
eywalker/plt-1013-add-more-content-to-python-best-practices-on-nauticalab

Conversation

@agent-kurouto
Copy link
Contributor

@agent-kurouto agent-kurouto bot commented Mar 19, 2026

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/NamedTuple
  • Literal — value-constrained types for function parameters; exhaustive narrowing with Pyright
  • NewType — zero-runtime-cost domain primitives to prevent mixing semantically different IDs/values
  • Final and ClassVar — 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-patterns
  • TypeGuard and type narrowing — custom predicates, built-in isinstance narrowing, and assert_never for exhaustive matching
  • Self — subclass-safe returns for fluent/builder patterns and classmethods; replaces TypeVar(bound=...) boilerplate
  • TYPE_CHECKING guard — avoiding circular imports by deferring type-only imports; paired with from __future__ import annotations
  • Annotated — attaching validation/documentation metadata to types; Pydantic v2 and FastAPI integration examples

Also 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

  • Read through all new subsections for accuracy and consistency with existing document style
  • Verify code examples are syntactically correct Python
  • Check that "Good" / "Avoid" / tip patterns match the rest of the document
  • Confirm no overlap or contradiction with existing sections

🤖 Generated with Claude Code

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>
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.

0 participants