Module pydantic_partials.sentinels

Global variables

var AutoPartialExcludeMarker

Used by pydantic_partials.partial.AutoPartialExclude to mark a field as excluded from automatic partials. See PartialConfigDict.auto_partials_exclude for more details.

var Missing

Returned as attribute value when attribute value is missing. Can also be set on attribute to indicate it's missing.

Classes

class AutoPartialExcludeMarkerType (*args, **kwargs)
Expand source code
class AutoPartialExcludeMarkerType(Sentinel):
    pass

This can be used as the superclass that should always be a Singleton no mater what. Example of something you may want to make a true singleton is a sentinel-type, like the NoneType that Python has.

See the default-type.Default for an example of how you can use this, or the following code example:

class DefaultType(Singleton):
    pass

Default = DefaultType()

Class Arguments:

  • name: You can provide a name for instances of the type, this is what they will return from __repr__ for the object's description.

    By default, if you don't provide this we take the class name, and strip off the word "Type" and the end of class name (if present at end of class name). Whatever is left is what we return for by default for this name argument.

  • value_as_bool: Default's to True. The value provided here is what is returned from __bool__. This is what Python uses as the bool value for the object. Consider overriding this to False if you want to make a sentential type-objects.

    It seems like sentential type-objects normally want to be False like.

    Similar to why None has False as it's bool-value.

    You can override by setting value_as_bool=True as a class argument, ie:

    ```python from xsentinels import Singleton

    class MySingletonType(Singleton, value_as_bool=True) pass

    Will now be True like when used as a bool.

    assert MySingletonType()

    ```

Ancestors

class MissingType (*args, **kwargs)
Expand source code
class MissingType(Sentinel):
    """ Class/Type of `Missing`, a sentinel that is used to indicate if a field is missing its value
        in a `pydantic_partials.partial.PartialModel` subclass.
    """
    # Notes/See (https://docs.pydantic.dev/latest/concepts/json_schema/#modifying-the-schema).

    @classmethod
    def __get_pydantic_core_schema__(
        cls, source_type: Type[Any], handler: GetCoreSchemaHandler
    ) -> core_schema.CoreSchema:
        assert source_type is MissingType
        # We never want to serialize any Missing values.
        return core_schema.with_info_after_validator_function(
            cls._validate,
            core_schema.is_instance_schema(cls=MissingType),  # core_schema.any_schema(),
            # serialization=core_schema.plain_serializer_function_ser_schema(
            #     cls._serialize,
            #     # return_schema=core_schema.AnySchema
            # )
        )

    @staticmethod
    def _validate(value: Any, info: core_schema.ValidationInfo) -> 'MissingType':
        if value is Missing:
            # Keeps the associated attribute 'deleted/omitted' from model.
            raise PydanticOmit()

        # If we somehow get a non-Missing value (should not happen), return it unchanged.
        return value

    @staticmethod
    def _serialize(value: Any) -> 'MissingType':
        # Keeps the associated attribute 'deleted/omitted' from model.
        # raise PydanticOmit()

        # Return same value we got, when requested to try and serialize real data it's not 'Missing',
        # and if it is, that means `Value` is `Missing` and we are fine to return that also.
        # I would love to rase a `raise PydanticOmit()` if `value` is `Missing`, but that does not currently work
        # for Pydantic, so I just return the `Missing` unchanged for now.
        # If there is a serialization error later on about how Pydantic can't turn `Missing` into a `Json`
        # then we can debug the situation
        # (right now PartialModel should delete any attributes that are set to `Missing` to work around this limitation)
        return value

Class/Type of Missing, a sentinel that is used to indicate if a field is missing its value in a PartialModel subclass.

Ancestors