Skip to content

upathtype

Class info

Classes

Name Children Inherits
UPath
mkdocs_mknodes.appconfig.upathtype
A Pydantic type for universal_pathlib.UPath.

    🛈 DocStrings

    Pydantic types for universal_pathlib (upath.UPath) with validation capabilities.

    This module provides a Pydantic-compatible type for upath.UPath, offering similar functionality to Pydantic's built-in Path type. It includes validators for existence checks and path type verification.

    Example
    from pydantic import BaseModel
    
    class Config(BaseModel):
        input_file: UPathFile
        output_dir: UPathDir
        temp_path: UPath
    
    # Usage
    config = Config(
        input_file="data.csv",
        output_dir="output",
        temp_path="temp/workspace"
    )
    

    FileConfig

    Bases: BaseModel

    Example configuration using UPath types.

    Source code in mkdocs_mknodes/appconfig/upathtype.py
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    class FileConfig(BaseModel):
        """Example configuration using UPath types."""
    
        input_file: UPathFile
        output_dir: UPathDir
        temp_path: UPath
    
        model_config = {
            "frozen": True,
            "extra": "forbid",
        }
    

    UPath

    A Pydantic type for universal_pathlib.UPath.

    This type handles conversion of strings and path-like objects to UPath objects, with proper serialization support. It can be used directly in Pydantic models and supports validation via the provided validator types.

    Source code in mkdocs_mknodes/appconfig/upathtype.py
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    class UPath:
        """A Pydantic type for universal_pathlib.UPath.
    
        This type handles conversion of strings and path-like objects to UPath objects,
        with proper serialization support. It can be used directly in Pydantic models
        and supports validation via the provided validator types.
        """
    
        @classmethod
        def __get_pydantic_core_schema__(
            cls,
            _source_type: Any,
            _handler: GetCoreSchemaHandler,
        ) -> CoreSchema:
            """Generate the Pydantic core schema for UPath type."""
            return core_schema.json_or_python_schema(
                json_schema=core_schema.str_schema(),
                python_schema=core_schema.union_schema([
                    core_schema.is_instance_schema(upath.UPath),
                    core_schema.chain_schema([
                        core_schema.str_schema(),
                        core_schema.no_info_plain_validator_function(_convert_to_upath),
                    ]),
                ]),
                serialization=core_schema.plain_serializer_function_ser_schema(
                    lambda x: str(x), return_schema=core_schema.str_schema(), when_used="json"
                ),
            )
    
        @classmethod
        def __get_pydantic_json_schema__(
            cls,
            _core_schema: CoreSchema,
            _handler: GetJsonSchemaHandler,
        ) -> JsonSchemaValue:
            """Generate the JSON schema for UPath type."""
            return {"type": "string", "format": "path"}
    

    __get_pydantic_core_schema__ classmethod

    __get_pydantic_core_schema__(
        _source_type: Any, _handler: GetCoreSchemaHandler
    ) -> CoreSchema
    

    Generate the Pydantic core schema for UPath type.

    Source code in mkdocs_mknodes/appconfig/upathtype.py
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    @classmethod
    def __get_pydantic_core_schema__(
        cls,
        _source_type: Any,
        _handler: GetCoreSchemaHandler,
    ) -> CoreSchema:
        """Generate the Pydantic core schema for UPath type."""
        return core_schema.json_or_python_schema(
            json_schema=core_schema.str_schema(),
            python_schema=core_schema.union_schema([
                core_schema.is_instance_schema(upath.UPath),
                core_schema.chain_schema([
                    core_schema.str_schema(),
                    core_schema.no_info_plain_validator_function(_convert_to_upath),
                ]),
            ]),
            serialization=core_schema.plain_serializer_function_ser_schema(
                lambda x: str(x), return_schema=core_schema.str_schema(), when_used="json"
            ),
        )
    

    __get_pydantic_json_schema__ classmethod

    __get_pydantic_json_schema__(
        _core_schema: CoreSchema, _handler: GetJsonSchemaHandler
    ) -> JsonSchemaValue
    

    Generate the JSON schema for UPath type.

    Source code in mkdocs_mknodes/appconfig/upathtype.py
    138
    139
    140
    141
    142
    143
    144
    145
    @classmethod
    def __get_pydantic_json_schema__(
        cls,
        _core_schema: CoreSchema,
        _handler: GetJsonSchemaHandler,
    ) -> JsonSchemaValue:
        """Generate the JSON schema for UPath type."""
        return {"type": "string", "format": "path"}
    

    _convert_to_upath

    _convert_to_upath(value: Any) -> UPath
    

    Convert a value to a UPath object.

    Parameters:

    Name Type Description Default
    value Any

    Input value to convert (string, PathLike, or UPath)

    required

    Returns:

    Name Type Description
    UPath UPath

    Converted UPath object

    Raises:

    Type Description
    TypeError

    If the value cannot be converted to a UPath

    Source code in mkdocs_mknodes/appconfig/upathtype.py
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    def _convert_to_upath(value: Any) -> upath.UPath:
        """Convert a value to a UPath object.
    
        Args:
            value: Input value to convert (string, PathLike, or UPath)
    
        Returns:
            UPath: Converted UPath object
    
        Raises:
            TypeError: If the value cannot be converted to a UPath
        """
        try:
            return upath.UPath(value)
        except (TypeError, ValueError) as e:
            msg = f"Cannot convert {value!r} to UPath: {e}"
            raise TypeError(msg) from e
    

    _validate_is_dir

    _validate_is_dir(path: UPath) -> UPath
    

    Validate that a path points to a directory.

    Parameters:

    Name Type Description Default
    path UPath

    UPath object to validate

    required

    Returns:

    Name Type Description
    UPath UPath

    The validated path

    Raises:

    Type Description
    ValueError

    If the path is not a directory

    Source code in mkdocs_mknodes/appconfig/upathtype.py
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    def _validate_is_dir(path: upath.UPath) -> upath.UPath:
        """Validate that a path points to a directory.
    
        Args:
            path: UPath object to validate
    
        Returns:
            UPath: The validated path
    
        Raises:
            ValueError: If the path is not a directory
        """
        path = upath.UPath(path)
        if not path.is_dir():
            msg = f"Path is not a directory: {path}"
            raise ValueError(msg)
        return path
    

    _validate_is_file

    _validate_is_file(path: UPath) -> UPath
    

    Validate that a path points to a file.

    Parameters:

    Name Type Description Default
    path UPath

    UPath object to validate

    required

    Returns:

    Name Type Description
    UPath UPath

    The validated path

    Raises:

    Type Description
    ValueError

    If the path is not a file

    Source code in mkdocs_mknodes/appconfig/upathtype.py
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    def _validate_is_file(path: upath.UPath) -> upath.UPath:
        """Validate that a path points to a file.
    
        Args:
            path: UPath object to validate
    
        Returns:
            UPath: The validated path
    
        Raises:
            ValueError: If the path is not a file
        """
        path = upath.UPath(path)
        if not path.is_file():
            msg = f"Path is not a file: {path}"
            raise ValueError(msg)
        return path
    

    _validate_path_exists

    _validate_path_exists(path: UPath) -> UPath
    

    Validate that a path exists.

    Parameters:

    Name Type Description Default
    path UPath

    UPath object to validate

    required

    Returns:

    Name Type Description
    UPath UPath

    The validated path

    Raises:

    Type Description
    ValueError

    If the path does not exist

    Source code in mkdocs_mknodes/appconfig/upathtype.py
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    def _validate_path_exists(path: upath.UPath) -> upath.UPath:
        """Validate that a path exists.
    
        Args:
            path: UPath object to validate
    
        Returns:
            UPath: The validated path
    
        Raises:
            ValueError: If the path does not exist
        """
        path = upath.UPath(path)
        if not path.exists():
            msg = f"Path does not exist: {path}"
            raise ValueError(msg)
        return path