Skip to main content

Mypy plugin to prevent passing float type to Decimal to avoid imprecisions.

Project description

Mypy non-float Decimal plugin

Restricts passing float numbers to Decimal

Why?

The implementation of floating point numbers can cause imprecisions in results. To avoid this problem you can use Decimal type, however you still need to avoid passing float as its parameter:

>>> Decimal(1.02)
Decimal("1.020000000000000017763568394002504646778106689453125")
>>> Decimal("1.02")
Decimal("1.02")

This plugin is meant to spot occurrences where float is passed to Decimal in your code.

Usage

  • install plugin
pip install mypy-nonfloat-decimal
  • add it into list of mypy plugins in your mypy config (mypy.ini)
[mypy]
plugins=mypy_nonfloat_decimal
  • upon running mypy will detect float passed to Decimal and report it as an error (example.py):
from decimal import Decimal
Decimal(1.02)
$ mypy --config-file ./mypy.ini ./example.py

example.py:2: error: Invalid type passed to Decimal (expected "Union[int, str, Decimal]"), got float instead

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

mypy-nonfloat-decimal-0.1.6.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

mypy_nonfloat_decimal-0.1.6-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file mypy-nonfloat-decimal-0.1.6.tar.gz.

File metadata

  • Download URL: mypy-nonfloat-decimal-0.1.6.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.9.1 Darwin/20.6.0

File hashes

Hashes for mypy-nonfloat-decimal-0.1.6.tar.gz
Algorithm Hash digest
SHA256 8231407c60de60d05e096c66927da0eac5291b0e74db5b6441fd375f7d1a003a
MD5 87898433a171cb919bf289e632c599c7
BLAKE2b-256 5fa5f8d173d171526bbf5317885e8fb07fde134aca047b4de0f787230a941752

See more details on using hashes here.

File details

Details for the file mypy_nonfloat_decimal-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for mypy_nonfloat_decimal-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 510cbe00625b2db06d9a6dffbd09a839c9621a5c7ebdb023978a692b831a1bee
MD5 840aee1ac0a64d25d50d796edf2175dc
BLAKE2b-256 5eb77cc3e76d8b35e2b9a2179c1911e2129ef408aa7b926b58e9330d35efe41d

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