|
7 | 7 |
|
8 | 8 | from typing import Any, TypeVar
|
9 | 9 |
|
| 10 | +from pyre_extensions import assert_is_instance |
10 | 11 |
|
11 | 12 | T = TypeVar("T")
|
12 | 13 | V = TypeVar("V")
|
|
15 | 16 | Y = TypeVar("Y")
|
16 | 17 |
|
17 | 18 |
|
18 |
| -def checked_cast(typ: type[T], val: V, exception: Exception | None = None) -> T: |
| 19 | +def assert_is_instance_optional(val: V | None, typ: type[T]) -> T | None: |
19 | 20 | """
|
20 |
| - Cast a value to a type (with a runtime safety check). |
21 |
| -
|
22 |
| - Returns the value unchanged and checks its type at runtime. This signals to the |
23 |
| - typechecker that the value has the designated type. |
24 |
| -
|
25 |
| - Like `typing.cast`_ ``check_cast`` performs no runtime conversion on its argument, |
26 |
| - but, unlike ``typing.cast``, ``checked_cast`` will throw an error if the value is |
27 |
| - not of the expected type. The type passed as an argument should be a python class. |
| 21 | + Asserts that the value is an instance of the given type if it is not None. |
28 | 22 |
|
29 | 23 | Args:
|
30 |
| - typ: the type to cast to |
31 |
| - val: the value that we are casting |
32 |
| - exception: override exception to raise if typecheck fails |
| 24 | + val: the value to check |
| 25 | + typ: the type to check against |
33 | 26 | Returns:
|
34 |
| - the ``val`` argument, unchanged |
35 |
| -
|
36 |
| - .. _typing.cast: https://docs.python.org/3/library/typing.html#typing.cast |
| 27 | + the `val` argument, unchanged |
37 | 28 | """
|
38 |
| - if not isinstance(val, typ): |
39 |
| - raise ( |
40 |
| - exception |
41 |
| - if exception is not None |
42 |
| - else ValueError(f"Value was not of type {typ}:\n{val}") |
43 |
| - ) |
44 |
| - return val |
45 |
| - |
46 |
| - |
47 |
| -def checked_cast_optional(typ: type[T], val: V | None) -> T | None: |
48 |
| - """Calls checked_cast only if value is not None.""" |
49 | 29 | if val is None:
|
50 | 30 | return val
|
51 |
| - return checked_cast(typ, val) |
| 31 | + return assert_is_instance(val, typ) |
52 | 32 |
|
53 | 33 |
|
54 |
| -def checked_cast_list(typ: type[T], old_l: list[V]) -> list[T]: |
55 |
| - """Calls checked_cast on all items in a list.""" |
56 |
| - new_l = [] |
57 |
| - for val in old_l: |
58 |
| - val = checked_cast(typ, val) |
59 |
| - new_l.append(val) |
60 |
| - return new_l |
| 34 | +def assert_is_instance_list(old_l: list[V], typ: type[T]) -> list[T]: |
| 35 | + """ |
| 36 | + Asserts that all items in a list are instances of the given type. |
61 | 37 |
|
| 38 | + Args: |
| 39 | + old_l: the list to check |
| 40 | + typ: the type to check against |
| 41 | + Returns: |
| 42 | + the `old_l` argument, unchanged |
| 43 | + """ |
| 44 | + return [assert_is_instance(val, typ) for val in old_l] |
62 | 45 |
|
63 |
| -def checked_cast_dict( |
64 |
| - key_typ: type[K], value_typ: type[V], d: dict[X, Y] |
| 46 | + |
| 47 | +def assert_is_instance_dict( |
| 48 | + d: dict[X, Y], key_type: type[K], val_type: type[V] |
65 | 49 | ) -> dict[K, V]:
|
66 |
| - """Calls checked_cast on all keys and values in the dictionary.""" |
| 50 | + """ |
| 51 | + Asserts that all keys and values in the dictionary are instances |
| 52 | + of the given classes. |
| 53 | +
|
| 54 | + Args: |
| 55 | + d: the dictionary to check |
| 56 | + key_type: the type to check against for keys |
| 57 | + val_type: the type to check against for values |
| 58 | + Returns: |
| 59 | + the `d` argument, unchanged |
| 60 | + """ |
67 | 61 | new_dict = {}
|
68 | 62 | for key, val in d.items():
|
69 |
| - val = checked_cast(value_typ, val) |
70 |
| - key = checked_cast(key_typ, key) |
| 63 | + key = assert_is_instance(key, key_type) |
| 64 | + val = assert_is_instance(val, val_type) |
71 | 65 | new_dict[key] = val
|
72 | 66 | return new_dict
|
73 | 67 |
|
74 | 68 |
|
75 | 69 | # pyre-fixme[34]: `T` isn't present in the function's parameters.
|
76 |
| -def checked_cast_to_tuple(typ: tuple[type[V], ...], val: V) -> T: |
| 70 | +def assert_is_instance_of_tuple(val: V, typ: tuple[type[V], ...]) -> T: |
77 | 71 | """
|
78 |
| - Cast a value to a union of multiple types (with a runtime safety check). |
79 |
| - This function is similar to `checked_cast`, but allows for the type to be |
80 |
| - defined as a tuple of types, in which case the value is cast as a union of |
81 |
| - the types in the tuple. |
| 72 | + Asserts that a value is an instance of any type in a tuple of types. |
82 | 73 |
|
83 | 74 | Args:
|
84 |
| - typ: the tuple of types to cast to |
85 |
| - val: the value that we are casting |
| 75 | + typ: the tuple of types to check against |
| 76 | + val: the value that we are checking |
86 | 77 | Returns:
|
87 |
| - the ``val`` argument, unchanged |
| 78 | + the `val` argument, unchanged |
88 | 79 | """
|
89 | 80 | if not isinstance(val, typ):
|
90 |
| - raise ValueError(f"Value was not of type {type!r}:\n{val!r}") |
| 81 | + raise TypeError(f"Value was not of any type {typ!r}:\n{val!r}") |
91 | 82 | # pyre-fixme[7]: Expected `T` but got `V`.
|
92 | 83 | return val
|
93 | 84 |
|
|
0 commit comments