From 51c719c6ba37c5d9242d3b9be5cb9eeaab9760c5 Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Sun, 21 Apr 2024 03:08:48 +0800 Subject: [PATCH 1/4] Fix LSP.plugin.core.typing with py38 Signed-off-by: Jack Cherng --- plugin/core/typing.py | 179 ++++++++++-------------------------------- 1 file changed, 42 insertions(+), 137 deletions(-) diff --git a/plugin/core/typing.py b/plugin/core/typing.py index be399071a..5a3b23511 100644 --- a/plugin/core/typing.py +++ b/plugin/core/typing.py @@ -1,152 +1,57 @@ import sys - -if sys.version_info >= (3, 11, 0): - - from enum import Enum, IntEnum, IntFlag, StrEnum - from typing import Any - from typing import Callable - from typing import cast - from typing import Deque - from typing import Dict - from typing import final - from typing import Generator - from typing import Generic - from typing import IO - from typing import Iterable - from typing import Iterator - from typing import List - from typing import Literal - from typing import Mapping - from typing import NotRequired - from typing import Optional - from typing import ParamSpec - from typing import Protocol - from typing import Required - from typing import Sequence - from typing import Set - from typing import Tuple - from typing import Type - from typing import TYPE_CHECKING - from typing import TypedDict - from typing import TypeGuard - from typing import TypeVar - from typing import Union - +from enum import Enum, IntEnum, IntFlag # noqa: F401 +from typing import ( # noqa: F401 + IO, # noqa: F401 + TYPE_CHECKING, # noqa: F401 + Any, # noqa: F401 + Callable, # noqa: F401 + Deque, # noqa: F401 + Dict, # noqa: F401 + Generator, # noqa: F401 + Generic, # noqa: F401 + Iterable, # noqa: F401 + Iterator, # noqa: F401 + List, # noqa: F401 + Literal, # noqa: F401 + Mapping, # noqa: F401 + Optional, # noqa: F401 + Protocol, # noqa: F401 + Sequence, # noqa: F401 + Set, # noqa: F401 + Tuple, # noqa: F401 + Type, # noqa: F401 + TypedDict, # noqa: F401 + TypeVar, # noqa: F401 + Union, # noqa: F401 + cast, # noqa: F401 + final, # noqa: F401 +) + +if sys.version_info >= (3, 11): + from enum import StrEnum # noqa: F401 + from typing import ( + NotRequired, # noqa: F401 + ParamSpec, # noqa: F401 + Required, # noqa: F401 + TypeGuard, # noqa: F401 + ) else: - TYPE_CHECKING = False - - def cast(typ, val): # type: ignore - return val - - def final(func): # type: ignore - return func - - def _make_type(name: str) -> '_TypeMeta': - return _TypeMeta(name, (Type,), {}) # type: ignore - - class _TypeMeta(type): - def __getitem__(self, args: 'Any') -> 'Any': - if not isinstance(args, tuple): - args = (args,) - - name = '{}[{}]'.format( - str(self), - ', '.join(map(str, args)) - ) - return _make_type(name) - - def __str__(self) -> str: - return self.__name__ - - class Type(metaclass=_TypeMeta): # type: ignore - pass - - class TypedDict(Type, dict): # type: ignore - def __init__(*args, **kwargs) -> None: # type: ignore - pass - - class TypeGuard(Type): # type: ignore - pass - - class Enum(Type): # type: ignore - pass - - class IntEnum(Type): # type: ignore - pass - - class IntFlag(Type): # type: ignore - pass - class StrEnum(Type): # type: ignore pass - class Any(Type): # type: ignore - pass - - class Callable(Type): # type: ignore - pass - - class Deque(Type): # type: ignore - pass - - class Dict(Type): # type: ignore - pass - - class Generic(Type): # type: ignore - pass - - class Generator(Type): # type: ignore - pass - - class IO(Type): # type: ignore - pass - - class Iterable(Type): # type: ignore - pass - - class Iterator(Type): # type: ignore - pass - - class List(Type): # type: ignore - pass - - class Literal(Type): # type: ignore - pass - - class Mapping(Type): # type: ignore - pass - - class Optional(Type): # type: ignore - pass - - class Set(Type): # type: ignore - pass - - class Tuple(Type): # type: ignore - pass - - class Union(Type): # type: ignore - pass - - class Protocol(Type): # type: ignore - pass - - class Sequence(Type): # type: ignore - pass - - class Required(Type): # type: ignore - pass - class NotRequired(Type): # type: ignore pass - def TypeVar(*args, **kwargs) -> Any: # type: ignore - return object - class ParamSpec(Type): # type: ignore args = ... kwargs = ... def __init__(*args, **kwargs) -> None: # type: ignore pass + + class Required(Type): # type: ignore + pass + + class TypeGuard(Type): # type: ignore + pass From 185644300657ed83ce7298d2c4ad142872fddbc8 Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Sun, 21 Apr 2024 03:46:45 +0800 Subject: [PATCH 2/4] fixup: try to fix NotRequired, Required, TypeGuard Signed-off-by: Jack Cherng --- plugin/core/typing.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/plugin/core/typing.py b/plugin/core/typing.py index 5a3b23511..0fc98b923 100644 --- a/plugin/core/typing.py +++ b/plugin/core/typing.py @@ -36,11 +36,12 @@ TypeGuard, # noqa: F401 ) else: + _T = TypeVar("_T") class StrEnum(Type): # type: ignore pass - class NotRequired(Type): # type: ignore + class NotRequired(Type, Generic[_T]): # type: ignore pass class ParamSpec(Type): # type: ignore @@ -50,8 +51,8 @@ class ParamSpec(Type): # type: ignore def __init__(*args, **kwargs) -> None: # type: ignore pass - class Required(Type): # type: ignore + class Required(Type, Generic[_T]): # type: ignore pass - class TypeGuard(Type): # type: ignore + class TypeGuard(Type, Generic[_T]): # type: ignore pass From 1ce73a520cc3b68a8a37dee3ea0b974a95699b7f Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Sun, 21 Apr 2024 03:59:42 +0800 Subject: [PATCH 3/4] refactor: naive functional StrEnum polyfill Signed-off-by: Jack Cherng --- plugin/core/typing.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/plugin/core/typing.py b/plugin/core/typing.py index 0fc98b923..f4754b829 100644 --- a/plugin/core/typing.py +++ b/plugin/core/typing.py @@ -38,8 +38,15 @@ else: _T = TypeVar("_T") - class StrEnum(Type): # type: ignore - pass + class StrEnum(str, Enum): + """ + Naive polyfill for Python 3.11's StrEnum. + + See https://docs.python.org/3.11/library/enum.html#enum.StrEnum + """ + + __format__ = str.__format__ + __str__ = str.__str__ class NotRequired(Type, Generic[_T]): # type: ignore pass From ac2ee2b7cd33854375c879d883bef5d5b81dfaa7 Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Sun, 21 Apr 2024 04:06:50 +0800 Subject: [PATCH 4/4] chore: reduce amount of "# noqa: 401"s Signed-off-by: Jack Cherng --- plugin/core/typing.py | 58 +++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/plugin/core/typing.py b/plugin/core/typing.py index f4754b829..038793c38 100644 --- a/plugin/core/typing.py +++ b/plugin/core/typing.py @@ -1,39 +1,39 @@ import sys from enum import Enum, IntEnum, IntFlag # noqa: F401 from typing import ( # noqa: F401 - IO, # noqa: F401 - TYPE_CHECKING, # noqa: F401 - Any, # noqa: F401 - Callable, # noqa: F401 - Deque, # noqa: F401 - Dict, # noqa: F401 - Generator, # noqa: F401 - Generic, # noqa: F401 - Iterable, # noqa: F401 - Iterator, # noqa: F401 - List, # noqa: F401 - Literal, # noqa: F401 - Mapping, # noqa: F401 - Optional, # noqa: F401 - Protocol, # noqa: F401 - Sequence, # noqa: F401 - Set, # noqa: F401 - Tuple, # noqa: F401 - Type, # noqa: F401 - TypedDict, # noqa: F401 - TypeVar, # noqa: F401 - Union, # noqa: F401 - cast, # noqa: F401 - final, # noqa: F401 + IO, + TYPE_CHECKING, + Any, + Callable, + Deque, + Dict, + Generator, + Generic, + Iterable, + Iterator, + List, + Literal, + Mapping, + Optional, + Protocol, + Sequence, + Set, + Tuple, + Type, + TypedDict, + TypeVar, + Union, + cast, + final, ) if sys.version_info >= (3, 11): from enum import StrEnum # noqa: F401 - from typing import ( - NotRequired, # noqa: F401 - ParamSpec, # noqa: F401 - Required, # noqa: F401 - TypeGuard, # noqa: F401 + from typing import ( # noqa: F401 + NotRequired, + ParamSpec, + Required, + TypeGuard, ) else: _T = TypeVar("_T")