Skip to content

Commit

Permalink
Fix bug in handling of deprecated specifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
avylove committed Nov 3, 2022
1 parent a28890d commit 079a268
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions prefixed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import re
import sys

__version__ = '0.4.0'
__version__ = '0.4.1'

try:
BASESTRING = basestring
Expand Down Expand Up @@ -104,6 +104,7 @@ def raise_from_none(exc): # pragma: no cover
else:
from string import maketrans # pragma: no cover

DEPRECATED = {'j', 'J'}
TRANS_DEPRECATED = maketrans('jJ', 'km')


Expand Down Expand Up @@ -299,13 +300,14 @@ def __format__(self, format_spec):

spec = match.groupdict()

# Handle deprecated spec types
if spec['type'] in DEPRECATED:
spec['type'] = spec['type'].translate(TRANS_DEPRECATED)

# If not a spec we handle, use float.__format__(()
if spec['type'] not in {'h', 'H', 'k', 'K', 'm', 'M'}:
return super(Float, self).__format__(format_spec)

# Handle deprecated spec types
spec['type'] = spec['type'].translate(TRANS_DEPRECATED)

# Determine value and prefix
value, prefix, spec = _convert(float(self), spec)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,14 @@ def test_margin(self):
self.assertEqual(format(Float(1049), '%5.2h'), '1049.00')
self.assertEqual(format(Float(1050), '%5.2h'), '1.05k')

def test_deprecated(self):
"""
Confirm deprecated format specifiers function
"""

self.assertEqual(format(Float(2048), '.2j'), '2.00Ki')
self.assertEqual(format(Float(2048), '.2J'), '2.00K')


class TestFloatMath(unittest.TestCase):
"""
Expand Down

0 comments on commit 079a268

Please sign in to comment.