diff --git a/docs/index.html b/docs/index.html index 43845e4..19dfe2f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -95,8 +95,59 @@

License

NoneType = type(None) Never = NoReturn +__all__ = [ + "__version__", + "__pubdate__", + # + # imported + "Never", + "NoneType", + # + # types + "Ignored", + "TypeForm", + "CheckFn", + "Checks", + "CastFn", + "Casts", + # + # top-level API + "castfit", + "is_type", + "to_type", + # + # extensions + "checks_type", + "casts_to", + # + # lower-level API + "get_origin_type", + "setattrs", + "is_any", + "to_any", + "is_never", + "to_never", + "is_none", + "to_none", + "is_literal", + "to_literal", + "is_union", + "to_union", + "to_bytes", + "to_str", + "is_list", + "to_list", + "is_set", + "to_set", + "is_dict", + "to_dict", + "is_tuple", + "to_tuple", + "to_datetime", +] __version__ = "0.1.0" +__pubdate__ = "2023-12-15T00:43:07Z" K = TypeVar("K") """Type variable for keys.""" @@ -112,12 +163,22 @@

License

# NOTE: We want just `Type[T]`, but `mypy` treats special forms as `object`. CheckFn = Callable[[Any, TypeForm[Any]], bool] +"""Function signature that checks if a value is of a type.""" + Checks = Dict[TypeForm[Any], CheckFn] +"""Type of internal mapping of types to check functions.""" + TYPE_CHECKS: Checks = {} +"""Mapping of types to check functions.""" CastFn = Callable[[Any, TypeForm[T]], T] +"""Function signature that maps a value to a type.""" + Casts = Dict[TypeForm[Any], CastFn[Any]] +"""Type of internal mapping of types to cast functions.""" + TYPE_CASTS: Casts = {} +"""Mapping of types to cast functions.""" @overload @@ -402,14 +463,6 @@

License

Global variables

-
var K
-
-

Type variable for keys.

-
-
var T
-
-

Type variable for values.

-
var Ignored

A function argument that is ignored.

@@ -418,6 +471,22 @@

Global variables

Type and special forms like All, Union, etc.

+
var CheckFn
+
+

Function signature that checks if a value is of a type.

+
+
var Checks
+
+

Type of internal mapping of types to check functions.

+
+
var CastFn
+
+

Function signature that maps a value to a type.

+
+
var Casts
+
+

Type of internal mapping of types to cast functions.

+
@@ -448,7 +517,7 @@

Functions

-def is_type(value: Any, kind: TypeForm[Any], checks: Optional[Checks] = None) ‑> bool +def is_type(value: Any, kind: TypeForm[Any], checks: Optional[Checks] = None) ‑> bool

Return True if value is of a type compatible with kind.

@@ -468,7 +537,7 @@

Functions

-def to_type(value: Any, kind: TypeForm[T], checks: Optional[Checks] = None, casts: Optional[Casts] = None) ‑> ~T +def to_type(value: Any, kind: TypeForm[T], checks: Optional[Checks] = None, casts: Optional[Casts] = None) ‑> ~T

Try to cast value to the type of kind.

@@ -499,43 +568,6 @@

Functions

raise TypeError(f"Cannot cast {value!r} to {kind}")
-
-def get_origin_type(given: TypeForm[T]) ‑> Type[~T] -
-
-

Returns the given type, its origin, or type(obj).

-

See: typing.get_origin

-
- -Expand source code - -
def get_origin_type(given: TypeForm[T]) -> Type[T]:
-    """Returns the `given` type, its origin, or `type(obj)`.
-
-    See: [typing.get_origin](https://docs.python.org/3/library/typing.html#typing.get_origin)
-    """
-    origin = get_origin(given) or given
-    if isinstance(origin, type):
-        return cast(Type[T], origin)  # cast due to mypy
-    return cast(Type[T], type(given))  # cast due to mypy
-
-
-
-def setattrs(obj: object, **values: Dict[str, Any]) ‑> object -
-
-

Like setattr() but for multiple values and returns the object.

-
- -Expand source code - -
def setattrs(obj: object, **values: Dict[str, Any]) -> object:
-    """Like `setattr()` but for multiple values and returns the object."""
-    for name, val in values.items():
-        setattr(obj, name, val)
-    return obj
-
-
def checks_type(*types: Any) ‑> Callable[[Callable[[Any, Union[Type[Any], Any]], bool]], Callable[[Any, Union[Type[Any], Any]], bool]]
@@ -557,7 +589,7 @@

Functions

-def casts_to(*types: Any) ‑> Callable[[~T], ~T] +def casts_to(*types: Any) ‑> Callable[[~T], ~T]

Define a type-caster for one or more types.

@@ -576,6 +608,43 @@

Functions

return _factory
+
+def get_origin_type(given: TypeForm[T]) ‑> Type[~T] +
+
+

Returns the given type, its origin, or type(obj).

+

See: typing.get_origin

+
+ +Expand source code + +
def get_origin_type(given: TypeForm[T]) -> Type[T]:
+    """Returns the `given` type, its origin, or `type(obj)`.
+
+    See: [typing.get_origin](https://docs.python.org/3/library/typing.html#typing.get_origin)
+    """
+    origin = get_origin(given) or given
+    if isinstance(origin, type):
+        return cast(Type[T], origin)  # cast due to mypy
+    return cast(Type[T], type(given))  # cast due to mypy
+
+
+
+def setattrs(obj: object, **values: Dict[str, Any]) ‑> object +
+
+

Like setattr() but for multiple values and returns the object.

+
+ +Expand source code + +
def setattrs(obj: object, **values: Dict[str, Any]) -> object:
+    """Like `setattr()` but for multiple values and returns the object."""
+    for name, val in values.items():
+        setattr(obj, name, val)
+    return obj
+
+
def is_any() ‑> bool
@@ -592,7 +661,7 @@

Functions

-def to_any(value: T) ‑> ~T +def to_any(value: T) ‑> ~T

Always return value.

@@ -667,7 +736,7 @@

Functions

-def is_literal(value: Any, kind: TypeForm[T]) ‑> bool +def is_literal(value: Any, kind: TypeForm[T]) ‑> bool

Return True if value is a valid Literal.

@@ -682,7 +751,7 @@

Functions

-def to_literal(value: T, kind: TypeForm[T]) ‑> ~T +def to_literal(value: T, kind: TypeForm[T]) ‑> ~T

Return value if it is one of the Literal values.

@@ -699,7 +768,7 @@

Functions

-def is_union(value: Any, kind: TypeForm[T]) ‑> bool +def is_union(value: Any, kind: TypeForm[T]) ‑> bool

Return True if value is a valid Union.

@@ -714,7 +783,7 @@

Functions

-def to_union(value: Any, kind: TypeForm[T]) ‑> ~T +def to_union(value: Any, kind: TypeForm[T]) ‑> ~T
@@ -769,7 +838,7 @@

Functions

-def is_list(value: Any, kind: TypeForm[T]) ‑> bool +def is_list(value: Any, kind: TypeForm[T]) ‑> bool

Return True if value is a valid list.

@@ -789,7 +858,7 @@

Functions

-def to_list(value: Any, kind: TypeForm[List[T]] = builtins.list) ‑> List[~T] +def to_list(value: Any, kind: TypeForm[List[T]] = builtins.list) ‑> List[~T]

Cast value into list.

@@ -806,7 +875,7 @@

Functions

-def is_set(value: Any, kind: TypeForm[T]) ‑> bool +def is_set(value: Any, kind: TypeForm[T]) ‑> bool

Return True if value is a valid set.

@@ -826,7 +895,7 @@

Functions

-def to_set(value: Any, kind: TypeForm[Set[T]] = builtins.set) ‑> Set[~T] +def to_set(value: Any, kind: TypeForm[Set[T]] = builtins.set) ‑> Set[~T]

Cast value into set.

@@ -843,7 +912,7 @@

Functions

-def is_dict(value: Any, kind: TypeForm[T]) ‑> bool +def is_dict(value: Any, kind: TypeForm[T]) ‑> bool

Return True if value is a valid dict.

@@ -863,7 +932,7 @@

Functions

-def to_dict(value: Any, kind: TypeForm[Dict[KT]] = builtins.dict) ‑> Dict[~K, ~T] +def to_dict(value: Any, kind: TypeForm[Dict[K, T]] = builtins.dict) ‑> Dict[~K, ~T]

Cast value into a dict.

@@ -882,7 +951,7 @@

Functions

-def is_tuple(value: Any, kind: TypeForm[T]) ‑> bool +def is_tuple(value: Any, kind: TypeForm[T]) ‑> bool

Return True if value is a valid tuple.

@@ -946,6 +1015,16 @@

Functions

+

Classes

+
+
+class NoneType +(...) +
+
+
+
+
diff --git a/test/test_to_type.py b/test/test_to_type.py index 655144e..724c006 100644 --- a/test/test_to_type.py +++ b/test/test_to_type.py @@ -20,11 +20,18 @@ from castfit import NoneType -def test_get_class() -> None: +def test_get_origin_type() -> None: + """Get the appropriate constructor.""" assert castfit.get_origin_type(List[int]) == list assert castfit.get_origin_type(list) == list assert castfit.get_origin_type([]) == list + class MyList(List[int]): + pass + + assert castfit.get_origin_type(MyList) == MyList + assert castfit.get_origin_type(MyList([1, 2, 3])) == MyList + def test_any() -> None: """Keep whatever type you have.""" @@ -115,7 +122,9 @@ def test_datetime() -> None: ) -def test_spec_class() -> None: +def test_castfit_class() -> None: + """Cast data using a class.""" + class Spec: name: str age: int @@ -127,7 +136,9 @@ class Spec: assert have.loc == Path("/") -def test_spec_object() -> None: +def test_castfit_object() -> None: + """Cast data using an instance.""" + class Spec: name: str age: int @@ -140,6 +151,8 @@ class Spec: def test_spec_dataclass() -> None: + """Cast data using a dataclass.""" + @dataclass class Spec: name: str