This document outlines the Python coding standards and conventions used across Function.

General Principles

  • Readability: Code should be clear and self-documenting.
  • Consistency: Follow established patterns throughout the codebase.
  • Type Safety: Use comprehensive type annotations.
  • Modularity: Keep functions and classes focused on single responsibilities.
  • Performance: Consider efficiency in compiler-related code.

File Structure and Organization

This section covers how to structure Python files, organize imports, and arrange code within modules to maintain clarity and consistency.

Imports

We use specific imports rather than wildcards, always include from __future__ import annotations when needed, and organize imports in four distinct groups with alphabetical ordering within each group. Related imports are grouped together, and we prefer absolute imports for clarity.

Naming Conventions

We use snake_case for variables and functions, PascalCase for classes, UPPER_SNAKE_CASE for constants, and single leading underscores for private members. Names should be descriptive and avoid abbreviations unless they’re well-known in the domain.

Function and Class Definitions

We requrie comprehensive type annotations on functions and classes. We use keyword-only arguments for optional parameters, and group logically.

Type Annotations

We use comprehensive type annotations throughout the codebase. All function parameters, return types, and class attributes must be annotated.

Comments and Docstrings

We encourage the use of comments and docstrings to provide context around code.

Code Formatting

We write highly ergonomic code that is easy to both look at and reason about:

Error Handling

We encourage defining a small set of custom exception types that can be explicitly handled:

Testing

Tests follow the Arrange-Act-Assert pattern with comments, and are organized in a test/ directory that mirrors the source code structure.

Dependencies

We prefer well-maintained packages with pinned versions for critical dependencies, use optional import patterns with try/except blocks for non-critical features, and favor relative imports for internal modules.