We can only handle 1-D cases, which is tested here anyway.  So it would be clearer to extract the single dimension index "dim = dims[0]" right here, instead of leaving that to the code below.
(You would also need to use dim == None, but that works fine I think.)