This is an annoyance I’ve run into multiple times over many years while profiling python code and yesterday when I hit it again I also found another who encountered this problem: python - Why is pstats.print_callers() ignores the restriction argument? - Stack Overflow
Simply put: print_callers(*amount) only applies its provided filters to the top-level details (the callees for print_callers and callers for print_callees), and the second-level details are unfiltered and left sorted by name. If you want to print the top 20 callers of a function with 1000 callers and you run print_callers(funcname, 20), you will get the top 20 matches of funcname and each will have all 1000 callers. Hence, the nominal workflow is to print the results, copy them or read them back in, parse them by splitting on whitespace and casting to ints and floats, and then sort.
I’m proposing adding keyword arguments to print_callers and print_callees to allow sorting and filtering of the second-level details. Implementation doesn’t look too difficult since there’s already similar code that would be shared with sort_stats and get_print_line and mainly would touch print_call_line; I’m about to attempt it myself.