python-babel / babel

The official repository for Babel, the Python Internationalization Library
http://babel.pocoo.org/
BSD 3-Clause "New" or "Revised" License
1.32k stars 440 forks source link

Support for formatting NaN, Infinity #955

Closed DenverCoder1 closed 1 year ago

DenverCoder1 commented 1 year ago

This came up while I was looking for ways to improve type-safety - specifically that the type of decimal_tuple.exponent in get_decimal_precision() is int | Literal['n', 'N', 'F'] and can't be compared with 0 or taken the absolute value of if it is not an int.

This fixes that type issue but I'm making this a separate PR since it also fixes NaN and Infinity cases in other areas, so it won't raise exceptions when formatting.

Fixes #132

>>> format_decimal(decimal.Decimal('Infinity'))
'∞'
>>> format_decimal(decimal.Decimal('-Infinity'))
'-∞'
>>> format_decimal(decimal.Decimal('NaN'))
'NaN'
codecov[bot] commented 1 year ago

Codecov Report

Merging #955 (e87cc84) into master (2a4b784) will increase coverage by 0.01%. The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #955      +/-   ##
==========================================
+ Coverage   90.48%   90.50%   +0.01%     
==========================================
  Files          24       24              
  Lines        4393     4402       +9     
==========================================
+ Hits         3975     3984       +9     
  Misses        418      418              
Impacted Files Coverage Δ
babel/numbers.py 98.04% <100.00%> (+0.04%) :arrow_up:

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more