Replace the alias with a function.
testalias() {
python3 -c 'f = open("/tmp/testopenfile", "w+"); f.write("hi\n")'
}
detach ()
{
"$@" > /dev/null 2> /dev/null &
}
Aliases areBash only looks at the first word of a command for alias expansion, so the function gets the literal argument testalias. (I think zsh has "global" aliases which would be expanded anywhere on the command line, but I doubt you'd want e.g. echo testalias to expand the alias contents.)
Alias expansion also happens early in the parsing process, way before $1 is expanded, so when the function runs, $1 expands to just the same testalias, and stays like that. It would probably give you an error about not finding the command testalias, except that stderr was redirected to /dev/null, so you don't see the error.
In fact, an alias in a function is expanded when the function is parsed, not when it's used.
$ alias foo="echo abc"
$ f() { foo; }
$ alias foo="echo def"
$ g() { foo; }
$ f
abc
$ g
def
With testalias a function, it's found when the testalias command is looked up.