Skip to content

Commit 45f84b9

Browse files
committed
Halve import time
1 parent 6192cd1 commit 45f84b9

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

src/mdurl/_decode.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from __future__ import annotations
22

3-
from collections.abc import Sequence
43
import functools
54
import re
65

6+
TYPE_CHECKING = False
7+
if TYPE_CHECKING:
8+
from collections.abc import Sequence
9+
10+
711
DECODE_DEFAULT_CHARS = ";/?:@&=+$,#"
812
DECODE_COMPONENT_CHARS = ""
913

src/mdurl/_encode.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
from __future__ import annotations
22

3-
from collections.abc import Sequence
4-
from string import ascii_letters, digits, hexdigits
53
from urllib.parse import quote as encode_uri_component
64

7-
ASCII_LETTERS_AND_DIGITS = ascii_letters + digits
5+
TYPE_CHECKING = False
6+
if TYPE_CHECKING:
7+
from collections.abc import Sequence
8+
9+
ASCII_LETTERS_AND_DIGITS = (
10+
"abcdefghijklmnopqrstuvwxyz" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "0123456789"
11+
)
12+
HEXDIGITS = "0123456789" "abcdef" "ABCDEF"
813

914
ENCODE_DEFAULT_CHARS = ";/?:@&=+$,-_.!~*'()#"
1015
ENCODE_COMPONENT_CHARS = "-_.!~*'()"
@@ -56,7 +61,7 @@ def encode(
5661

5762
# %
5863
if keep_escaped and code == 0x25 and i + 2 < l:
59-
if all(c in hexdigits for c in string[i + 1 : i + 3]):
64+
if all(c in HEXDIGITS for c in string[i + 1 : i + 3]):
6065
result += string[i : i + 3]
6166
i += 2
6267
i += 1 # JS for loop statement3

src/mdurl/_format.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import TYPE_CHECKING
4-
3+
TYPE_CHECKING = False
54
if TYPE_CHECKING:
65
from mdurl._url import URL
76

src/mdurl/_url.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,32 @@
11
from __future__ import annotations
22

3-
from typing import NamedTuple
4-
5-
6-
class URL(NamedTuple):
7-
protocol: str | None
8-
slashes: bool
9-
auth: str | None
10-
port: str | None
11-
hostname: str | None
12-
hash: str | None # noqa: A003
13-
search: str | None
14-
pathname: str | None
3+
from collections import namedtuple
4+
5+
TYPE_CHECKING = False
6+
if TYPE_CHECKING:
7+
from typing import NamedTuple
8+
9+
class URL(NamedTuple):
10+
protocol: str | None
11+
slashes: bool
12+
auth: str | None
13+
port: str | None
14+
hostname: str | None
15+
hash: str | None # noqa: A003
16+
search: str | None
17+
pathname: str | None
18+
19+
else:
20+
URL = namedtuple(
21+
"URL",
22+
[
23+
"protocol",
24+
"slashes",
25+
"auth",
26+
"port",
27+
"hostname",
28+
"hash",
29+
"search",
30+
"pathname",
31+
],
32+
)

0 commit comments

Comments
 (0)