Skip to main content

Pydantic Lax Mode Type Conversions

This page provides a complete reference for how Pyrefly converts types when working with Pydantic models in lax mode (the default). For background on how lax mode works, see the main Pydantic documentation.

Note: Types without a specific conversion rule (e.g., Callable, Any, custom classes) are converted to Any.


Atomic Type Conversions

Named unions are used for atomic types to keep type signatures concise.

Input TypeNamed UnionExpanded Type
intLaxIntint | bool | float | str | bytes | Decimal
floatLaxFloatint | bool | float | str | bytes | Decimal
boolLaxBoolbool | int | float | str | Decimal
DecimalLaxDecimalDecimal | int | float | str
strLaxStrstr | bytes | bytearray
bytesLaxBytesstr | bytes | bytearray
dateLaxDatedate | datetime | int | float | str | bytes | Decimal
datetimeLaxDatetimedate | datetime | int | float | str | bytes | Decimal
timeLaxTimetime | int | float | str | bytes | Decimal
timedeltaLaxTimedeltatimedelta | int | float | str | bytes | Decimal
PathLaxPathPath | str
UUIDLaxUuidUUID | str
None(no conversion)None

Compositional Type Conversions

Notation:

  • T_converted means the type T is recursively converted using lax mode rules (e.g., intLaxInt)
  • T_flattened means the type T is converted and expanded (e.g., intint | bool | float | str | bytes | Decimal)
Input Type/ContainerOutput Type/Container
type[T]type[T_converted]
T1 | T2 | ...T1_flattened | T2_flattened | ...
list[T], set[T], frozenset[T],
Sequence[T], Iterable[T], deque[T],
tuple[T, ...]
Iterable[T_converted]
tuple[T1, T2, ...]Iterable[T1_flattened | T2_flattened | ...]
dict[K, V]Mapping[K, V_converted]

Examples:

  • Type wrapper: type[int]type[LaxInt]
  • Union types: Each member is converted and flattened. int | boolint | bool | float | str | bytes | Decimal
  • Single-element containers and unbounded tuples: Named unions are preserved. list[int]Iterable[LaxInt]
  • Concrete tuples: Element types are expanded and flattened. tuple[int, str]Iterable[int | bool | float | str | bytes | bytearray | Decimal]
  • Dictionaries: Only values are converted; keys remain unchanged. dict[str, int]Mapping[str, LaxInt]