Skip to main content

pydantic-linq

C# LINQ (System.Linq.Enumerable) for Python. It is typed and works with Pydantic.

from pydantic import BaseModel
import pydantic_linq  # adds LINQ operators to list / tuple / set / frozenset

class Row(BaseModel):
    id: int
    tag: str

rows = [Row(id=1, tag="a"), Row(id=2, tag="b"), Row(id=3, tag="a")]

rows.where(lambda r: r.tag == "a").select(lambda r: r.id).to_list()   # [1, 3]
rows.group_by(lambda r: r.tag).select(lambda g: (g.key, len(g))).to_list()
rows.order_by_descending(lambda r: r.id).then_by(lambda r: r.tag).first()

Async

AsyncEnumerable covers System.Linq.Async. It wraps any sync or async iterable. Sequence operators are deferred, and terminal operators are awaited:

async def load_age(r: Row) -> int: ...

ages = await rows.to_async_enumerable().where(lambda r: r.id > 1).select(load_age).to_list()

A selector or predicate can be a plain function or an async def, so there are no separate *Await overloads. Terminal operators keep the sync names (to_list, first, count) and are awaited. The C# Async suffix is dropped.

Conventions

  • Method names are the C# names in snake_case: OrderByDescending becomes order_by_descending. Except becomes except_, because except is a Python keyword.
  • Operators that return a sequence are deferred and can be enumerated again, as in .NET.
  • Equality uses value semantics (==). Non-frozen (unhashable) pydantic models work in distinct, union and the other set operators.
  • cast(Model) validates each element with a pydantic TypeAdapter, so dicts become models. of_type(Model) filters with isinstance.
  • Enumerable[Model] can be a pydantic field type. It validates like list[Model] and serializes as a list.
  • C# KeyValuePair becomes a (key, value) tuple. IComparer becomes a cmp(a, b) -> int function. A C# Range or Index (for example ^1) becomes a slice or a negative int.
  • Lambda overloads that take an index (Select((x, i) => ...)) map to .index(), followed by the operator.
  • Empty-sequence errors raise InvalidOperationError, which subclasses ValueError.

Builtin patching

Importing the package uses forbiddenfruit to patch list, tuple, set and frozenset. A builtin method that already exists keeps its Python meaning, for example list.count(x) and list.reverse(). To get the LINQ version, use rows.as_enumerable().count(). Type checkers do not see the patched methods. Where you need static types, use Enumerable(rows). To remove the patches, call pydantic_linq.uninstall().

Layout

src/pydantic_linq/
  core/           errors, value-equality set, Grouping (no internal deps)
  sync/
    base.py       EnumerableBase, deferred execution
    operators/    one mixin per LINQ category: filtering, projection, partitioning, sets, sorting, ...
    enumerable.py Enumerable = all mixins
    ordered.py    OrderedEnumerable (then_by)
    grouping.py   Grouping (an Enumerable with .key), Lookup
  aio/            AsyncEnumerable, same layout as sync/
  integrations/   pydantic field schema, builtin patching

Examples

examples/ runs from simple to complex: 01_basics through 07_sales_report, an end-to-end report pipeline. Run any of them with uv run python examples/01_basics.py.

Metadata

Release files for pydantic-linq 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-linq 0.1.0
File Size Uploaded Provenance Actions
pydantic_linq-0.1.0.tar.gz 41.5 kB With provenance Details for pydantic_linq-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-linq 0.1.0
File Interpreter ABI Platform Actions
pydantic_linq-0.1.0-py3-none-any.whl Python 3 none any Details for pydantic_linq-0.1.0-py3-none-any.whl

Total release size: 79.4 kB

Release files / pydantic_linq-0.1.0.tar.gz

Download URL pydantic_linq-0.1.0.tar.gz
Size 41.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f1a3125dbcf5c9f99f7bfd6de910ffce8376cc1b05e5d0387d9777a26cb934a0
BLAKE2b-256 checksum
How to use checksums
b378f9e2386ea0ac37f89ccbcef2eb8ad12b3f640fbc742442d731f55b5fd6bd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Release files / pydantic_linq-0.1.0-py3-none-any.whl

Download URL pydantic_linq-0.1.0-py3-none-any.whl
Size 37.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
91d57f3398997699cd56b199abeef5d90e0b4e5f4be7ba47f82c02cdfba650f0
BLAKE2b-256 checksum
How to use checksums
c005eae0dc2216a024cff5ecbb189590a1506833c71796ea55c4d92d23c4fe33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

Security for pydantic-linq 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pydantic_linq-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page