marktacular/vf-python-type-annotations icon
public
Published on 6/4/2025
Python Code Type Annotations

Rules

When writing Python code with type annotations:

  1. Use Any from the typing module instead of implementation-specific types like csv.writer
  2. Create custom TypedDict classes to define structure of dictionary data
  3. Use cast() from typing module to properly annotate values from external sources or when changing type
  4. Add null/None checks before accessing potentially None values
  5. Be explicit about return types and ensure all return paths match the declared type
  6. For dictionary values with complex structures, define custom types rather than using nested Dict[str, Any]
  7. For collections of objects with defined structures, use specific types rather than generic Dict or List
  8. Be careful with Optional types - only use cast to remove Optional when you've verified value is not None
  9. Add proper type annotations for function parameters and return values
  10. When using TypeVars, define them at the module level with clear constraints