create_baseline_stubs: improve package detection #7283
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
In current version, the
create_baseline_stubs.pyscript defaults to the project name as the package name. This is not always the best choice (e.g.PyYAMLandyaml), and the--packagecan be used in that case.In this PR, I'm improving the default package detection when there is no
--packageflag. Because this script is run manually, I feel like it is ok to display a message saying to use--packageflag if the detected package is not the wanted one.I checked with the following projects:
redisdefaults toredispython-nmapdefaults tonmapPyYAMLdefaults toyaml(note thatPyYAMLalso exports a_yamlmodule)The improvement relies on
importlib.metadatamodule, and fallbacks to current default (i.e. project name) if that fails or for Python < 3.8 (where that module does not exist).The code for package detection is slightly inspired by
packages_distributionsin Python >= 3.10.