Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ license = "MIT"
license-files = [ "LICENSE.txt" ]
dependencies = [
"docstring-parser >= 0.15",
"typing-inspect >= 0.7.1",
]
requires-python = ">=3.10"
classifiers = [
Expand Down
8 changes: 4 additions & 4 deletions src/tap/tap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
Union,
get_type_hints,
get_origin as typing_get_origin,
get_args as typing_get_args,
get_args,
)
from typing_inspect import is_literal_type

from tap.utils import (
get_class_variables,
get_args,
is_literal_type,
get_argument_name,
get_dest,
get_origin,
Expand Down Expand Up @@ -201,6 +200,7 @@ def _add_argument(self, *name_or_flags, **kwargs) -> None:
var_args = (str, type(None))

# Raise error if type function is not explicitly provided for Union types (not including Optionals)

if get_origin(var_type) in UNION_TYPES and not (len(var_args) == 2 and var_args[1] == type(None)):
raise ArgumentTypeError(
"For Union types, you must include an explicit type function in the configure method. "
Expand Down Expand Up @@ -329,7 +329,7 @@ def _is_ignored_argument(self, variable: str, annotations: Optional[dict[str, An
annotations = self._annotations_with_extras if annotations is None else annotations
if variable in annotations:
var_type = annotations[variable]
if typing_get_origin(var_type) is Annotated and _TapIgnoreMarker in typing_get_args(var_type):
if typing_get_origin(var_type) is Annotated and _TapIgnoreMarker in get_args(var_type):
return True
return False

Expand Down
39 changes: 18 additions & 21 deletions src/tap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import pickle
import re
import subprocess
import collections.abc
import sys
import typing
import textwrap
import tokenize
import warnings
from argparse import ArgumentParser, ArgumentTypeError
from base64 import b64decode, b64encode
from io import StringIO
from json import JSONEncoder
from types import UnionType
from types import UnionType, GenericAlias
from typing import (
Annotated,
Any,
Expand All @@ -24,11 +26,14 @@
Optional,
TypeAlias,
TypeVar,
Generic,
_GenericAlias,
_SpecialGenericAlias,
ClassVar,
)

from typing_inspect import get_args as typing_inspect_get_args
from typing_inspect import get_origin as typing_inspect_get_origin

TYPING_GENERIC_ALIAS = (_GenericAlias, _SpecialGenericAlias, GenericAlias)
NO_CHANGES_STATUS = """nothing to commit, working tree clean"""
PRIMITIVES = (str, int, float, bool)
PathLike = str | os.PathLike
Expand Down Expand Up @@ -385,7 +390,7 @@ def get_class_variables(cls: type) -> dict[str, dict[str, str]]:

def get_literals(literal: Literal, variable: str) -> tuple[Callable[[str], Any], list[type]]:
"""Extracts the values from a Literal type and ensures that the values are all primitive types."""
literals = list(get_args(literal))
literals = list(typing.get_args(literal))

if not all(isinstance(literal, PRIMITIVES) for literal in literals):
raise ArgumentTypeError(
Expand Down Expand Up @@ -576,29 +581,21 @@ def enforce_reproducibility(
raise ValueError(f"{no_reproducibility_message}: Uncommitted changes " f"in current args.")


# TODO: remove this once typing_inspect.get_origin is fixed for 3.10
# https://github.com/ilevkivskyi/typing_inspect/issues/64
# https://github.com/ilevkivskyi/typing_inspect/issues/65
def get_origin(tp: Any) -> Any:
"""Same as typing_inspect.get_origin but fixes unparameterized generic types like Set."""
origin = typing_inspect_get_origin(tp)

if origin is None:
"""Get the unsubscribed version of a type."""
if (origin := typing.get_origin(tp)) is None:
origin = tp

if isinstance(origin, UnionType):
origin = UnionType

return origin


# TODO: remove this once typing_inspect.get_args is fixed for Python 3.10 union types
def get_args(tp: Any) -> tuple[type, ...]:
"""Same as typing_inspect.get_args but fixes Python 3.10 union types."""
if isinstance(tp, UnionType):
return tp.__args__
def is_literal_type(tp: Any) -> bool:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it's worth adding a test for this change (if that indeed is what codecov has an issue with).

if tp is Literal:
return True

if isinstance(tp, TYPING_GENERIC_ALIAS) and tp.__origin__ is Literal:
return True

return typing_inspect_get_args(tp)
return False


_T = TypeVar("_T")
Expand Down