remove typing dependencies (#1126)

Summary:
This PR removes the `typing_extensions` and `typing_inspect` dependencies as we can now rely on the built-in `typing` module since Python 3.9.

Test Plan:
existing tests
This commit is contained in:
Zsolt Dollenstein 2024-04-03 19:50:14 +01:00 committed by GitHub
parent a35a05f056
commit 2ffca10845
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 22 additions and 36 deletions

View file

@ -4,9 +4,7 @@
# LICENSE file in the root directory of this source tree.
from enum import auto, Enum
from typing import Any, Callable, Iterable, Optional, Sequence, Tuple, Union
from typing_extensions import final
from typing import Any, Callable, final, Iterable, Optional, Sequence, Tuple, Union
from libcst._parser.parso.pgen2.generator import ReservedString
from libcst._parser.parso.python.token import PythonTokenTypes, TokenType

View file

@ -15,9 +15,7 @@ from tokenize import (
Imagnumber as IMAGNUMBER_RE,
Intnumber as INTNUMBER_RE,
)
from typing import Callable, Generator, Optional, Sequence, Union
from typing_extensions import Literal
from typing import Callable, Generator, Literal, Optional, Sequence, Union
from libcst._add_slots import add_slots
from libcst._maybe_sentinel import MaybeSentinel

View file

@ -5,17 +5,20 @@
from typing import (
Any,
ClassVar,
ForwardRef,
get_args,
get_origin,
Iterable,
Literal,
Mapping,
MutableMapping,
MutableSequence,
Tuple,
TypeVar,
Union,
)
from typing_extensions import Literal
from typing_inspect import get_args, get_origin, is_classvar, is_typevar, is_union_type
def is_value_of_type( # noqa: C901 "too complex"
# pyre-fixme[2]: Parameter annotation cannot be `Any`.
@ -48,11 +51,11 @@ def is_value_of_type( # noqa: C901 "too complex"
- Forward Refs -- use `typing.get_type_hints` to resolve these
- Type[...]
"""
if is_classvar(expected_type):
if expected_type is ClassVar or get_origin(expected_type) is ClassVar:
classvar_args = get_args(expected_type)
expected_type = (classvar_args[0] or Any) if classvar_args else Any
if is_typevar(expected_type):
if type(expected_type) is TypeVar:
# treat this the same as Any
# TODO: evaluate bounds
return True
@ -62,13 +65,13 @@ def is_value_of_type( # noqa: C901 "too complex"
if expected_origin_type == Any:
return True
elif is_union_type(expected_type):
elif expected_type is Union or get_origin(expected_type) is Union:
return any(
is_value_of_type(value, subtype) for subtype in expected_type.__args__
)
elif isinstance(expected_origin_type, type(Literal)):
literal_values = get_args(expected_type, evaluate=True)
literal_values = get_args(expected_type)
return any(value == literal for literal in literal_values)
elif isinstance(expected_origin_type, ForwardRef):
@ -82,14 +85,11 @@ def is_value_of_type( # noqa: C901 "too complex"
if not isinstance(value, tuple):
return False
type_args = get_args(expected_type, evaluate=True)
type_args = get_args(expected_type)
if len(type_args) == 0:
# `Tuple` (no subscript) is implicitly `Tuple[Any, ...]`
return True
if type_args is None:
return True
if len(value) != len(type_args):
return False
# TODO: Handle `Tuple[T, ...]` like `Iterable[T]`
@ -106,7 +106,7 @@ def is_value_of_type( # noqa: C901 "too complex"
if not issubclass(type(value), expected_origin_type):
return False
type_args = get_args(expected_type, evaluate=True)
type_args = get_args(expected_type)
if len(type_args) == 0:
# `Mapping` (no subscript) is implicitly `Mapping[Any, Any]`.
return True
@ -143,7 +143,7 @@ def is_value_of_type( # noqa: C901 "too complex"
if not issubclass(type(value), expected_origin_type):
return False
type_args = get_args(expected_type, evaluate=True)
type_args = get_args(expected_type)
if len(type_args) == 0:
# `Iterable` (no subscript) is implicitly `Iterable[Any]`.
return True

View file

@ -443,8 +443,7 @@ generated_code.append("")
generated_code.append("")
generated_code.append("# This file was generated by libcst.codegen.gen_matcher_classes")
generated_code.append("from dataclasses import dataclass")
generated_code.append("from typing import Optional, Sequence, Union")
generated_code.append("from typing_extensions import Literal")
generated_code.append("from typing import Literal, Optional, Sequence, Union")
generated_code.append("import libcst as cst")
generated_code.append("")
generated_code.append(

View file

@ -11,8 +11,6 @@ import functools
import sys
from typing import cast, Dict, List, Optional, Sequence, Set, Tuple, Union
from typing_extensions import TypeAlias
import libcst as cst
import libcst.matchers as m
from libcst.codemod import CodemodContext, VisitorBasedCodemodCommand
@ -143,9 +141,9 @@ class _ArityError(Exception):
pass
UnpackedBindings: TypeAlias = Union[cst.BaseExpression, List["UnpackedBindings"]]
UnpackedAnnotations: TypeAlias = Union[str, List["UnpackedAnnotations"]]
TargetAnnotationPair: TypeAlias = Tuple[cst.BaseExpression, str]
UnpackedBindings = Union[cst.BaseExpression, List["UnpackedBindings"]]
UnpackedAnnotations = Union[str, List["UnpackedAnnotations"]]
TargetAnnotationPair = Tuple[cst.BaseExpression, str]
class AnnotationSpreader:

View file

@ -6,9 +6,7 @@
# This file was generated by libcst.codegen.gen_matcher_classes
from dataclasses import dataclass
from typing import Optional, Sequence, Union
from typing_extensions import Literal
from typing import Literal, Optional, Sequence, Union
import libcst as cst
from libcst.matchers._decorators import call_if_inside, call_if_not_inside, leave, visit

View file

@ -11,6 +11,7 @@ from typing import (
Dict,
Iterable,
List,
Literal,
Mapping,
MutableMapping,
NamedTuple,
@ -23,8 +24,6 @@ from typing import (
Union,
)
from typing_extensions import Literal
from libcst._type_enforce import is_value_of_type
from libcst.testing.utils import data_provider, UnitTest

View file

@ -16,11 +16,7 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
requires-python = ">=3.9"
dependencies = [
"typing_extensions>=3.7.4.2",
"typing_inspect>=0.4.0",
"pyyaml>=5.2",
]
dependencies = ["pyyaml>=5.2"]
[project.optional-dependencies]
dev = [