From d61cad65fc5efa537cc4daf0431604ee610dfb79 Mon Sep 17 00:00:00 2001 From: Harry Dalton Date: Thu, 13 Jul 2023 13:50:55 +0100 Subject: [PATCH 1/2] Remove some of the str conversions that are redundant in Python 3 --- Lib/fontTools/unicodedata/__init__.py | 53 +++++++++++++-------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/Lib/fontTools/unicodedata/__init__.py b/Lib/fontTools/unicodedata/__init__.py index 6f2e65dcb..019779847 100644 --- a/Lib/fontTools/unicodedata/__init__.py +++ b/Lib/fontTools/unicodedata/__init__.py @@ -15,33 +15,30 @@ from . import Blocks, Scripts, ScriptExtensions, OTTags __all__ = [ - tostr(s) - for s in ( - # names from built-in unicodedata module - "lookup", - "name", - "decimal", - "digit", - "numeric", - "category", - "bidirectional", - "combining", - "east_asian_width", - "mirrored", - "decomposition", - "normalize", - "unidata_version", - "ucd_3_2_0", - # additonal functions - "block", - "script", - "script_extension", - "script_name", - "script_code", - "script_horizontal_direction", - "ot_tags_from_script", - "ot_tag_to_script", - ) + # names from built-in unicodedata module + "lookup", + "name", + "decimal", + "digit", + "numeric", + "category", + "bidirectional", + "combining", + "east_asian_width", + "mirrored", + "decomposition", + "normalize", + "unidata_version", + "ucd_3_2_0", + # additonal functions + "block", + "script", + "script_extension", + "script_name", + "script_code", + "script_horizontal_direction", + "ot_tags_from_script", + "ot_tag_to_script", ] @@ -203,7 +200,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): From 9bfb72b05598922cf5cc3fea233d1ebf86ba00dd Mon Sep 17 00:00:00 2001 From: Harry Dalton Date: Thu, 13 Jul 2023 13:53:36 +0100 Subject: [PATCH 2/2] Fix typing of script_horizontal_direction() Without explicit annotation, some type checkers infer that the type of the 'default' argument can only be type[KeyError]. This was the case in unicodedata_test.py, where pyright disallowed "LTR". This commit adds annotations to avoid this, fixing the issue in the test (and external code dependent on the API). Some of the other functions in this file have the same semantics and suffer from the same type error, and so this fix could also be extended to them as usage requires. --- Lib/fontTools/unicodedata/__init__.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Lib/fontTools/unicodedata/__init__.py b/Lib/fontTools/unicodedata/__init__.py index 019779847..808c9c722 100644 --- a/Lib/fontTools/unicodedata/__init__.py +++ b/Lib/fontTools/unicodedata/__init__.py @@ -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: @@ -192,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". """