Skip to content

Commit c8ac15b

Browse files
henrykraphaelm
authored andcommitted
Implement more generic Field types
Cleanup warnings in test
1 parent 9f65c1a commit c8ac15b

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

fints/formals.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ def _parse_value(self, value):
275275

276276
class FloatField(FieldRenderFormatStringMixin, DataElementField):
277277
type = 'float'
278+
## FIXME: Not implemented, no one uses this?
278279

279280
class BinaryField(DataElementField):
280281
type = 'bin'
@@ -287,6 +288,56 @@ def _render_value(self, value):
287288

288289
def _parse_value(self, value): return bytes(value)
289290

291+
class FixedLengthMixin:
292+
FIXED_LENGTH = [None, None, None]
293+
294+
def __init__(self, *args, **kwargs):
295+
for i, a in enumerate(('length', 'min_length', 'max_length')):
296+
kwargs[a] = self.FIXED_LENGTH[i] if len(self.FIXED_LENGTH) > i else None
297+
298+
super().__init__(*args, **kwargs)
299+
300+
class IDField(FixedLengthMixin, AlphanumericField):
301+
type = 'id'
302+
FIXED_LENGTH = [None, None, 30]
303+
304+
class BooleanField(FixedLengthMixin, AlphanumericField):
305+
type = 'jn'
306+
FIXED_LENGTH = [1]
307+
308+
def _render_field(self, value):
309+
return "J" if value else "N"
310+
311+
def _parse_field(self, value):
312+
if value is None:
313+
return None
314+
if value == "J":
315+
return True
316+
elif value == "N":
317+
return False
318+
else:
319+
raise ValueError("Invalid value {!r} for BooleanField".format(value))
320+
321+
class CodeField(AlphanumericField):
322+
type = 'code'
323+
324+
## FIXME: Not further implemented, might want to use Enums
325+
326+
class CountryField(FixedLengthMixin, DigitsField):
327+
type = 'ctr'
328+
FIXED_LENGTH = [3]
329+
330+
class CurrencyField(FixedLengthMixin, AlphanumericField):
331+
type = 'cur'
332+
FIXED_LENGTH = [3]
333+
334+
class DateField(FixedLengthMixin, NumericField):
335+
type = 'dat'
336+
FIXED_LENGTH = [8]
337+
338+
class TimeField(FixedLengthMixin, DigitsField):
339+
type = 'tim'
340+
FIXED_LENGTH = [6]
290341

291342
class SegmentSequence:
292343
def __init__(self, segments = None):

tests/test_formals.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,4 +331,5 @@ class A(Container):
331331

332332
assert i1.a
333333

334-
i2 = A(a=[])
334+
with pytest.warns(UserWarning, match=r'Generic field used for type None value \[\]'):
335+
i2 = A(a=[])

0 commit comments

Comments
 (0)