Skip to content

Commit

Permalink
typing: Attribute.append/prepend (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikonst committed Jun 29, 2021
1 parent 3d29ab4 commit d42a019
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pynamodb/attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from datetime import timezone
from inspect import getfullargspec
from inspect import getmembers
from typing import Any, Callable, Dict, Generic, List, Mapping, Optional, TypeVar, Type, Union, Set, overload
from typing import Any, Callable, Dict, Generic, List, Mapping, Optional, TypeVar, Type, Union, Set, overload, Iterable
from typing import TYPE_CHECKING

from pynamodb._compat import GenericMeta
Expand Down Expand Up @@ -198,10 +198,10 @@ def __rsub__(self, other: Any) -> '_Decrement':
def __or__(self, other: Any) -> '_IfNotExists':
return Path(self).__or__(other)

def append(self, other: Any) -> '_ListAppend':
def append(self, other: Iterable) -> '_ListAppend':
return Path(self).append(other)

def prepend(self, other: Any) -> '_ListAppend':
def prepend(self, other: Iterable) -> '_ListAppend':
return Path(self).prepend(other)

def set(self, value: Any) -> 'SetAction':
Expand Down
15 changes: 15 additions & 0 deletions tests/test_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,18 @@ class MyModel(Model):
_ = MyModel.attr.is_in(123) # E: Argument 1 to "is_in" of "Attribute" has incompatible type "int"; expected "str" [arg-type]
_ = MyModel.attr.is_in(['foo', 'bar']) # E: Argument 1 to "is_in" of "Attribute" has incompatible type "List[str]"; expected "str" [arg-type]
""")


def test_append(assert_mypy_output):
assert_mypy_output("""
from pynamodb.models import Model
from pynamodb.attributes import ListAttribute, NumberAttribute
class MyModel(Model):
attr = ListAttribute(of=NumberAttribute)
MyModel.attr.append(42) # E: Argument 1 to "append" of "Attribute" has incompatible type "int"; expected "Iterable[Any]" [arg-type]
MyModel.attr.append([42])
MyModel.attr.prepend(42) # E: Argument 1 to "prepend" of "Attribute" has incompatible type "int"; expected "Iterable[Any]" [arg-type]
MyModel.attr.prepend([42])
""")

0 comments on commit d42a019

Please sign in to comment.