def build(self, cls: List, origin: Optional[type], args: List[Any], is_mapping_key: bool) -> Tuple[Optional[str], str]: type_ = args[0] # can't have optional here built = f'({self.store.get_repr(type_)})[]' if (name := get_extended_name(cls)) is None: return None, built
def build(self, cls: Any, origin: Optional[type], args: List[Any], is_mapping_key: bool) -> Tuple[Optional[str], str]: key_type, value_type = args # can't have optional here type_token = "interface" if self.store.as_interface else "type" built = f'{{ [key: {self.store.get_repr(key_type)}]: {self.store.get_repr(value_type)} }}' if (name := get_extended_name(cls)) is None: return name, built
def build( self, cls: Any, origin: Optional[type], args: List[Any], is_mapping_key: bool ) -> Tuple[Optional[str], Union[str, Dict[str, Any]]]: # Union[Any] because Union is like Never built = '[' + f', '.join( self.store.get_repr(arg) for arg in args if arg is not NoneType) + ']' if (name := get_extended_name(cls)) is None: return None, built
def build(self, cls: Union[Any], origin, args, is_mapping_key: bool) -> Tuple[Optional[str], str]: # Union[Any] because Union is like Never if (name := get_extended_name(cls)) is None: if is_mapping_key: return None, f' | '.join( self.store.get_repr(arg) for arg in args if arg is not NoneType) else: return None, f' | '.join( self.store.get_repr(arg) for arg in args)
def test_generic_array(self): u = Named(Union[int, str]) a = List[u] store = TSTypeStore() res = '(u)[]' print('extended name should be u', get_extended_name(u)) self.assertEqual(store.get_repr(a), res)
def build(self, cls: Literal, origin, args, is_mapping_key: bool) -> Tuple[Optional[str], str]: name = get_extended_name(cls) is_inline = name is None s = [] for arg in args: if isinstance(arg, (int, float)): s.append(f'{arg}') elif isinstance(arg, str): s.append(f'"{arg}"') else: raise InvalidTypeArgument( f'{arg} is neither a number instance, nor a string instance' ) res = ' | '.join(s) if is_inline: return None, res else: return name, f'type {name} = {res};'
def test_inline_generic_array(self): b = List[Union[int, str]] store = TSTypeStore() res = '(number /*int*/ | string)[]' print('extended name should be None', get_extended_name(b)) self.assertEqual(store.get_repr(b), res)
def test_set_name(self): item = Named(Union[int, str]) self.assertEqual(get_extended_name(item), 'item')