-
Notifications
You must be signed in to change notification settings - Fork 41
Keep an annotation value of 1 through serialization #912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🌐 Web query:
For Pydantic 2.1, does a wrap model serializer receive an emptySerializationInfo.includewhenmodel_dump(include=set())is called?💡 Result:
In Pydantic 2.1, when
model_dump(include=set())(or an empty set) is called, theSerializationInfo.includeproperty received by a wrap model serializer is typically empty or None, rather than containing the explicitly passed empty set [1][2]. Pydantic's serialization logic forincludeandexcludeparameters is designed to filter fields during the recursion of the model tree [1]. When an empty set is provided tomodel_dump, the serialization process interprets this as a request to include no fields. Consequently, when the serializer is invoked, theSerializationInfoobject reflects the active include rules for the current level of the model [1][3]. If no specific fields are selected for that level,info.includewill generally not contain the empty set literal passed to the top-level method, as the internal state has already processed or reduced the inclusion rules for that specific model instance [1]. Technical discussions indicate thatinfo.includeandinfo.excludein serializer contexts reflect the current sub-level's filtering requirements rather than the global, top-level arguments passed tomodel_dump[1]. If no further fields are to be included, this property is typically empty [1]. Users should be aware that passingincludeorexcludeparameters to handlers within wrap serializers can lead to unexpected behavior if not handled precisely, as these parameters are intended to filter nested structures during the serialization process [1][4].Citations:
includeandexcludeare not passed to field serializer contexts pydantic/pydantic#9538🏁 Script executed:
Repository: DASDAE/dascore
Length of output: 5477
🏁 Script executed:
Repository: DASDAE/dascore
Length of output: 18327
🌐 Web query:
Pydantic 2.1 SerializationInfo.include wrap model serializer model_dump(include=set()) exact behavior💡 Result:
In Pydantic V2, when using a
@model_serializerwith mode='wrap', the serialization handler (the next_serializer argument) does not automatically reconcile the include/exclude parameters passed to model_dump if not handled manually [1][2]. When you call model_dump(include=set), the SerializationInfo object passed to your wrapper function contains the serialization state, including these parameters [2][3]. However, simply passing the model instance (self) back to the handler often results in an error (e.g., PydanticOmit) or the ignoring of the include/exclude set because the wrapper function is expected to correctly manage or propagate these constraints [1][4]. Key behaviors identified: 1. Manual Propagation: If you are in a wrap serializer and wish to respect the include/exclude sets defined at the top level, you must ensure that your handler receives the appropriate arguments [2]. A common pitfall is passing the model instance (self) incorrectly or failing to pass the info object where the handler expects it [1][4]. 2. PydanticOmit Exceptions: Attempting to call the handler with improper arguments when an include set is active can trigger a PydanticOmit exception [1][4]. This occurs because the serialization logic is interrupted by the custom serializer's failure to return a valid serialized output for the requested fields [1]. 3. SerializationInfo State: The SerializationInfo object does contain information about the current serialization state [3]. While you can inspect info.include or info.exclude, Pydantic does not automatically apply these filters to your custom return value; you must manually ensure that any object returned from your wrap serializer respects the requested inclusion [2]. If you intend to perform custom serialization while still allowing Pydantic to filter fields, the most reliable approach is to delegate the standard serialization logic to the handler while ensuring you are not interfering with the internal state the handler expects [4]. If the custom logic is intended to only modify specific parts, you may need to perform the filtering yourself or ensure the handler is called with the correct context [2].Citations:
excludedoes not take effect when custom class usesmodel_serializerpydantic/pydantic#6575🏁 Script executed:
Repository: DASDAE/dascore
Length of output: 273
🏁 Script executed:
Repository: DASDAE/dascore
Length of output: 203
🏁 Script executed:
Repository: DASDAE/dascore
Length of output: 203
🌐 Web query:
site:github.com/pydantic/pydantic "info.include" "model_serializer"💡 Result:
In Pydantic, info.include refers to the include parameter passed to a serialization method (like model_dump) within a custom serialization context [1][2]. When using a custom serializer (such as
@model_serializeror@field_serializer), you can accept an info argument of type SerializationInfo [2]. This object contains metadata about the current serialization process, including the include and exclude sets that were passed to the top-level serialization call [1][2]. Key points regarding info.include: - Contextual Availability: The info.include attribute allows your custom serializer to programmatically inspect which fields the user requested to include [1][2]. - Dynamic Handling: It is often used in wrap mode serializers to manually pass the include or exclude settings down to the standard serialization logic (the handler), allowing for custom behavior that still respects the user's requested field filtering [3][4]. - Empty State: If no include filter is provided during the model_dump call, info.include may be empty or None [1]. - Pydantic Core Integration: This mechanism is a part of Pydantic's serialization API, designed to provide developers with the flexibility to override or modify serialization while still conforming to the parameters defined by the caller [1][5]. If you are implementing a custom serializer, ensure your function signature accepts the info argument: def my_serializer(self, info: SerializationInfo): # Check what fields were requested for inclusion if info.include: # Custom logic using info.include pass... This allows your code to remain aware of the broader serialization configuration applied by the end user [2].Citations:
includeandexcludeare not passed to field serializer contexts pydantic/pydantic#9538dict()in V1,model_dump()is no longer overridable by nested models pydantic/pydantic#6718excludedoes not take effect when custom class usesmodel_serializerpydantic/pydantic#6575🌐 Web query:
site:github.com/pydantic/pydantic "include=set()" serializer💡 Result:
The syntax
include=set()orinclude={...}(a set of strings) is used in Pydantic serialization methods, such asmodel_dump(), to control which fields are included in the serialized output [1][2]. Key details regarding this usage: 1. Functionality: When you pass a set to theincludeparameter inmodel_dump(), Pydantic filters the output to only include the fields listed in the set [1]. Conversely,excludedoes the opposite, removing specified fields [1]. 2. Data Structure: These parameters typically accept sets or dictionaries [2][3]. While often documented asSet[str], they are implemented to support filtering logic that checks for key membership [4]. 3. Nested Models: For nested models, you can use a dictionary where the keys are the field names and the values are nested sets or dictionaries defining which sub-fields to include [1]. 4. Common Issues: * Serialization Errors: Users occasionally encounterPydanticOmitorPydanticSerializationErrorwhen combininginclude/excludewith custommodel_serializerorfield_serializermethods, as these custom serializers may require specific handling of theinclude/excludecontext [5][6][7][8]. * Type Checking: Some users have reported that strict type checkers may flag inputs likefrozensetor tuples, even if they technically function, because the official type hints are often restricted tosetanddict[3][9][10]. In summary,include=set()is the standard, documented way to whitelist fields during serialization in Pydantic V2 [1][2]. If you are encountering issues, it is often due to interactions with custom serializers rather than theincludeparameter itself [7][8].Citations:
exclude=/include=for.model_dump()method(s) pydantic/pydantic#10010includeandexcludeare not passed to field serializer contexts pydantic/pydantic#9538model_dumpcould usefully accept frozenset pydantic/pydantic#10517excludeandincludeargument inmodel_dumptyping error pydantic/pydantic#10395🌐 Web query:
Pydantic SerializationInfo include empty set model_dump include empty set💡 Result:
In Pydantic V2, when using
model_dump(), theincludeandexcludeparameters are used to filter fields [1][2]. TheSerializationInfoobject provided to custom serializers contains metadata about the current serialization process, including theincludeandexcludesettings [1][3]. Observations regardingincludeandexcludebehavior: 1. Empty SerializationInfo: When callingmodel_dump()without specificincludeorexcludearguments, the corresponding properties inSerializationInfo(e.g.,info.include) are typically empty orNone[4]. This is expected behavior, as there are no active filters to propagate to that specific field or nested model [4]. 2. Propagation in Custom Serializers: If you are using amodel_serializerorfield_serializerwithmode='wrap', you must manually pass theinfoobject to thehandlerto ensure that filtering (includingincludeandexcludelogic) is applied to nested models correctly [5][6]. If thehandleris called without theinfoobject, or if custom logic fails to respect it, serialization filters may appear to be ignored [5][6]. 3. Empty Set vs. None: If you specifically pass an empty set tomodel_dump(include=set()), Pydantic will interpret this as an instruction to include no fields, typically resulting in an empty dictionary (excluding cases where model logic or default behavior overrides this). If you are encountering issues whereincludeorexcludeseems to be ignored in custom serializers, verify that: - Your serializer is usingmode='wrap'[7][5]. - You are correctly passing theinfoargument into thehandlercall (e.g.,handler(self, info)orhandler(value, info)) [5][6]. If you need to exclude specific empty containers (like empty lists or sets) from the final output, Pydantic does not have a nativeexclude_emptyparameter formodel_dump()[7]. This is best handled using a@model_serializer(mode='wrap')to post-process the dictionary returned by the handler [7]. [7] [5] [6]Citations:
includeandexcludeare not passed to field serializer contexts pydantic/pydantic#9538excludeargument pydantic/pydantic#11263🏁 Script executed:
Repository: DASDAE/dascore
Length of output: 9733
🏁 Script executed:
Repository: DASDAE/dascore
Length of output: 29682
🏁 Script executed:
Repository: DASDAE/dascore
Length of output: 22632
🏁 Script executed:
Repository: DASDAE/dascore
Length of output: 13121
Preserve empty
includefilters.When
model_dump(include=set())is used,info.includecontains the empty set. The current condition treats it as no filter and restoresvalue. Useinclude is None or field in include, and add a regression test.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents