Skip to content

Commit a9684e2

Browse files
authored
Support derived model initializers (#4)
1 parent 14abb4a commit a9684e2

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

pynamodb_mypy/plugin.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,12 @@ def _get_function_signature_hook__pynamodb_model__init__(self, ctx: FunctionSigC
103103
if not isinstance(model_instance, mypy.types.Instance): # pragma: no cover
104104
return ctx.default_signature
105105

106-
args = {
107-
attr_name: _rehydrate_type(ctx.api, attr_data["type"])
108-
for attr_name, attr_data in _pynamodb_attributes_metadata(model_instance.type).items()
109-
}
106+
args = {}
107+
for model_cls in model_instance.type.mro:
108+
args.update({
109+
attr_name: _rehydrate_type(ctx.api, attr_data["type"])
110+
for attr_name, attr_data in _pynamodb_attributes_metadata(model_cls).items()
111+
})
110112

111113
# substitute hash/range key types
112114
hash_key_type: mypy.types.Type = mypy.types.NoneTyp()

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = pynamodb-mypy
3-
version = 0.1.1
3+
version = 0.1.2
44
description = mypy plugin for PynamoDB
55
long_description = file: README.md
66
long_description_content_type = text/markdown

tests/test_plugin.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,21 @@ class MyModel(Model):
1414
my_range_key = NumberAttribute(range_key=True)
1515
my_attr = NumberAttribute()
1616
17+
class MyDerivedModel(MyModel):
18+
my_derived_attr = NumberAttribute()
19+
1720
MyModel(my_attr=5.5)
1821
MyModel(5.5, my_attr=5.5)
1922
MyModel(5.5, 5.5, my_attr=5.5)
2023
MyModel(hash_key=5.5, range_key=5.5, my_attr=5.5)
2124
MyModel(hash_key='hello', range_key='world', my_attr=5.5) # E: Argument "hash_key" to "MyModel" has incompatible type "str"; expected "float" [arg-type]
2225
# E: Argument "range_key" to "MyModel" has incompatible type "str"; expected "float" [arg-type]
2326
MyModel(foobar=5.5) # E: Unexpected keyword argument "foobar" for "MyModel" [call-arg]
27+
28+
# test with derived model
29+
30+
MyDerivedModel(my_attr=5.5, my_derived_attr=42)
31+
MyDerivedModel(foobar=5.5) # E: Unexpected keyword argument "foobar" for "MyDerivedModel" [call-arg]
2432
"""
2533
)
2634

0 commit comments

Comments
 (0)