Skip to content
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

Merged
merged 2 commits into from
Sep 14, 2016

Conversation

Michael0x2a
Copy link
Contributor

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.

@Michael0x2a Michael0x2a force-pushed the refine-python2-decimal-stubs branch 3 times, most recently from 9d752e7 to 283dfed Compare September 14, 2016 05:43
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.
ROUND_UP = ... # type: Any
ROUND_HALF_DOWN = ... # type: Any
ROUND_05UP = ... # type: Any
_Decimal = Union[Decimal, int]
Copy link
Member

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.

Copy link
Contributor Author

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).

Copy link
Member

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.
@Michael0x2a
Copy link
Contributor Author

@gvanrossum -- ok, I made comparisons more lenient. Try now?

@gvanrossum gvanrossum merged commit 748428d into python:master Sep 14, 2016
@gvanrossum
Copy link
Member

I think this is a good improvement and I think it's found some nonsense on our side!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants