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

Defer importing ens._normalization to speed up web3 import by 27% #3285

Merged
merged 2 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions ens/utils.py
Expand Up @@ -35,9 +35,6 @@
HexBytes,
)

from ._normalization import (
normalize_name_ensip15,
)
from .constants import (
ACCEPTABLE_STALE_HOURS,
AUCTION_START_GAS_CONSTANT,
Expand Down Expand Up @@ -124,6 +121,11 @@ def normalize_name(name: str) -> str:
:param str name: the dot-separated ENS name
:raises InvalidName: if ``name`` has invalid syntax
"""
# Defer import because module initialization takes > 0.1 ms
from ._normalization import (
normalize_name_ensip15,
)

if is_empty_name(name):
return ""
elif isinstance(name, (bytes, bytearray)):
Expand Down
8 changes: 6 additions & 2 deletions tests/ens/test_ens.py
Expand Up @@ -118,7 +118,9 @@ def test_ens_methods_normalize_name(
# normalizes the full name and each label
expected_call_args = {"tester.eth", "tester", "eth"}

with patch("ens.utils.normalize_name_ensip15") as mock_normalize_name_ensip15:
with patch(
"ens._normalization.normalize_name_ensip15"
) as mock_normalize_name_ensip15:
mock_normalize_name_ensip15.side_effect = normalize_name_ensip15

# test setup address while appropriately setting up the test
Expand Down Expand Up @@ -243,7 +245,9 @@ async def test_async_ens_methods_normalize_name_with_ensip15(
# normalizes the full name and each label
expected_call_args = {"tester.eth", "tester", "eth"}

with patch("ens.utils.normalize_name_ensip15") as mock_normalize_name_ensip15:
with patch(
"ens._normalization.normalize_name_ensip15"
) as mock_normalize_name_ensip15:
mock_normalize_name_ensip15.side_effect = normalize_name_ensip15

# test setup address while appropriately setting up the test
Expand Down
8 changes: 6 additions & 2 deletions tests/ens/test_utils.py
Expand Up @@ -174,7 +174,9 @@ def test_normal_name_to_hash(name, hashed):
def test_name_utility_methods_normalize_the_name_using_ensip15(utility_method):
# we already have tests for `normalize_name_ensip15` so we just need to make sure
# the utility methods call it under the hood with the correct arguments
with patch("ens.utils.normalize_name_ensip15") as normalize_name_ensip15_mock:
with patch(
"ens._normalization.normalize_name_ensip15"
) as normalize_name_ensip15_mock:
utility_method("foo.eth")
normalize_name_ensip15_mock.assert_called_once_with("foo.eth")

Expand All @@ -188,7 +190,9 @@ def test_label_to_hash_normalizes_name_using_ensip15():
)
assert normalized_name.as_text == "foo.eth"

with patch("ens.utils.normalize_name_ensip15") as mock_normalize_name_ensip15:
with patch(
"ens._normalization.normalize_name_ensip15"
) as mock_normalize_name_ensip15:
for label in normalized_name.labels:
mock_normalize_name_ensip15.return_value = ENSNormalizedName([label])

Expand Down