Skip to content

optimize CheckedSession.__setattr__ or ._check_key_value #1171

Description

@gdementen

It is currently much more expensive than necessary (it recomputes annotations and TypeAdapter on each attribute set, whether that particular attribute has already been set or not). This is not super important though given that we use it to store arrays which themselves take much more time to compute than the attribute setter. Still, in some rare workflows, I think this could make a difference. Edit: Note that even nested loops setting one element of an array at a time should not be impacted because those are using __setitem__ on the arrays themselves, not __setattr__ or __setitem__ on the session, so this would be even rarer than I initially thought.

It should be possible to cache setters (like Pydantic itself does BTW). I have actually tried doing so (yet another instance of a "how hard could it be?" 😉) and it mostly works... except inheritance 😢.

Activity

  1. self-assigned this
    on Mar 13, 2026
  2. added a commit that references this issue on Mar 13, 2026
    6b90fe5
  3. added this to the 0.36 milestone on Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions