From b0c8358ac81b1f473191a877e20ba9b59ddab8ef Mon Sep 17 00:00:00 2001 From: Erez Shinan Date: Sun, 9 Oct 2022 20:06:27 +0300 Subject: [PATCH] Fix for Python 3.6 --- runtype/dataclass.py | 3 ++- runtype/pytypes.py | 3 ++- runtype/utils.py | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/runtype/dataclass.py b/runtype/dataclass.py index cdc3c9b..575af56 100644 --- a/runtype/dataclass.py +++ b/runtype/dataclass.py @@ -5,10 +5,11 @@ import random from copy import copy import dataclasses -from typing import Union, ForwardRef +from typing import Union from abc import ABC, abstractmethod import inspect +from .utils import ForwardRef from .common import CHECK_TYPES from .validation import TypeMismatchError, ensure_isa as default_ensure_isa from .pytypes import TypeCaster, type_caster, SumType, NoneType diff --git a/runtype/pytypes.py b/runtype/pytypes.py index 1459a34..1eec894 100644 --- a/runtype/pytypes.py +++ b/runtype/pytypes.py @@ -9,6 +9,7 @@ import typing from datetime import datetime +from .utils import ForwardRef from .base_types import DataType, Validator, TypeMismatchError from . import base_types from . import datetime_parse @@ -340,7 +341,7 @@ def _to_canon(self, t): if isinstance(t, (base_types.Type, Validator)): return t - if isinstance(t, typing.ForwardRef): + if isinstance(t, ForwardRef): t = t._evaluate(self.frame.f_globals, self.frame.f_locals, set()) if isinstance(t, tuple): diff --git a/runtype/utils.py b/runtype/utils.py index d53cd02..ef60afa 100644 --- a/runtype/utils.py +++ b/runtype/utils.py @@ -1,5 +1,15 @@ import inspect +try: + from typing import ForwardRef +except ImportError: + # python 3.6 + from typing import _ForwardRef as ForwardRef + def _evaluate(self, g, l, _): + return self._eval_type(g, l) + ForwardRef._evaluate = _evaluate + + def get_func_signatures(typesystem, f): sig = inspect.signature(f) typesigs = []