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.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pynamodb-mypy-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 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.0.tar.gz
Algorithm Hash digest
SHA256 e7bf0437a5d1c2b18c72a3fa4d5fa0c236df7ebce194423095980133e9c2b1c5
MD5 1bb7336cac662d831f536e2fbe930508
BLAKE2b-256 0b08947bf54a4f8a1a47386d0234f96ae475c709421728134c5ef7a919ebb8e9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pynamodb_mypy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 76f1f2eb3dc5f09f3eaa38e8c8f8690c031657937a3eb00d6a04c901594c9e0b
MD5 fbbe6b878d922b6b73853402948fb6db
BLAKE2b-256 24a0ea33132ed958a438d795ff49e92ef7cf4e9998ab408f01026b8329f8ba54

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