Skip to main content

mypy plugin for PynamoDB

Project description

pynamodb-mypy

A plugin for mypy which gives it deeper understanding of PynamoDB (beyond what's possible through type stubs).

Usage

Add it to the plugins option of the [mypy] section in your mypy.ini:

[mypy]
plugins = pynamodb_mypy

Nullable attributes

When declaring attributes with null=True, the value types would become optional. For example:

from pynamodb.attributes import UnicodeAttribute
from pynamodb.models import Model

class MyModel(Model):
  my_key = UnicodeAttribute()
  my_value = UnicodeAttribute(null=True)

...
my_model = MyModel.get('key')
if my_model.my_value.lower() == 'foo':  # error: Item "None" of "Optional[str]" has no attribute "lower"
   print("Value is foo")

would have to be changed to, e.g.:

if my_model.my_value and my_model.my_value.lower() == 'foo':
   print("Value is foo")

Typed model initializers

When declaring models, the __init__ method would be typed to accept only the attributes declared in the model. For example:

from pynamodb.models import Model
from pynamodb.attributes import NumberAttribute
from pynamodb.attributes import UnicodeAttribute

class MyModel(Model):
  my_key = UnicodeAttribute(hash_key=True)
  my_value = NumberAttribute(null=True)

# Existing attributes would be enforced:
_ = MyModel(my_key='key', my_value=42, my_other_value='other_value')  # error: Unexpected keyword argument "my_other_value" for "MyModel"

# Typing would be enforced:
_ = MyModel(my_key='key', my_value='42')  # error: Argument 2 to "MyModel" has incompatible type "str"; expected "Optional[int]"

# Nullability will be enforced::
_ = MyModel(my_key='key', my_value=None)
_ = MyModel(my_key=None, my_value=None)  # error: Argument "my_key" to "MyModel" has incompatible type "None"; expected "str"

# The hash key and range key can also be passed as positional arguments:
_ = MyModel('key')
_ = MyModel(42)  # error: Argument 1 to "MyModel" has incompatible type "int"; expected "str"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pynamodb-mypy-0.1.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

pynamodb_mypy-0.1.1-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file pynamodb-mypy-0.1.1.tar.gz.

File metadata

  • Download URL: pynamodb-mypy-0.1.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for pynamodb-mypy-0.1.1.tar.gz
Algorithm Hash digest
SHA256 6ebc494622152fc6fca01c70a6be3267e1db44d47415d8d53c9f0d754ec00f69
MD5 12651aa69144c6f0a685b3376daa3f1a
BLAKE2b-256 8eca0cb02f46d64bad9f7cdb7028bff57bb27a11b0c84eed59740660483d733b

See more details on using hashes here.

File details

Details for the file pynamodb_mypy-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pynamodb_mypy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0100746decdd06b0c5b31a3d77dacde417a733e5bc2c51c3b099225cc305743e
MD5 2e3dc8a1f585939f7452264cbb0a18db
BLAKE2b-256 6cccb3801c39507abf6b9bea180bee4414ab9ebc8abb74ba9c8b5759f6267298

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page