Skip to content

Commit

Permalink
refactor: Add a new constructor
Browse files Browse the repository at this point in the history
refactor: Initialize options

refactor: Ignore constructor call

fix: Add Optional

refactor: Reuse th constructor

refactor: Use public parameter
  • Loading branch information
Sarrabah committed Jul 25, 2024
1 parent aee39a7 commit 3116b30
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/frformat/custom_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class CustomFormat(ABC, Generic[ValueType]):
metadata: Metadata
formatter: Formatter = DefaultFormatter[ValueType]()

def __init__(self):
...

@abstractmethod
def is_valid(self, value: ValueType) -> bool:
...
Expand Down
16 changes: 9 additions & 7 deletions src/frformat/enum_format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Set, Type
from typing import Optional, Set, Type

from frformat import CustomStrFormat, Metadata
from frformat.common import normalize_value
Expand All @@ -11,20 +11,22 @@ class EnumFormat(CustomStrFormat):
May preprocess the input and valid values according to given "options"."""

def __init__(self, options: Options):
self._options = options
def __init__(self, options: Optional[Options] = None):
self.options = options if options is not None else Options()

_normalized_extra_values = {
normalize_value(e, self._options)
for e in self._options.extra_valid_values
normalize_value(e, self.options)
for e in self.options.extra_valid_values
}

self._normalized_enum = {
normalize_value(e, self._options) for e in enum
normalize_value(e, self.options) for e in enum
}.union(_normalized_extra_values)

metadata = Metadata(name, description)

def is_valid(self, value: str) -> bool:
normalized_value = normalize_value(value, self._options)
normalized_value = normalize_value(value, self.options)
return normalized_value in self._normalized_enum

EnumFormat.__name__ = class_name
Expand Down
5 changes: 2 additions & 3 deletions src/tests/test_geo_fr.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
Region,
)
from frformat.common import NBSP, NNBSP
from frformat.options import Options
from tests.testing import strict_lenient_test_helper_factory


Expand All @@ -29,7 +28,7 @@ def test_code_fantoir():

def test_code_commune_insee():
value = "01015"
code_commune_insee = CodeCommuneInsee(Options())
code_commune_insee = CodeCommuneInsee()
assert code_commune_insee.is_valid(value)
assert code_commune_insee.format(value) == value

Expand All @@ -39,7 +38,7 @@ def test_code_commune_insee():

def test_code_postal():
value = "05560"
code_postal = CodePostal(Options())
code_postal = CodePostal()
assert code_postal.is_valid(value)
assert code_postal.format(value) == value

Expand Down

0 comments on commit 3116b30

Please sign in to comment.