Skip to content

Commit

Permalink
Drop support for Python 3.7
Browse files Browse the repository at this point in the history
Resolves #21
  • Loading branch information
davidfstr committed Dec 23, 2023
1 parent f0ebe49 commit 2f28dee
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/push-github-action.yml
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11"]
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand Down
6 changes: 2 additions & 4 deletions pyproject.toml
Expand Up @@ -16,7 +16,6 @@ classifiers = [ # https://pypi.org/classifiers/
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
Expand All @@ -28,8 +27,7 @@ classifiers = [ # https://pypi.org/classifiers/
"Release notes" = "https://github.com/davidfstr/trycast#changelog"

[tool.poetry.dependencies]
# Python 3.7.4 is the minimum Python with typing.ForwardRef
python = "^3.7.4"
python = ">=3.8"

[tool.poetry.dev-dependencies]
mypy = "*"
Expand Down Expand Up @@ -80,7 +78,7 @@ line_length = 88
legacy_tox_ini = """
[tox]
envlist = py37,py38,py39,py310
envlist = py38,py39,py310
isolated_build = True
[testenv]
Expand Down
55 changes: 6 additions & 49 deletions trycast.py
Expand Up @@ -21,6 +21,7 @@
Dict,
FrozenSet,
List,
Literal,
Mapping,
MutableMapping,
MutableSequence,
Expand All @@ -37,7 +38,7 @@
)
from typing import _eval_type as eval_type # type: ignore[attr-defined]
from typing import _type_repr as type_repr # type: ignore[attr-defined]
from typing import cast, overload
from typing import cast, get_args, get_origin, overload

try:
from types import UnionType # type: ignore[attr-defined]
Expand All @@ -49,25 +50,13 @@ class UnionType(type): # type: ignore[no-redef]

# get_type_hints
try:
# Python 3.7+
# If typing_extensions available,
# understands both typing.* and typing_extensions.* types
from typing_extensions import get_type_hints # type: ignore[attr-defined]
except ImportError:
# Python 3.6
# If typing_extensions not available
from typing import get_type_hints # type: ignore[misc] # incompatible import

# Literal
if sys.version_info >= (3, 8):
from typing import Literal # Python 3.8+
else:
try:
from typing_extensions import Literal # Python 3.5+
except ImportError:
if not TYPE_CHECKING:

class Literal:
def __class_getitem__(cls, key):
pass


# TypeGuard
if sys.version_info >= (3, 10):
Expand All @@ -85,28 +74,6 @@ def __class_getitem__(cls, key):
return bool


# get_origin, get_args
if sys.version_info >= (3, 8):
from typing import get_args, get_origin # Python 3.8+

elif sys.version_info >= (3, 7):

def get_origin(tp: object) -> Optional[object]:
if isinstance(tp, _GenericAlias): # type: ignore[16] # pyre
return tp.__origin__ # type: ignore[reportGeneralTypeIssues] # pyright
else:
return None

def get_args(tp: object) -> Tuple[object, ...]:
if isinstance(tp, _GenericAlias): # type: ignore[16] # pyre
return tp.__args__ # type: ignore[reportGeneralTypeIssues] # pyright
else:
return ()

else: # pragma: no cover
raise ImportError("Expected Python 3.7 or later.")


# _is_typed_dict
_typed_dict_meta_list = []
try:
Expand Down Expand Up @@ -145,7 +112,7 @@ def _is_typed_dict(tp: object) -> bool:

# _is_newtype
if NewType.__class__.__name__ == "function": # type: ignore[reportGeneralTypeIssues] # pyright
# Python 3.7 - 3.9
# Python 3.8 - 3.9
def _is_newtype(tp: object) -> bool:
return (
hasattr(tp, "__class__")
Expand Down Expand Up @@ -587,16 +554,6 @@ def _trycast_inner(tp, value, failure, options):
else:
return failure
else:
if len(callable_args) != 2 or (
callable_args[0] is not Ellipsis
and not isinstance(callable_args[0], list)
):
# Python 3.7
callable_args = (
list(callable_args[: len(callable_args) - 1]),
callable_args[len(callable_args) - 1],
)

assert len(callable_args) == 2
(param_types, return_type) = callable_args

Expand Down

0 comments on commit 2f28dee

Please sign in to comment.