diff --git a/coconut/compiler/compiler.py b/coconut/compiler/compiler.py index 8aaf2496..cae7f6d8 100644 --- a/coconut/compiler/compiler.py +++ b/coconut/compiler/compiler.py @@ -3493,25 +3493,30 @@ def __new__(_coconut_cls, {all_args}): return self.assemble_data(decorators, name, namedtuple_call, inherit, extra_stmts, stmts, base_args, paramdefs) - def make_namedtuple_call(self, name, namedtuple_args, types=None): + def make_namedtuple_call(self, name, namedtuple_args, types=None, of_args=None): """Construct a namedtuple call.""" if types: wrapped_types = [ self.wrap_typedef(types.get(i, "_coconut.typing.Any"), for_py_typedef=False) for i in range(len(namedtuple_args)) ] - if name is None: - return "_coconut_mk_anon_namedtuple(" + tuple_str_of(namedtuple_args, add_quotes=True) + ", " + tuple_str_of(wrapped_types) + ")" - else: - return '_coconut.typing.NamedTuple("' + name + '", [' + ", ".join( - '("' + argname + '", ' + wrapped_type + ")" - for argname, wrapped_type in zip(namedtuple_args, wrapped_types) - ) + "])" else: - if name is None: - return "_coconut_mk_anon_namedtuple(" + tuple_str_of(namedtuple_args, add_quotes=True) + ")" - else: - return '_coconut.collections.namedtuple("' + name + '", ' + tuple_str_of(namedtuple_args, add_quotes=True) + ')' + wrapped_types = None + if name is None: + return ( + "_coconut_mk_anon_namedtuple(" + + tuple_str_of(namedtuple_args, add_quotes=True) + + ("" if wrapped_types is None else ", " + tuple_str_of(wrapped_types)) + + ("" if of_args is None else ", of_args=" + tuple_str_of(of_args) + "") + + ")" + ) + elif wrapped_types is None: + return '_coconut.collections.namedtuple("' + name + '", ' + tuple_str_of(namedtuple_args, add_quotes=True) + ')' + ("" if of_args is None else tuple_str_of(of_args)) + else: + return '_coconut.typing.NamedTuple("' + name + '", [' + ", ".join( + '("' + argname + '", ' + wrapped_type + ")" + for argname, wrapped_type in zip(namedtuple_args, wrapped_types) + ) + "])" + ("" if of_args is None else tuple_str_of(of_args)) def assemble_data(self, decorators, name, namedtuple_call, inherit, extra_stmts, stmts, match_args, paramdefs=()): """Create a data class definition from the given components. @@ -3617,8 +3622,7 @@ def anon_namedtuple_handle(self, original, loc, tokens): names.append(name) items.append(item) - namedtuple_call = self.make_namedtuple_call(None, names, types) - return namedtuple_call + "(" + ", ".join(items) + ")" + return self.make_namedtuple_call(None, names, types, of_args=items) def single_import(self, loc, path, imp_as, type_ignore=False): """Generate import statements from a fully qualified import and the name to bind it to.""" diff --git a/coconut/compiler/templates/header.py_template b/coconut/compiler/templates/header.py_template index 7aa6a0a9..d0120655 100644 --- a/coconut/compiler/templates/header.py_template +++ b/coconut/compiler/templates/header.py_template @@ -2017,17 +2017,17 @@ collectby.using_threads = _coconut_partial(_coconut_parallel_mapreduce, collectb def _namedtuple_of(**kwargs): """Construct an anonymous namedtuple of the given keyword arguments.""" {namedtuple_of_implementation} -def _coconut_mk_anon_namedtuple(fields, types=None, of_kwargs=None): +def _coconut_mk_anon_namedtuple(fields, types=None, of_kwargs={empty_dict}, of_args=()): if types is None: NT = _coconut.collections.namedtuple("_namedtuple_of", fields) else: NT = _coconut.typing.NamedTuple("_namedtuple_of", [(f, t) for f, t in _coconut.zip(fields, types)]) _coconut.copyreg.pickle(NT, lambda nt: (_coconut_mk_anon_namedtuple, (nt._fields, types, nt._asdict()))) - if of_kwargs is None: - nt = NT - else: - nt = NT(**of_kwargs) -{set_nt_match_args} return nt + if not (of_kwargs or of_args): + return NT + nt = NT(*of_args, **of_kwargs) +{set_nt_match_args} + return nt def _coconut_ndim(arr): arr_mod = _coconut_get_base_module(arr) if (arr_mod in _coconut.numpy_modules or _coconut.hasattr(arr.__class__, "__matconcat__")) and _coconut.hasattr(arr, "ndim"): diff --git a/coconut/constants.py b/coconut/constants.py index 995f53d6..14960727 100644 --- a/coconut/constants.py +++ b/coconut/constants.py @@ -666,7 +666,7 @@ def get_path_env_var(env_var, default): prompt_vi_mode = get_bool_env_var(vi_mode_env_var, False) prompt_wrap_lines = True prompt_history_search = True -prompt_use_suggester = not PY2 +prompt_use_suggester = False base_dir = os.path.dirname(os.path.abspath(fixpath(__file__)))