Skip to content

Commit a20f297

Browse files
committed
Lazy import urllib
This change improves speed by roughly ~30ms, from 77ms to 45ms with Python 3.10 on my machine.
1 parent aa7be27 commit a20f297

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

fastjsonschema/ref_resolver.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@
77
"""
88

99
import contextlib
10-
import json
1110
import re
1211
from urllib import parse as urlparse
13-
from urllib.parse import unquote
14-
from urllib.request import urlopen
1512

1613
from .exceptions import JsonSchemaDefinitionException
1714

@@ -29,6 +26,8 @@ def resolve_path(schema, fragment):
2926
3027
Path is unescaped according https://tools.ietf.org/html/rfc6901
3128
"""
29+
from urllib.parse import unquote
30+
3231
fragment = fragment.lstrip('/')
3332
parts = unquote(fragment).split('/') if fragment else []
3433
for part in parts:
@@ -55,6 +54,9 @@ def resolve_remote(uri, handlers):
5554
urllib library is used to fetch requests from the remote ``uri``
5655
if handlers does notdefine otherwise.
5756
"""
57+
import json
58+
from urllib.request import urlopen
59+
5860
scheme = urlparse.urlsplit(uri).scheme
5961
if scheme in handlers:
6062
result = handlers[scheme](uri)
@@ -149,6 +151,8 @@ def get_scope_name(self):
149151
"""
150152
Get current scope and return it as a valid function name.
151153
"""
154+
from urllib.parse import unquote
155+
152156
name = 'validate_' + unquote(self.resolution_scope).replace('~1', '_').replace('~0', '_').replace('"', '')
153157
name = re.sub(r'($[^a-zA-Z]|[^a-zA-Z0-9])', '_', name)
154158
name = name.lower().rstrip('_')

0 commit comments

Comments
 (0)