Skip to content

fix compatibility for python 3.10#258

Merged
phenobarbital merged 2 commits intomainfrom
new-exports
Jan 27, 2026
Merged

fix compatibility for python 3.10#258
phenobarbital merged 2 commits intomainfrom
new-exports

Conversation

@phenobarbital
Copy link
Copy Markdown
Owner

@phenobarbital phenobarbital commented Jan 27, 2026

Summary by Sourcery

Ensure compatibility with Python 3.10 while improving model serialization behavior and bumping the library version.

Bug Fixes:

  • Handle EnumType import for Python 3.10+ by falling back to EnumMeta when EnumType is unavailable.
  • Prevent internal and excluded fields, including the _pgoutput attribute, from being included in collapsed value dictionaries.

Enhancements:

  • Extend model serialization helpers to accept an exclude set for more flexible field exclusion.
  • Update the library version metadata to reflect the latest release.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Jan 27, 2026

Reviewer's Guide

Updates datamodel serialization to better control excluded fields and restores enum compatibility for Python 3.10+, while bumping the library version.

Class diagram for updated datamodel BaseModel serialization methods

classDiagram
    class BaseModel {
        +to_dict(remove_nulls bool = False, convert_enums bool = False, as_values bool = False, exclude set = None) dict~str, Any~
        +__collapse_as_values__(remove_nulls bool = False, convert_enums bool = False, as_values bool = False, exclude set = None) dict~str, Any~
        +columns() dict~str, Field~
    }

    class Field {
        +metadata dict~str, Any~
    }

    BaseModel "*" --> "*" Field : uses
Loading

Flow diagram for updated to_dict and collapse_as_values exclusion logic

flowchart TD
    A[to_dict called] --> B[Initialize exclude set or use provided]
    B --> C[Add _pgoutput to exclude]
    C --> D{as_values?}
    D -- Yes --> E[Call __collapse_as_values__ with remove_nulls, convert_enums, as_values, exclude]
    D -- No --> F[Call as_dict to build dict]
    F --> G{convert_enums?}
    G -- Yes --> H[Convert enum values in dict]
    G -- No --> I[Skip enum conversion]
    H --> J[Optionally remove nulls]
    I --> J[Optionally remove nulls]
    J --> K[Return dict]

    subgraph CollapseValues[__collapse_as_values__]
        L[Iterate over columns] --> M{field name in exclude?}
        M -- Yes --> L
        M -- No --> N{field name starts with __ or equals __errors__?}
        N -- Yes --> L
        N -- No --> O{field.metadata.exclude is True?}
        O -- Yes --> L
        O -- No --> P[Get attribute value]
        P --> Q[Apply remove_nulls / convert_enums / collapse logic]
        Q --> R[Store in output dict]
        R --> L
    end
Loading

File-Level Changes

Change Details Files
Extend BaseModel.to_dict/collapse_as_values to support field exclusion and always omit internal attributes during value collapsing.
  • Add an optional exclude set parameter to to_dict and collapse_as_values and ensure _pgoutput is always excluded.
  • Propagate the exclude set into collapse_as_values when serializing as values.
  • Skip fields in value collapsing when their name is in the exclude set, when they are internal/error fields, or when field metadata requests exclusion.
datamodel/models.py
Adjust enum imports in JSON parser for compatibility with Python 3.10+ enum API changes.
  • Change direct EnumType import to a try/except that falls back to EnumMeta when EnumType is unavailable.
  • Leave ORJSON_DEFAULT_OPTIONS unchanged aside from removing a commented-out option.
datamodel/parsers/json.pyx
Bump library version to reflect compatibility and serialization changes.
  • Increment version from 0.10.17 to 0.10.19.
datamodel/version.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@phenobarbital phenobarbital merged commit aa9e8ad into main Jan 27, 2026
2 checks passed
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The exclude = exclude or set(); exclude.add('_pgoutput') pattern in to_dict mutates a caller-provided exclude set in-place, which may be surprising; consider copying it before modification if you intend exclude to be local to this call.
  • The new exclude handling is only applied in the __collapse_as_values__ branch of to_dict, so _pgoutput and other excluded fields will still appear when as_values=False; if the intent is to always exclude these fields, the non-as_values path should apply the same logic.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `exclude = exclude or set(); exclude.add('_pgoutput')` pattern in `to_dict` mutates a caller-provided `exclude` set in-place, which may be surprising; consider copying it before modification if you intend `exclude` to be local to this call.
- The new `exclude` handling is only applied in the `__collapse_as_values__` branch of `to_dict`, so `_pgoutput` and other excluded fields will still appear when `as_values=False`; if the intent is to always exclude these fields, the non-`as_values` path should apply the same logic.

## Individual Comments

### Comment 1
<location> `datamodel/models.py:214` </location>
<code_context>
         convert_enums: bool = False,
-        as_values: bool = False
+        as_values: bool = False,
+        exclude: Optional[set] = None
     ) -> dict[str, Any]:
+        exclude = exclude or set()
</code_context>

<issue_to_address>
**issue (bug_risk):** Avoid mutating the caller-provided `exclude` set inside `to_dict`.

Since `exclude` comes from the caller, `exclude = exclude or set(); exclude.add('_pgoutput')` will modify the caller’s set. If the same set is reused, it will unexpectedly keep accumulating entries like `_pgoutput`. Instead, copy it first, e.g. `exclude_local = set(exclude or ())`, then add `'_pgoutput'` to the copy so the function doesn’t mutate caller-provided data.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread datamodel/models.py
convert_enums: bool = False,
as_values: bool = False
as_values: bool = False,
exclude: Optional[set] = None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Avoid mutating the caller-provided exclude set inside to_dict.

Since exclude comes from the caller, exclude = exclude or set(); exclude.add('_pgoutput') will modify the caller’s set. If the same set is reused, it will unexpectedly keep accumulating entries like _pgoutput. Instead, copy it first, e.g. exclude_local = set(exclude or ()), then add '_pgoutput' to the copy so the function doesn’t mutate caller-provided data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant