Merge pull request #3204 from daltonmaag/fix-unicodedata-typing

Fix typing of script_horizontal_direction()
This commit is contained in:
Cosimo Lupo 2023-07-13 15:30:00 +01:00 committed by GitHub
commit 56731a9f14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,11 @@
from __future__ import annotations
from fontTools.misc.textTools import byteord, tostr
import re
from bisect import bisect_right
from typing import Literal, TypeVar, overload
try:
# use unicodedata backport compatible with python2:
@ -15,8 +19,6 @@ from . import Blocks, Scripts, ScriptExtensions, OTTags
__all__ = [
tostr(s)
for s in (
# names from built-in unicodedata module
"lookup",
"name",
@ -41,7 +43,6 @@ __all__ = [
"script_horizontal_direction",
"ot_tags_from_script",
"ot_tag_to_script",
)
]
@ -195,7 +196,25 @@ RTL_SCRIPTS = {
}
def script_horizontal_direction(script_code, default=KeyError):
HorizDirection = Literal["RTL", "LTR"]
T = TypeVar("T")
@overload
def script_horizontal_direction(script_code: str, default: T) -> HorizDirection | T:
...
@overload
def script_horizontal_direction(
script_code: str, default: type[KeyError] = KeyError
) -> HorizDirection:
...
def script_horizontal_direction(
script_code: str, default: T | type[KeyError] = KeyError
) -> HorizDirection | T:
"""Return "RTL" for scripts that contain right-to-left characters
according to the Bidi_Class property. Otherwise return "LTR".
"""
@ -203,7 +222,7 @@ def script_horizontal_direction(script_code, default=KeyError):
if isinstance(default, type) and issubclass(default, KeyError):
raise default(script_code)
return default
return str("RTL") if script_code in RTL_SCRIPTS else str("LTR")
return "RTL" if script_code in RTL_SCRIPTS else "LTR"
def block(char):