From 123e067c5e2e6740807378c73616447e27672c8a Mon Sep 17 00:00:00 2001 From: Jorge Miranda Date: Wed, 2 Aug 2023 16:44:25 +0100 Subject: [PATCH] Add support for __future__.annotations. --- src/autofaker/generator.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/autofaker/generator.py b/src/autofaker/generator.py index 8321afb..399d6ef 100644 --- a/src/autofaker/generator.py +++ b/src/autofaker/generator.py @@ -43,12 +43,23 @@ def create_datetime(type_name, field_name: str = None, use_fake_data: bool = Fal @staticmethod def _get_type_name(t) -> str: + PRIMITIVE_TYPES = { + "int", "float", "str", "complex", "range", "bytes", "bytearray" + } + try: return t.__name__ except Exception: attributes = dir(t) if "_name" in attributes: return t._name + # If __future__.annotations was imported by the user, then the type + # will be a str. Thus, asserting the type with type() will fail, + # because it will always be a string. This is because, annotations + # transforms any type into a string object. Therefore, if the type + # is a string, assess if it is the name of a known primitive type. + elif type(t) == str and t in PRIMITIVE_TYPES: + return t return type(t).__name__