Skip to content

Commit 93ad116

Browse files
committed
Implement more docstrings and bump version.
1 parent 9211af4 commit 93ad116

File tree

2 files changed

+90
-7
lines changed

2 files changed

+90
-7
lines changed

fastapi_users_db_dynamodb/attributes.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,38 @@
88

99
class GUID(Attribute[UUID_ID]):
1010
"""
11-
Custom PynamoDB attribute to store UUIDs as strings.
12-
Ensures value is always a UUID object in Python.
11+
Custom PynamoDB `Attribute` to store `UUID`s as strings. \
12+
13+
Ensures value is always a `UUID` object in Python.
1314
"""
1415

1516
attr_type = STRING
1617
python_type = UUID_ID
1718

1819
def serialize(self, value):
20+
"""Serialize a value for later storage in DynamoDB.
21+
22+
Args:
23+
value (UUID_ID): The `UUID` to be serialized.
24+
25+
Returns:
26+
str: The serialized `UUID` as a string.
27+
"""
1928
if value is None:
2029
return None
2130
if isinstance(value, UUID_ID):
2231
return str(value)
2332
return str(UUID_ID(value))
2433

2534
def deserialize(self, value):
35+
"""Deserialize a value from DynamoDB and convert it back into a python object.
36+
37+
Args:
38+
value (str): The serialized string representation of the `UUID`.
39+
40+
Returns:
41+
UUID_ID: The deserialized `UUID` python object.
42+
"""
2643
if value is None:
2744
return None
2845
if not isinstance(value, UUID_ID):
@@ -38,19 +55,36 @@ class TransformingUnicodeAttribute(UnicodeAttribute):
3855
"""
3956

4057
def __init__(self, transform: Callable[[str], str] | None = None, **kwargs):
41-
"""
42-
:param transform: A callable to transform the string (e.g., str.lower, str.upper)
43-
:param kwargs: Other UnicodeAttribute kwargs
58+
"""Initialize the `Attribute` class.
59+
60+
Args:
61+
transform (Callable[[str], str] | None, optional): A callable to transform the string (e.g., `str.lower`, `str.upper`). Defaults to None.
4462
"""
4563
super().__init__(**kwargs)
4664
self.transform = transform
4765

4866
def serialize(self, value):
67+
"""Serialize a value for later storage in DynamoDB.
68+
69+
Args:
70+
value (str): The string to be transformed and serialized.
71+
72+
Returns:
73+
str: The transformed and serialized object.
74+
"""
4975
if value is not None and self.transform:
5076
value = self.transform(value)
5177
return super().serialize(value)
5278

5379
def deserialize(self, value):
80+
"""Deserialize a value from DynamoDB and convert it back into a python object.
81+
82+
Args:
83+
value (str): The serialized string representation of the object.
84+
85+
Returns:
86+
str: The deserialized python object.
87+
"""
5488
value = super().deserialize(value)
5589
if value is not None and self.transform:
5690
value = self.transform(value)

fastapi_users_db_dynamodb/config.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,39 @@
11
from enum import StrEnum
22
from typing import Any, Literal, TypedDict
33

4-
__version__ = "1.0.0"
4+
__version__ = "1.0.1"
55

66

7-
# Right now, only ON-DEMAND mode is supported!
7+
# Right now, only ON-DEMAND/PAY_PER_REQUEST mode is supported!
88
class BillingMode(StrEnum):
9+
"""An `Enum` class representing the billing mode of tables. \
10+
11+
**NOTE**: Right now, only `PAY_PER_REQUEST` is supported!
12+
13+
Args:
14+
StrEnum (_type_): The base enum class.
15+
16+
Returns:
17+
_type_: The enum member, representing the billing mode.
18+
"""
919
PAY_PER_REQUEST = "PAY_PER_REQUEST"
1020
# PROVISIONED = "PROVISIONED"
1121

1222
def __str__(self) -> str:
23+
"""Return the billing mode directly as a string.
24+
25+
Returns:
26+
str: The billing mode's string representation.
27+
"""
1328
return self.value
1429

1530

1631
class __ConfigMap(TypedDict):
32+
"""A `TypedDict`, enforcing static types on config keys.
33+
34+
Args:
35+
TypedDict (_type_): The base type.
36+
"""
1737
DATABASE_REGION: str
1838
# DATABASE_BILLING_MODE: BillingMode
1939
DATABASE_BILLING_MODE: Literal[BillingMode.PAY_PER_REQUEST]
@@ -23,6 +43,16 @@ class __ConfigMap(TypedDict):
2343

2444

2545
def __create_config():
46+
"""Create a new config instance for this python process. \
47+
Can be modified using this module's `get` and `set` methods.
48+
49+
Raises:
50+
KeyError: If the key is not known or not defined.
51+
TypeError: If the config expected another type for a certain key.
52+
53+
Returns:
54+
_type_: The created `get` and `set` methods.
55+
"""
2656
__config_map: __ConfigMap = {
2757
"DATABASE_REGION": "eu-central-1",
2858
"DATABASE_BILLING_MODE": BillingMode.PAY_PER_REQUEST,
@@ -32,9 +62,28 @@ def __create_config():
3262
}
3363

3464
def get(key: str, default: Any = None) -> Any:
65+
"""Get a value from the config dict.
66+
67+
Args:
68+
key (str): The unique key specifying the configuration option.
69+
default (Any, optional): A default value that is being returned when the key is not found. Defaults to None.
70+
71+
Returns:
72+
Any: The configuration value.
73+
"""
3574
return __config_map.get(key, default)
3675

3776
def set(key: str, value: Any) -> None:
77+
"""Set/Change a value inside of the config dict.
78+
79+
Args:
80+
key (str): The unique key specifying the configuration option.
81+
value (Any): The new value this configuration option will be changed to.
82+
83+
Raises:
84+
KeyError: If the key is not defined or invalid.
85+
TypeError: If the value's type does not match the options expected type.
86+
"""
3887
if key not in __config_map:
3988
raise KeyError(f"Unknown config key: {key}")
4089
expected_type = type(__config_map[key]) # type: ignore[literal-required]

0 commit comments

Comments
 (0)