Skip to content

Commit

Permalink
Adds additional fixes for earlier python versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-schneider committed Mar 26, 2022
1 parent fb32fa5 commit 89414a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 11 additions & 6 deletions omegaconf/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
attr = None # type: ignore # pragma: no cover

if sys.version_info >= (3, 8):
from typing import Literal
from typing import Literal # pragma: no cover
else:
from typing_extensions import Literal # pragma: no cover

Expand Down Expand Up @@ -600,10 +600,14 @@ def is_tuple_annotation(type_: Any) -> bool:

def is_literal_annotation(type_: Any) -> bool:
origin = getattr(type_, "__origin__", None)
if sys.version_info < (3, 7, 0):
return origin is Literal or type_ is Literal # pragma: no cover
else:
if sys.version_info >= (3, 8, 0):
return origin is Literal # pragma: no cover
else:
return (
origin is Literal
or type_ is Literal
or ("typing_extensions.Literal" in str(type_) and not isinstance(type, str))
) # pragma: no cover


def is_dict_subclass(type_: Any) -> bool:
Expand Down Expand Up @@ -848,14 +852,15 @@ def type_str(t: Any, include_module_name: bool = False) -> str:
or isinstance(t, bytes)
or isinstance(t, Enum)
):
return str(t)
# only occurs when using typing.Literal after 3.8
return str(t) # pragma: no cover

if sys.version_info < (3, 7, 0): # pragma: no cover
# Python 3.6
if hasattr(t, "__name__"):
name = str(t.__name__)
else:
if t.__origin__ is not None:
if hasattr(t, "__origin__") and t.__origin__ is not None:
name = type_str(t.__origin__)
else:
name = str(t)
Expand Down
7 changes: 5 additions & 2 deletions omegaconf/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
from abc import abstractmethod
from enum import Enum
from typing import Any, Dict, List, Optional, Type, Union
from typing import Any, Dict, Optional, Type, Union

from omegaconf._utils import (
ValueKind,
Expand Down Expand Up @@ -454,7 +454,10 @@ def __init__(
f"LiteralNode can only operate on Literal annotation ({literal_type})"
)
self.literal_type = literal_type
self.fields: List[Any] = list(self.literal_type.__args__)
if hasattr(self.literal_type, "__args__"):
self.fields = list(self.literal_type.__args__) # pragma: no cover
elif hasattr(self.literal_type, "__values__"): # pragma: no cover
self.fields = list(self.literal_type.__values__) # pragma: no cover
super().__init__(
parent=parent,
value=value,
Expand Down

0 comments on commit 89414a8

Please sign in to comment.