def schema(self) -> str: """ PostGres schema string """ schema = [] for field, field_type in self.fields.items(): field_type = guard_generic_alias(field_type) if field_type in [list, tuple, Sequence, Collection]: field_type = [typing_get_args(field_type[0])] dimensions = 0 while isinstance(field_type, Sequence) and not isinstance(field_type, str): if len(field_type) > 0: field_type = field_type[0] else: field_type = list dimensions += 1 if isinstance(field_type, Mapping): field_type = dict try: field_type = self.FIELD_TYPES[field_type.__name__] except KeyError: raise TypeError( f'PostGres does not support type "{field_type}"') schema.append(f'{field} {field_type}{"[]" * dimensions}') schema.append(f'PRIMARY KEY({", ".join(self.primary_key)})') return ', '.join(schema)
def get_args(tp: Type[Any]) -> Tuple[Any, ...]: """Get type arguments with all substitutions performed. For unions, basic simplifications used by Union constructor are performed. Examples:: get_args(Dict[str, int]) == (str, int) get_args(int) == () get_args(Union[int, Union[T, int], str][int]) == (int, str) get_args(Union[int, Tuple[T, int]][str]) == (int, Tuple[str, int]) get_args(Callable[[], T][int]) == ([], int) """ # the fallback is needed for the same reasons as `get_origin` (see above) return typing_get_args(tp) or getattr(tp, '__args__', ()) or generic_get_args(tp)