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.

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 we somehow get a non-Missing value (should not happen), return it unchanged.
        return Missing

    @staticmethod
    def _serialize(value: Any) -> str:
        # We used to want to raise a PydanticOmit here, but now we are using `exclude_if`, which does not need it.
        return "**MISSING**"

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

Ancestors