bpo-1284670: Allow to restrict ModuleFinder to get direct dependencies by MaxwellDupre · Pull Request #30228 · python/cpython (original) (raw)
I don't think this does what the bug report asks for. In your test case, a.py imports b, b imports c, c import sys. The reporter wants to see only the things directly imported by a.py, so the returned list should be ["a", "b"]
. But your test case instead returns c and sys too.
Your implementation makes scan_code not recurse into nested code objects. But that doesn't mean it won't recur into imported files (as the report asks for); it means we won't look at imports inside classes or functions. The recursion we'd actually want to disable goes through scan_code -> _safe_import_hook -> import_hook -> load_tail -> import_module -> load_module -> scan_code.
Last, I'm not convinced we should make this change at all. modulefinder is a rarely used module and the enhancement request is very old and hasn't gotten a lot of feedback.