In this contrived example, I want pyright to accept that using a Decimal instead of a float is fine (I am aware of the limitations, but this is what is needed and I am fine with the consequences).
from decimal import Decimal
d = Decimal(1.0)
f: float = d
Pyright will say on the line f: float = d
:
Expression of type "Decimal" cannot be assigned to declared type "float"
"Decimal" is incompatible with "float" Pylance(reportGeneralTypeIssues)
The options I know of are:
- ignore
reportGeneralTypeIssues
completely (that would remove a lot of the power of Pyright, even inside one file) - ignore
reportGeneralTypeIssues
on each line where I have the Decimal/float conversion (visually very very polluting in my codebase)
Is there a way to tell "Ignore reportGeneralTypeIssues
errors related to Decimal -> float conversions"?
Edit:
I know that there will be conversion happening down the line in my code (I am basically working with 2 types of pydantic models: one with Decimal, one with float), so I am basically looking for a no boilerplate option (yes, I could convert all Decimals to float manually, but that's not what I am looking for).
There are several approaches to this, so I'll list a couple below:
1. Convert the type
This is the most straight-forward, we simply convert the object to a float prior to use. I would recommend this because then the types actually reflect the objects used. The entire point of using types is so that objects you use have the properties you expect.
2. Masking the type
This way is not ideal, but you can (for no runtime cost) mask what type is really being used behind a "cast".
This just overrides the type of the item effectively.
Syntax
You could wrap either of these in a function for brevity e.g.:
With the above syntax you can also make use almost exactly the same, with a change to the import line:
new_decimal.py
used_file.py
Hope this helps!