Skip to content

Commit 9d599c7

Browse files
committed
Reduce import time
1 parent 8a01483 commit 9d599c7

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

hypothesis-python/src/hypothesis/provisional.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# https://tools.ietf.org/html/rfc3696
2020

2121
import string
22+
from functools import lru_cache
2223
from importlib import resources
2324
from typing import Optional
2425

@@ -31,15 +32,17 @@
3132
FRAGMENT_SAFE_CHARACTERS = URL_SAFE_CHARACTERS | {"?", "/"}
3233

3334

34-
# This file is sourced from http://data.iana.org/TLD/tlds-alpha-by-domain.txt
35-
# The file contains additional information about the date that it was last updated.
36-
traversable = resources.files("hypothesis.vendor") / "tlds-alpha-by-domain.txt"
37-
_comment, *_tlds = traversable.read_text(encoding="utf-8").splitlines()
38-
assert _comment.startswith("#")
35+
@lru_cache(maxsize=1)
36+
def get_top_level_domains() -> tuple[str, ...]:
37+
# This file is sourced from http://data.iana.org/TLD/tlds-alpha-by-domain.txt
38+
# The file contains additional information about the date that it was last updated.
39+
traversable = resources.files("hypothesis.vendor") / "tlds-alpha-by-domain.txt"
40+
_comment, *_tlds = traversable.read_text(encoding="utf-8").splitlines()
41+
assert _comment.startswith("#")
3942

40-
# Remove special-use domain names from the list. For more discussion
41-
# see https://github.com/HypothesisWorks/hypothesis/pull/3572
42-
TOP_LEVEL_DOMAINS = ["COM", *sorted((d for d in _tlds if d != "ARPA"), key=len)]
43+
# Remove special-use domain names from the list. For more discussion
44+
# see https://github.com/HypothesisWorks/hypothesis/pull/3572
45+
return ("COM", *sorted((d for d in _tlds if d != "ARPA"), key=len))
4346

4447

4548
class DomainNameStrategy(st.SearchStrategy):
@@ -101,7 +104,7 @@ def do_draw(self, data):
101104
# prevent us from generating at least a 1 character subdomain.
102105
# 3 - Randomize the TLD between upper and lower case characters.
103106
domain = data.draw(
104-
st.sampled_from(TOP_LEVEL_DOMAINS)
107+
st.sampled_from(get_top_level_domains())
105108
.filter(lambda tld: len(tld) + 2 <= self.max_length)
106109
.flatmap(
107110
lambda tld: st.tuples(

0 commit comments

Comments
 (0)