Starting from here, I want a function that add a superscript depending on the number of elements passed as arguments. As depicted below.

As you can see, when there already is a superscript in the set, parentheses are added when not needed. My guess is there is a problem of expansion in my code. Any help appreciated!
% !TeX program = lualatex
% !BIB TS-program = biber
% !TeX encoding = utf8
% !TeX spellcheck = fr_FR
\documentclass{memoir}
\usepackage{unicode-math}
\usepackage{booktabs}
% tiré de https://tex.stackexchange.com/a/481888/151808
\NewDocumentCommand{\ajoutParentheses}{me{^}}{\ensuremath{\ajoutParenthesesAux{#1}{#2}{#3}}} % adds parentheses around #1 if needed
\NewDocumentCommand{\liste}{d()}{\ensuremath{\left(\formatListe { #1 }[ , ]\right)}} % (⋅,⋅,⋅) typesets list
% typesets #2 \in [or \notin if #1] #3{#4}^{#5} adding parentheses around #2 if list and around #3 if needed
\NewDocumentCommand{\dans}{smD(){\BbbR}e{_^}}{\ensuremath{\ajoutParenthesesListe{#2}\IfBooleanTF{#1}{\notin}{\in}\IfValueTF{#5}{\ajoutParenthesesAux{#3}{#4}{#5}}{\ajoutParenthesesAux{#3}{#4}{\nbElmts{#2}}}}}
\def\pe{+\star}
\def\BbbRpe{\ensuremath{\BbbR^{\pe}}} % R plus étoile
\ExplSyntaxOn
\DeclareExpandableDocumentCommand{\IfNoValueOrEmptyTF}{mmm}
{
\IfNoValueTF{#1}{#2}
{
\tl_if_empty:nTF {#1} {#2} {#3}
}
}
\NewDocumentCommand{\ajoutParenthesesAux}{mmm}
{
\bool_lazy_and:nnTF { \tl_if_novalue_p:n { #2 } } { \tl_if_novalue_p:n { #3 } }
{
#1
}
{
\ajoutparentheses_add:nnn { #1 } { #2 } { #3 }
}
}
\bool_new:N \l__ajoutparentheses_sub_bool
\bool_new:N \l__ajoutparentheses_sup_bool
\bool_new:N \l__sub_bool
\bool_new:N \l__sup_bool
\prg_generate_conditional_variant:Nnn \str_if_in:nn { e } { T,F,TF }
\cs_new_protected:Nn \ajoutparentheses_add:nnn
{
\str_if_in:enTF { #1 } { _ }
{ \bool_set_true:N \l__ajoutparentheses_sub_bool }
{ \bool_set_false:N \l__ajoutparentheses_sub_bool }
\str_if_in:enTF { #1 } { ^ }
{ \bool_set_true:N \l__ajoutparentheses_sup_bool }
{ \bool_set_false:N \l__ajoutparentheses_sup_bool }
% \cs_log:N {1: ~ #1}
% \cs_log:N {2: ~ #2}
% \cs_log:N {3: ~ #3}
% \cs_log:N \l__ajoutparentheses_sub_bool
% \cs_log:N \l__ajoutparentheses_sup_bool
% \fbox{#3} \tl_if_blank:oTF { #3 } {=blanc} {≠blanc}\tl_if_empty:oTF { #3 } {=vide} {≠vide}\IfNoValueOrEmptyTF{ #3 }{NoV ~ ou ~ vide}{pas ~ NoV ~ ou ~ vide}
\IfNoValueOrEmptyTF{ #2 }{ \bool_set_false:N \l__sub_bool }{ \bool_set_true:N \l__sub_bool }
\IfNoValueOrEmptyTF{ #3 }{ \bool_set_false:N \l__sup_bool }{ \bool_set_true:N \l__sup_bool }
% \cs_log:N \l__sub_bool
% \cs_log:N \l__sup_bool
\bool_lazy_and:nnTF { \bool_lazy_or_p:nn { \l__ajoutparentheses_sub_bool }{ \l__ajoutparentheses_sup_bool } } { \bool_lazy_or_p:nn { \l__sub_bool }{ \l__sup_bool } }
{
\left(#1\right)\tl_if_novalue:nF { #2 } { \sb{#2} } \tl_if_novalue:nF { #3 } { \sp{#3} }
}
{
#1\tl_if_novalue:nF { #2 } { \sb{#2} } \tl_if_novalue:nF { #3 } { \sp{#3} }
}
}
\NewDocumentCommand{\nbElmts}{m}{
\clist_clear_new:N \l__estia_clist
\clist_set:Nn \l__estia_clist { #1 }
\int_compare:nNnTF { \clist_count:N \l__estia_clist } > 1 { \clist_count:N \l__estia_clist } { \c_empty_tl }
}
\NewDocumentCommand{\ajoutParenthesesListe}{m}{
\clist_clear_new:N \l__estia_clist
\clist_set:Nn \l__estia_clist { #1 }
\int_compare:nNnTF { \clist_count:N \l__estia_clist } > 1 { \liste(#1) } { #1 }
}
%\NewDocumentCommand{\dans}{smD(){\BbbR}e{_^}}{ % doesn't work since “e” type argument is changed within \ExplSyntax
% \tl_clear:N \l_tmpa_tl\l_tmpa_tl
% \IfValueTF{#5}{\tl_set:Nv \l_tmpa_tl { #5 }}{
% \clist_clear_new:N \l__estia_clist
% \clist_set:Nn \l__estia_clist { #2 }\tl_set:Nn \l_tmpa_tl {
% \int_compare:nNnTF { \clist_count:N \l__estia_clist } > 1 { \clist_count:N \l__estia_clist } {}
% }}
% % \tl_set:Nn \l_tmpa_tl { 5 }
% % \cs_log:N {#5 ~ \nbElmts{#2} ~ \l_tmpa_tl}
% \ensuremath{\ajoutParenthesesListe{#2}
% % \IfBooleanTF{#1}{\notin}{\in}\IfValueTF{#5}{\ajoutParenthesesAux{#3}{#4}{#5}}{\ajoutParenthesesAux{#3}{#4}{\nbElmts{#2}}}}}
% \IfBooleanTF{#1}{\notin}{\in}\ajoutParenthesesAux{#3}{#4}{\l_tmpa_tl}}}
\cs_new_protected:Npn \l_estia_formatListe:nnnnn #1#2#3#4#5
{
\clist_clear_new:N \l__estia_clist
\clist_map_inline:nn { #1 }
{
\str_if_eq:noTF { … } { \tl_to_str:n { ##1 } }
{ \clist_put_right:Nn \l__estia_clist { ##1 } }
{ \clist_put_right:Nn \l__estia_clist { #5 } }
}
\clist_use:Nnnn \l__estia_clist { #2 } { #3 } { #4 }
}
\NewDocumentCommand{\formatListe}{mO{,;}ooD(){##1}}{
\l_estia_formatListe:nnnnn { #1 }{ \IfNoValueOrEmptyTF{#2}{,;}{#2} } { \IfNoValueOrEmptyTF{#3}{#2}{#3} } { \IfNoValueOrEmptyTF{#4}{#2}{#4} } { #5 }
}
\ExplSyntaxOff
\begin{document}
\begin{tabular}{@{}lccr@{}}
\toprule
{\bfseries Cmd} & {\bfseries Output} & {\bfseries Should be}&\
\midrule
\verb*|\ajoutParentheses{aze}_1^2| & \ajoutParentheses{aze}_1^2 & $aze_1^2$ & right\\
\verb*|\ajoutParentheses{aze_1}_1^2| & \ajoutParentheses{aze_1}_1^2 & $\left(aze_1\right)_1^2$ & right\\
\verb*|\dans{x}| & \dans{x} & $x\in\BbbR$ & right\\
\verb*|\dans{x}(\BbbRpe)| & \dans{x}(\BbbRpe) & $x\in\BbbRpe$ & {\bfseries wrong}\\
\verb*|\dans{x_1}| & \dans{x_1} & $x_1\in\BbbR$ & right\\
\verb*|\dans{x_1}(\BbbRpe)| & \dans{x_1}(\BbbRpe) & $x_1\in\BbbRpe$ & {\bfseries wrong}\\
\verb*|\dans{x, y}| & \dans{x, y} & $\left(x, y\right)\in\BbbR^2$ & right\\
\verb*|\dans{x, y}(\BbbRpe)| & \dans{x, y}(\BbbRpe) & $\left(x, y\right)\in\left(\BbbRpe\right)^2$ & right\\
\verb*|\dans{U}^3| & \dans{U}^3 & $U\in\BbbR^3$ & right\\
\verb*|\dans{U, V}(\BbbR^3)| & \dans{U, V}(\BbbR^3) & $\left(U, V\right)\in\left(\BbbR^3\right)^2$ & right\\
\bottomrule
\end{tabular}
\end{document}
\BbbRpeis defined as "R plus étoile", and so does not have( )anywhere. Is that correct? – Cicada Jan 26 '23 at 09:17\BbbRpe. It should have parentheses if there are more than one elements in the first argument, as in\dans{x, y}(\BbbRpe). No parentheses in the two lines markedwrong. – NBur Jan 26 '23 at 09:46\dans{x}(\BbbRpe)in Output column (where parentheses are added manually), and$x\in\BbbRpe$in the "Should be" column. So no parentheses is expected output. Correct? Am I understanding correctly? – Cicada Jan 27 '23 at 06:28x, so no more exponent to be added on the set. However parentheses are added… – NBur Jan 27 '23 at 09:36\nbElmtsis not expandable, either rewrite it to be expandable or make it assign the result to some temporary variable. (you could debug this by doing something like\tl_show:n { #3 } \bool_show:n {\tl_if_novalue_p:n {#3}}so you see#3is\nbElmts{...}verbatim andif_novalueitself returns false – user202729 Jan 31 '23 at 17:41