1+ from __future__ import annotations
2+
13import bisect
24import re
35import unicodedata
46import warnings
5- from typing import Optional , Union
67
78from . import idnadata
89from .intranges import intranges_contain
2526_bidi_joiner_r_or_d = frozenset ({"R" , "D" })
2627
2728
28- def _joining_type (cp : int ) -> Optional [ str ] :
29+ def _joining_type (cp : int ) -> str | None :
2930 for jt , ranges in idnadata .joining_types .items ():
3031 if intranges_contain (cp , ranges ):
3132 return jt
@@ -67,7 +68,7 @@ def _unot(s: int) -> str:
6768 return f"U+{ s :04X} "
6869
6970
70- def valid_label_length (label : Union [ bytes , str ] ) -> bool :
71+ def valid_label_length (label : bytes | str ) -> bool :
7172 """Check that a label does not exceed the maximum permitted length.
7273
7374 Per :rfc:`1035` (and :rfc:`5891` §4.2.4) a DNS label must not exceed
@@ -82,7 +83,7 @@ def valid_label_length(label: Union[bytes, str]) -> bool:
8283 return len (label ) <= 63
8384
8485
85- def valid_string_length (domain : Union [ bytes , str ] , trailing_dot : bool ) -> bool :
86+ def valid_string_length (domain : bytes | str , trailing_dot : bool ) -> bool :
8687 """Check that a full domain name does not exceed the maximum length.
8788
8889 Per :rfc:`1035`, a domain name is limited to 253 octets when no trailing
@@ -136,7 +137,7 @@ def check_bidi(label: str, check_ltr: bool = False) -> bool:
136137 raise IDNABidiError (f"First codepoint in label { label !r} must be directionality L, R or AL" )
137138
138139 valid_ending = False
139- number_type : Optional [ str ] = None
140+ number_type : str | None = None
140141 for idx , cp in enumerate (label , 1 ):
141142 direction = unicodedata .bidirectional (cp )
142143
@@ -319,7 +320,7 @@ def valid_contexto(label: str, pos: int, exception: bool = False) -> bool:
319320 return False
320321
321322
322- def check_label (label : Union [ str , bytes , bytearray ] ) -> None :
323+ def check_label (label : str | bytes | bytearray ) -> None :
323324 """Run the full set of IDNA 2008 validity checks on a single label.
324325
325326 Applies, in order: NFC normalisation (:func:`check_nfc`), hyphen
@@ -409,7 +410,7 @@ def alabel(label: str) -> bytes:
409410 return label_bytes
410411
411412
412- def ulabel (label : Union [ str , bytes , bytearray ] ) -> str :
413+ def ulabel (label : str | bytes | bytearray ) -> str :
413414 """Convert a single A-label into its U-label form.
414415
415416 Performs the inverse of :func:`alabel`: an ``xn--``-prefixed label is
@@ -482,7 +483,7 @@ def uts46_remap(domain: str, std3_rules: bool = True, transitional: bool = False
482483 code_point = ord (char )
483484 i = code_point if code_point < 256 else bisect .bisect_right (uts46_starts , code_point ) - 1
484485 status = chr (uts46_statuses [i ])
485- replacement : Optional [ str ] = uts46_replacements [i ]
486+ replacement : str | None = uts46_replacements [i ]
486487
487488 # UTS #46 §4: V is always valid, D is deviation (kept unless transitional),
488489 # 3 is disallowed-STD3 (kept unmapped if std3_rules is off and no mapping).
@@ -509,7 +510,7 @@ def uts46_remap(domain: str, std3_rules: bool = True, transitional: bool = False
509510
510511
511512def encode (
512- s : Union [ str , bytes , bytearray ] ,
513+ s : str | bytes | bytearray ,
513514 strict : bool = False ,
514515 uts46 : bool = False ,
515516 std3_rules : bool = False ,
@@ -581,7 +582,7 @@ def encode(
581582
582583
583584def decode (
584- s : Union [ str , bytes , bytearray ] ,
585+ s : str | bytes | bytearray ,
585586 strict : bool = False ,
586587 uts46 : bool = False ,
587588 std3_rules : bool = False ,
0 commit comments