Skip to content

Commit

Permalink
remove typing dependencies
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zsol committed Apr 3, 2024
1 parent f6493db commit cf70acd
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 33 deletions.
4 changes: 1 addition & 3 deletions libcst/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, Iterable, Optional, Sequence, Tuple, Union, final

from libcst._parser.parso.pgen2.generator import ReservedString
from libcst._parser.parso.python.token import PythonTokenTypes, TokenType
Expand Down
4 changes: 1 addition & 3 deletions libcst/_nodes/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
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, Optional, Sequence, Union, Literal

from libcst._add_slots import add_slots
from libcst._maybe_sentinel import MaybeSentinel
Expand Down
24 changes: 12 additions & 12 deletions libcst/_type_enforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@

from typing import (
Any,
ClassVar,
ForwardRef,
Iterable,
Mapping,
MutableMapping,
MutableSequence,
Tuple,
TypeVar,
Literal,
Union,
get_origin, get_args
)

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"
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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]`
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions libcst/codegen/gen_matcher_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,7 @@ def _get_fields(node: Type[cst.CSTNode]) -> Generator[Field, None, None]:
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(
Expand Down
4 changes: 1 addition & 3 deletions libcst/codemod/commands/convert_type_comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import dataclasses
import functools
import sys
from typing import cast, Dict, List, Optional, Sequence, Set, Tuple, Union

from typing_extensions import TypeAlias
from typing import cast, Dict, List, Optional, Sequence, Set, Tuple,TypeAlias, Union

import libcst as cst
import libcst.matchers as m
Expand Down
4 changes: 1 addition & 3 deletions libcst/matchers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions libcst/tests/test_type_enforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Dict,
Iterable,
List,
Literal,
Mapping,
MutableMapping,
NamedTuple,
Expand All @@ -23,8 +24,6 @@
Union,
)

from typing_extensions import Literal

from libcst._type_enforce import is_value_of_type
from libcst.testing.utils import data_provider, UnitTest

Expand Down
6 changes: 1 addition & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit cf70acd

Please sign in to comment.