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 toDecimal
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
Built Distribution
File details
Details for the file mypy-nonfloat-decimal-0.1.5.tar.gz
.
File metadata
- Download URL: mypy-nonfloat-decimal-0.1.5.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.9.1 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48a0fe73eecc79327493c801831f91bee5edca91036011eed9a62f9f9cb82fef |
|
MD5 | 7723f37fcd87c92e1a8c913f00b3ce2a |
|
BLAKE2b-256 | 1048e80c685de628b290c7244f8ecf0e40911ec9a62f6460f50a5909441323f1 |
File details
Details for the file mypy_nonfloat_decimal-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: mypy_nonfloat_decimal-0.1.5-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.9.1 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 999d6e58b35070f1c346a58977fc8f6b60c6b4e1d24a6ed83440ca19098a4e47 |
|
MD5 | 198ecb961f5406e55e6723e55553a8d9 |
|
BLAKE2b-256 | 2dd84ce2441ad6ee1415f1a98e336e44298c7124530b42c58126c34f4836d515 |