Determining top-level import package names programmatically

For wheels you could check the top-level directory names (filtering out .data and .dist-info), either from the ZIP, via the RECORD, etc. Setuptools also includes a top_level.txt file in the .dist-info that is simply a newline-deliminated list of top-level import package names.

For sdists, Setuptools-produced sdists include a similar top_level.txt in the .egg-info directory; that wouldn’t work for non-Setuptools sdists, but I’d wager only a tiny fraction of modern projects built with non-Setuptools build systems aren’t uploading wheels, particularly those that are at least reasonably popular, and you could always just build a wheel with build and then do the above.

It has been proposed (which also includes some discussion of how one can extract that currently, including in a scenario such as this).

Not sure how this is relevant, sorry? Whether or not they happen to use the discouraged dynamic build script feature of the Setuptools backend, any valid sdists must still be buildable, and will either include a pyproject.toml or will fall back to the legacy Setuptools builder, and you can just use build as normal to build a wheel and check via the methods specified above.