From ac196115401c4a3d8189289b48c2fe1968825f9f Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Tue, 19 Mar 2024 17:31:23 -0400 Subject: [PATCH 1/3] Move alias check before custom handler check --- mimesis/schema.py | 6 +++--- tests/test_schema.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/mimesis/schema.py b/mimesis/schema.py index 3d3a4f60f..c25fbf0fc 100644 --- a/mimesis/schema.py +++ b/mimesis/schema.py @@ -112,9 +112,6 @@ def _lookup_method(self, name: str) -> Any: :return: Callable object. :raise FieldError: When field is invalid. """ - # Check if the field is defined in aliases - name = self.aliases.get(name, name) - # Support additional delimiters name = re.sub(r"[/:\s]", ".", name) @@ -190,6 +187,9 @@ def perform( random = self.get_random_instance() + # Check if the field is defined in aliases + name = self.aliases.get(name, name) + # First, try to find a custom field handler. if name in self._handlers: result = self._handlers[name](random, **kwargs) # type: ignore diff --git a/tests/test_schema.py b/tests/test_schema.py index 74527d605..ecb0356fa 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -368,6 +368,17 @@ def __call__(self, random, a="b", c="d", **kwargs): return random.choice([a, c]) +def test_register_handler_with_alias(default_field: "Field"): + default_field.register_handler("bloop", my_field_handler) + default_field.aliases.update({"zoop": "bloop"}) + + assert isinstance(default_field("zoop"), str) + assert isinstance(default_field("bloop"), str) + + default_field.unregister_handler("bloop") + default_field.aliases.pop("zoop") + + @pytest.mark.parametrize( "field_name, handler", [ From 842a627e8482dfcc2e1b4cf455cb6b783f29ba2c Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Tue, 19 Mar 2024 17:33:58 -0400 Subject: [PATCH 2/3] Add to contributors --- CONTRIBUTORS.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 405ba6427..993acac1f 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -93,6 +93,7 @@ Patches and Suggestions - Florian Kroiß `(Wooza)`_ - Han Wang `(freddiewanah)`_ - David Gorup `(CerealKiller0807)`_ +- Matthew Heguy `(mheguy-flo)`_ .. _(lk-geimfari): https://github.com/lk-geimfari @@ -164,3 +165,4 @@ Patches and Suggestions .. _(Wooza): https://github.com/Wooza .. _(freddiewanah): https://github.com/freddiewanah .. _(CerealKiller0807): https://github.com/CerealKiller0807 +.. _(mheguy-flo): https://github.com/mheguy-flo From e8186c64a674f9388691bd1f721da4563a4e9523 Mon Sep 17 00:00:00 2001 From: Matthew Heguy Date: Tue, 19 Mar 2024 17:35:44 -0400 Subject: [PATCH 3/3] Add to changelog --- CHANGELOG.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e4810fefc..63d63605a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,3 +1,8 @@ +Version next +-------------- + +- Fix for custom handler with alias not working. + Version 15.1.0 --------------