new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · all subjects

python types

10 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Python type hints syntax and basic usage

Python type hints use colon syntax to declare variable types. For example, a function parameter `first_name: str` declares that the parameter must be a string. Type hints are optional annotations that do not change program execution but enable editor support, type checking, and code analysis. Type hints are declared using a colon (:), which is different from default values that use equals (=).

Simple Python types supported

Python supports type hints for simple built-in types including: int, float, bool, str, and bytes. These can be used directly as type annotations in function parameters and variable declarations.

Generic types with type parameters in square brackets

Generic types can take type parameters in square brackets to specify internal types. For example: list[str] declares a list of strings, tuple[int, int, str] declares a tuple with specific element types, set[bytes] declares a set of bytes, and dict[str, float] declares a dictionary with string keys and float values.

Union types allow multiple possible types

A variable can be declared to accept multiple types using the vertical bar (|) operator. For example, int | str means a value can be either an int or a str. This is called a union type.

Optional types with None

A variable can be declared to accept a type or None using the union operator. For example, str | None declares that a value can be either a string or None. This allows editors to detect errors where code assumes a value is always a string when it could actually be None.

Classes as type annotations

A class can be used as a type annotation. For example, declaring a variable as `one_person: Person` means one_person is an instance of the Person class, not the class itself. This enables full editor support for the instance's attributes and methods.

Pydantic for data validation

Pydantic is a Python library for data validation. You declare the shape of data as classes with typed attributes. Creating an instance of a Pydantic model will validate values, convert them to appropriate types if needed, and provide an object with all the data. FastAPI is built on Pydantic.

Annotated for type hints with metadata

The Annotated type from the typing module allows adding metadata to type hints. Syntax: Annotated[type, metadata]. The first type parameter is the actual type; the rest is metadata for tools. Annotated is standard Python and does not affect editor support. FastAPI uses Annotated metadata to define additional behavior for parameters.

typing module for additional types

The typing module provides additional types like Any for when something can be any type. Import with: from typing import Any. Use Any as a type annotation when a parameter or variable should accept any type.

Python **user_dict unpacking syntax

UserInDB(**user_dict) unpacks dictionary keys and values as keyword arguments. This is equivalent to UserInDB(username=user_dict['username'], email=user_dict['email'], ...).

Give your agent this brain