checker.py

View code on Github

Main module for checking Django models against TypeScript types.

class ts_backend_check.checker.TypeChecker(models_file: str, concatenated_types_file: str, check_blank: bool = False, model_name_conversions: dict[str, list[str]] = {}, backend_models_to_ignore: list[str] = [])[source]

Main class for checking Django models against TypeScript types.

Parameters:
models_filestr

The file path for the models file to check.

concatenated_types_filestr

A concatenated file text joined from all paths in ts_interface_file_paths.

check_blankbool, default=False

Whether to also check that fields marked ‘blank=True’ within Django models are optional (?) in the TypeScript interfaces.

model_name_conversionsdict[str: list[str]], default={}

A dictionary containing conversions of model names to their corresponding TypeScript interfaces.

backend_models_to_ignorelist[str], default=None | []

A list containing all Django models to be ignored by tsbc.

check() list[str][source]

Check models against TypeScript types.

Returns:
list

A list of fields missing from the TypeScript file.

_blank_fields(models_all_blank_fields: dict[str, list[str]], model_name: str) list[str][source]

Get a list of all blank fields for each model.

Parameters:
models_all_blank_fieldsdict[str,list[str]]

A dictionary containing all models as keys and their blank fields as values.

model_namestr

The name of the model to check the frontend TypeScript file for.

Returns:
list[str]

A list of all blank fields for the model.

_find_matching_interfaces(model_name: str) tuple[dict[str, list[str]], dict[str, list[str]]][source]

Find matching TypeScript interfaces for a model.

Parameters:
model_namestr

The name of the model to check the frontend TypeScript file for.

Returns:
tuple[dict[str, list[str]], dict[str, list[str]]]

Interfaces that match a model name.

_field_is_accounted_for(field: str, interfaces: dict[str, list[str]]) bool[source]

Check if a field is accounted for in TypeScript.

Parameters:
fieldstr

The field that should be used in the frontend TypeScript file.

interfacesdict[str, list[str]]

The interfaces from the frontend TypeScript file.

Returns:
Bool

Whether the field is accounted for in the frontend TypeScript file.

_property_is_optional_when_field_is_blank(model_name: str, field: str) bool[source]

Check that if the field is ‘blank=True’ that the corresponding interface property is optional (?).

Parameters:
model_namestr

The name of the model to check the frontend TypeScript file for.

fieldstr

The field that should match the optional state of the property in the TypeScript file.

Returns:
Bool

Whether the blank status of the model field matches the optional status of the interface property.

_ts_interface_properties_ordered(model_name: str, fields: list[str]) bool[source]

Check if the order of the TypeScript interface properties exactly matches that of the backend model fields.

Parameters:
model_namestr

The name of the model to check the frontend TypeScript file for.

fieldslist[str]

The fields of the backend model.

Returns:
bool

Whether the order of the properties of the TypeScript interface file match that of the backend model fields.

static _format_missing_interface_message(model_name: str) str[source]

Format message for missing interface.

Parameters:
model_namestr

The name of the model that an interface is missing from.

Returns:
str

The message displayed to the user when missing interfaces are found.

static _format_missing_field_message(field: str, model_name: str, interfaces: dict[str, list[str]]) str[source]

Format message for missing field.

Parameters:
fieldstr

The model field that’s missing.

model_namestr

The name of the model that the field is missing from.

interfacesdict[str, set[str]]

The interfaces that have been searched.

Returns:
str

The message displayed to the user when missing fields are found.

static _format_optional_properties_message(field: str, model_name: str, models_file: str) str[source]

Format message for when the blank status of a model field doesn’t match the optional status of the corresponding property.

Parameters:
fieldstr

The model field that doesn’t match blank and optional states.

model_namestr

The name of the model that the mismatch occurs in.

models_filestr

The file path for the models file to check.

Returns:
str

The message displayed to the user when missing fields are found.

static _format_unordered_interface_properties_message(models_file: str) str[source]

Format message for unordered interface properties.

Parameters:
models_filestr

The file path for the models file to check.

Returns:
str

The message displayed to the user when unordered interface properties are found.