Skip to content

Commit

Permalink
Fix deprecated UnicodeDelimitedTuple due to deprecated `_field_type…
Browse files Browse the repository at this point in the history
…s` (#35)

_field_types was removed in Python 3.9 causing UnicodeDelimitedTupleAttribute to not function properly.
  • Loading branch information
NazarioJL authored Jul 13, 2021
1 parent a6f8823 commit 1fd641d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
matrix:
python-version:
- "3.8"
- "3.9"

services:
dynamodb-local:
Expand Down
6 changes: 3 additions & 3 deletions pynamodb_attributes/timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class TimedeltaAttribute(Attribute[timedelta]):
""" "
"""
Stores a timedelta as a number of seconds (truncated).
>>> class MyModel(Model):
Expand All @@ -33,15 +33,15 @@ def __set__(self, instance: Any, value: Optional[Any]) -> None:


class TimedeltaMsAttribute(TimedeltaAttribute):
""" "
"""
Stores a timedelta as a number of milliseconds AKA ms (truncated).
"""

_multiplier = 1000.0


class TimedeltaUsAttribute(TimedeltaAttribute):
""" "
"""
Stores a timedelta as a number of microseconds AKA μs (truncated).
"""

Expand Down
6 changes: 3 additions & 3 deletions pynamodb_attributes/timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


class TimestampAttribute(Attribute[datetime]):
""" "
"""
Stores time as a Unix epoch timestamp (in seconds) in a DynamoDB number.
>>> class MyModel(Model):
Expand Down Expand Up @@ -37,15 +37,15 @@ def __set__(self, instance: Any, value: Optional[Any]) -> None:


class TimestampMsAttribute(TimestampAttribute):
""" "
"""
Stores time as a Unix epoch timestamp in milliseconds (ms) in a DynamoDB number.
"""

_multiplier = 1000.0


class TimestampUsAttribute(TimestampAttribute):
""" "
"""
Stores times as a Unix epoch timestamp in microseconds (μs) in a DynamoDB number.
"""

Expand Down
17 changes: 12 additions & 5 deletions pynamodb_attributes/unicode_delimited_tuple.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any
from typing import get_type_hints
from typing import List
from typing import Tuple
from typing import Type
Expand Down Expand Up @@ -44,12 +45,18 @@ def __init__(
self.delimiter = delimiter

def deserialize(self, value: str) -> T:
fields = getattr(self.tuple_type, "_fields", None)
field_types = getattr(self.tuple_type, "_field_types", None)
if fields and field_types:
values = value.split(self.delimiter, maxsplit=len(fields))
field_types = get_type_hints(self.tuple_type)

if field_types:
values = value.split(self.delimiter, maxsplit=len(field_types))
return self.tuple_type(
**{f: field_types[f](v) for f, v in zip(fields, values)}
**{
field_name: field_type(value)
for (field_name, field_type), value in zip(
field_types.items(),
values,
)
}
)
else:
return self.tuple_type(value.split(self.delimiter))
Expand Down

0 comments on commit 1fd641d

Please sign in to comment.