Skip to content

Commit 1f74752

Browse files
authored
Rename NullableAttribute to NullableAttributeWrapper (#1)
1 parent 3437002 commit 1f74752

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

pynamodb_mypy/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from mypy.plugin import Plugin
1010

1111
ATTR_FULL_NAME = 'pynamodb.attributes.Attribute'
12-
NULL_ATTR_FULL_NAME = 'pynamodb.attributes._NullableAttribute'
12+
NULL_ATTR_WRAPPER_FULL_NAME = 'pynamodb.attributes._NullableAttributeWrapper'
1313

1414

1515
class PynamodbPlugin(Plugin):
@@ -58,7 +58,7 @@ def _attribute_instantiation_hook(ctx: FunctionContext,
5858
return ctx.default_return_type
5959

6060
if null_arg_expr.fullname == 'builtins.True':
61-
return ctx.api.named_generic_type(NULL_ATTR_FULL_NAME, [
61+
return ctx.api.named_generic_type(NULL_ATTR_WRAPPER_FULL_NAME, [
6262
ctx.default_return_type,
6363
underlying_type,
6464
])

requirements.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
-e .
2-
# Remove this line after mypy-0.660 is released
3-
-e git+git://github.com/ikonst/mypy.git@5a8dffb5bd94bda615703f6994d39ea1c7c02ef5#egg=mypy
2+
# TODO: Remove this line after mypy-0.660 is released
3+
-e git+git://github.com/python/mypy.git@2b8691d8a485fbb91f28e6d570b147a629213118#egg=mypy
4+
# TODO: Remove this line after https://github.com/pynamodb/PynamoDB/pull/579 is released
5+
-e git+git://github.com/ikonst/pynamodb.git@1efc2abd42afc5c759655a0abde8b823aeb18a1c#egg=pynamodb
46
pre-commit
57
pytest
68
pytest-cov

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
packages=find_packages(exclude=['tests/*']),
1212
install_requires=[
1313
'mypy>=0.660',
14+
# TODO: update version after https://github.com/pynamodb/PynamoDB/pull/579 is released
15+
'pynamodb',
1416
],
1517
python_requires='>=3',
1618
)

tests/test_plugin.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,41 @@ def test_unicode_attribute():
2424
from pynamodb.models import Model
2525
2626
class MyModel(Model):
27-
my_attr = UnicodeAttribute(null=True)
27+
my_attr = UnicodeAttribute()
28+
my_nullable_attr = UnicodeAttribute(null=True)
2829
29-
reveal_type(MyModel.my_attr) # E: Revealed type is 'pynamodb.attributes._NullableAttribute[pynamodb.attributes.UnicodeAttribute, builtins.str]'
30-
reveal_type(MyModel().my_attr) # E: Revealed type is 'Union[builtins.str*, None]'
31-
MyModel().my_attr.lower() # E: Item "None" of "Optional[str]" has no attribute "lower"
32-
""") # noqa: E501
30+
reveal_type(MyModel.my_attr) # E: Revealed type is 'pynamodb.attributes.UnicodeAttribute'
31+
reveal_type(MyModel.my_nullable_attr) # E: Revealed type is 'pynamodb.attributes.UnicodeAttribute*'
32+
reveal_type(MyModel().my_attr) # E: Revealed type is 'builtins.str*'
33+
reveal_type(MyModel().my_nullable_attr) # E: Revealed type is 'Union[builtins.str*, None]'
34+
35+
MyModel().my_attr.lower()
36+
MyModel().my_nullable_attr.lower() # E: Item "None" of "Optional[str]" has no attribute "lower"
37+
""")
3338

3439

35-
def test_custom_type():
40+
def test_custom_attribute():
3641
assert_mypy_output("""
3742
from pynamodb.attributes import Attribute
3843
from pynamodb.models import Model
3944
4045
class BinaryAttribute(Attribute[bytes]):
41-
pass
46+
def do_something(self) -> None: ...
4247
4348
class MyModel(Model):
44-
my_attr = BinaryAttribute(null=True)
45-
46-
reveal_type(MyModel().my_attr) # E: Revealed type is 'Union[builtins.bytes*, None]'
47-
""")
49+
my_attr = BinaryAttribute()
50+
my_nullable_attr = BinaryAttribute(null=True)
51+
52+
reveal_type(MyModel.my_attr) # E: Revealed type is '__main__.BinaryAttribute'
53+
reveal_type(MyModel.my_attr.exists) # E: Revealed type is 'def () -> pynamodb.expressions.condition.Exists'
54+
reveal_type(MyModel.my_attr.do_something) # E: Revealed type is 'def ()'
55+
reveal_type(MyModel().my_attr) # E: Revealed type is 'builtins.bytes*'
56+
57+
reveal_type(MyModel.my_nullable_attr) # E: Revealed type is '__main__.BinaryAttribute*'
58+
reveal_type(MyModel.my_nullable_attr.exists) # E: Revealed type is 'def () -> pynamodb.expressions.condition.Exists'
59+
reveal_type(MyModel.my_nullable_attr.do_something) # E: Revealed type is 'def ()'
60+
reveal_type(MyModel().my_nullable_attr) # E: Revealed type is 'Union[builtins.bytes*, None]'
61+
""") # noqa: E501
4862

4963

5064
def test_unexpected_number_of_nulls():

0 commit comments

Comments
 (0)