Skip to content

Commit 4421acb

Browse files
authored
Annotate Attribute.set typing (#1104)
Make sure that e.g. `MyModel.number_attr.set('spam')` fails type-check.
1 parent a74e288 commit 4421acb

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

pynamodb/attributes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ def append(self, other: Iterable) -> '_ListAppend':
253253
def prepend(self, other: Iterable) -> '_ListAppend':
254254
return Path(self).prepend(other)
255255

256-
def set(self, value: Any) -> 'SetAction':
256+
def set(
257+
self,
258+
value: Union[_T, 'Attribute[_T]', '_Increment', '_Decrement', '_IfNotExists', '_ListAppend']
259+
) -> 'SetAction':
257260
return Path(self).set(value)
258261

259262
def remove(self) -> 'RemoveAction':

typing_tests/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ class MyModel(Model):
4343

4444

4545
def test_model_update() -> None:
46-
from pynamodb.attributes import NumberAttribute
46+
from pynamodb.attributes import NumberAttribute, UnicodeAttribute
4747
from pynamodb.models import Model
4848

4949
class MyModel(Model):
5050
my_attr = NumberAttribute()
51+
my_str_attr = UnicodeAttribute()
5152

5253
my_model = MyModel()
5354
my_model.update(actions=[
@@ -59,6 +60,9 @@ class MyModel(Model):
5960
MyModel.my_attr.set(MyModel.my_attr | 123),
6061
])
6162

63+
_ = MyModel.my_attr.set('foo') # type:ignore[arg-type]
64+
_ = MyModel.my_attr.set(MyModel.my_str_attr) # type:ignore[arg-type]
65+
6266

6367
def test_paths() -> None:
6468
import pynamodb.expressions.operand

0 commit comments

Comments
 (0)