-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refine stubs for Python 2's decimal module #545
Refine stubs for Python 2's decimal module #545
Conversation
9d752e7
to
283dfed
Compare
The decimal module for Python 2 was relatively incomplete, unlike the decimal module for Python 3. This commit copies the relevant type signatures from Python 3's decimal module to Python 2's. There was a lot of code in both stubs and it wasn't clear to me if it was safe to merge the two modules together, so I refrained from doing so.
283dfed
to
e792393
Compare
ROUND_UP = ... # type: Any | ||
ROUND_HALF_DOWN = ... # type: Any | ||
ROUND_05UP = ... # type: Any | ||
_Decimal = Union[Decimal, int] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think modern Decimal is lenient about floats and just accepts them. I recommend just adding float here, else you have to add it to many other places (in our server code I found problems comparing floats to decimals.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Should I also make that change in the Python 3's version of Decimal? (I copied _Decimal
from the current Python 3 stubs).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this needs more nuance. IIUC comparisons with floats are acceptable, but not e.g. addition. So maybe you need a more lenient union type just for comparisons.
This commit loosens the types for Decimals to allow comparisons like `Decimal('3.14') < 4.2`. Previously, you could compare decimals with only other decimals or ints.
@gvanrossum -- ok, I made comparisons more lenient. Try now? |
I think this is a good improvement and I think it's found some nonsense on our side! |
The decimal module for Python 2 was relatively incomplete, unlike the decimal module for Python 3. This commit copies the relevant type signatures from Python 3's decimal module to Python 2's (and slightly tweaks them to match the Python 2 function definitions in a few places).
There was a lot of code in both stubs and it wasn't clear to me if it was safe to merge the two modules together, so I refrained from doing so.