Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move alias check before custom handler check #1515

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version next
--------------

- Fix for custom handler with alias not working.

Version 15.1.0
--------------

Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
6 changes: 3 additions & 3 deletions mimesis/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down