Skip to content

Commit d19b1c1

Browse files
authored
models: range_key/filter_condition optional (pynamodb#588)
The `range_key_condition` and `filter_condition` arguments default to `None`, and `None` triggers a specific behavior, so the type hints must allow passing `None`.
1 parent 0d9b8e6 commit d19b1c1

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

pynamodb/models.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ class Model(metaclass=MetaModel):
5858
def query(
5959
cls: Type[_T],
6060
hash_key: KeyType,
61-
range_key_condition: Condition = ...,
62-
filter_condition: Condition = ...,
61+
range_key_condition: Optional[Condition] = ...,
62+
filter_condition: Optional[Condition] = ...,
6363
consistent_read: bool = ...,
6464
index_name: Optional[Text] = ...,
6565
scan_index_forward: Optional[Any] = ...,

pynamodb/tests/test_mypy.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,30 @@ class MyModel(Model):
2121
""")
2222

2323

24+
def test_model_query():
25+
assert_mypy_output("""
26+
from pynamodb.attributes import NumberAttribute
27+
from pynamodb.models import Model
28+
29+
class MyModel(Model):
30+
my_attr = NumberAttribute()
31+
32+
# test hash key types
33+
MyModel.query(123)
34+
MyModel.query('123')
35+
MyModel.query(12.3)
36+
MyModel.query(b'123')
37+
MyModel.query((1, 2, 3))
38+
MyModel.query({'1': '2'}) # E: Argument 1 to "query" of "Model" has incompatible type "Dict[str, str]"; expected "Union[str, bytes, float, Tuple[Any, ...]]"
39+
40+
# test conditions
41+
MyModel.query(123, range_key_condition=(MyModel.my_attr == 5), filter_condition=(MyModel.my_attr == 5))
42+
43+
# test conditions are optional
44+
MyModel.query(123, range_key_condition=None, filter_condition=None)
45+
""") # noqa: E501
46+
47+
2448
def test_number_attribute():
2549
assert_mypy_output("""
2650
from pynamodb.attributes import NumberAttribute

0 commit comments

Comments
 (0)