Skip to content

Use unknown instead of any as the value to fallback to when the type is not known #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2023
Merged
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
10 changes: 5 additions & 5 deletions django_typomatic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __map_choices_to_union(field_type, choices):
'''
if not choices:
_LOG.warning(f'No choices specified for Serializer Field: {field_type}')
return 'any'
return 'unknown'

return ' | '.join(f'"{key}"' if type(key) == str else str(key) for key in choices.keys())

Expand All @@ -89,7 +89,7 @@ def __map_choices_to_enum(enum_name, field_type, choices):
'''
if not choices:
_LOG.warning(f'No choices specified for Serializer Field: {field_type}')
return 'any'
return 'unknown'

choices_enum = f"export enum {enum_name} {{\n"
for key, value in choices.items():
Expand Down Expand Up @@ -120,11 +120,11 @@ def __process_field(field_name, field, context, serializer, trim_serializer_outp
ts_type = __get_trimmed_name(
field_type.__name__, trim_serializer_output)
elif field_type in __field_mappings[context]:
ts_type = __field_mappings[context].get(field_type, 'any')
ts_type = __field_mappings[context].get(field_type, 'unknown')
elif (context in __mapping_overrides) and (serializer in __mapping_overrides[context]) \
and field_name in __mapping_overrides[context][serializer]:
ts_type = __mapping_overrides[context][serializer].get(
field_name, 'any')
field_name, 'unknown')
elif field_type == serializers.PrimaryKeyRelatedField:
ts_type = "number | string"
elif hasattr(field, 'choices') and enum_choices:
Expand All @@ -133,7 +133,7 @@ def __process_field(field_name, field, context, serializer, trim_serializer_outp
elif hasattr(field, 'choices'):
ts_type = __map_choices_to_union(field_type, field.choices)
else:
ts_type = mappings.get(field_type, 'any')
ts_type = mappings.get(field_type, 'unknown')
if is_many:
ts_type += '[]'

Expand Down