Skip to content

Commit

Permalink
Add support for __future__.annotations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmpcm committed Aug 2, 2023
1 parent ffbbf53 commit 123e067
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/autofaker/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__


Expand Down

0 comments on commit 123e067

Please sign in to comment.