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
floatpassed toDecimaland 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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mypy_nonfloat_decimal-0.1.8.tar.gz.
File metadata
- Download URL: mypy_nonfloat_decimal-0.1.8.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
077b3b53815ff128ba83a41370b7f6c917aa9b3d9b548a3ffce96996e442d0e2
|
|
| MD5 |
8c4fdbab88f4cc1f90b452f0d583bd32
|
|
| BLAKE2b-256 |
5fb90303fbdf4cdd3eb4aee5bd01942e8cb9b6f92bb25b4a1e651b73a9245e42
|
File details
Details for the file mypy_nonfloat_decimal-0.1.8-py3-none-any.whl.
File metadata
- Download URL: mypy_nonfloat_decimal-0.1.8-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.12.2 Darwin/24.3.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4ac8a97cc4463522709ee02bd3cf74d4f61ff851be19b1dc1d513a2fb8f2041
|
|
| MD5 |
9cf06713496e590e980a70d28175c6ac
|
|
| BLAKE2b-256 |
204225de227cfa70b44d019dac1c564b5f94b71e4ddd3b47af7abe0e989107ee
|