Skip to content

Conversation

@GSVarsha
Copy link
Contributor

@GSVarsha GSVarsha commented Jul 29, 2025

setuptools is not included with the standard library since Python>=3.12

Although importlib.metadata was introduced in python 3.8, we get the below error in Python < 3.10

entry_points() got an unexpected keyword argument 'group'

Why?

In Python 3.12 and later, since setuptools is not included in standard library, discover_post_import_hooks() fails when setuptools is not installed in the environment.
It took so much time to find that this is the actual problem because the function just returns bluntly and dynamic import at runtime with autowrapt simply didn't work ☹️.

@GSVarsha
Copy link
Contributor Author

Fixes #291

@grayjk
Copy link
Contributor

grayjk commented Jul 29, 2025

@GSVarsha

To handle the unexpected keyword argument:

if sys.version_info < (3, 10):
    def entry_points(*, group):
        return importlib.metadata.entry_points().get(group, ())
else:
    entry_points = importlib.metadata.entry_points

@GSVarsha
Copy link
Contributor Author

@grayjk

Thanks for the suggestion. Implemented the same with a try-except block.

@GSVarsha GSVarsha changed the title Remove the dependency on pkg_resources and hence setuptools on Python>=3.10 Remove the dependency on pkg_resources and hence setuptools Jul 30, 2025
@GrahamDumpleton GrahamDumpleton merged commit b0c6789 into GrahamDumpleton:master Aug 1, 2025
0 of 28 checks passed
@GrahamDumpleton
Copy link
Owner

Ah crap. Didn't see you had this PR against master. Couldn't understand why GitHub actions was failing as couldn't see why it was based off old version of code, so merged it unthinkingly. 😩

I'll sort it out, but keep in mind that PRs should be against develop branch.

@GrahamDumpleton
Copy link
Owner

With a bit of AI copilot help have managed to clean up master. Now to work out how I can add a test which checks this case. I don't think anything I have no will strictly test this as these entry points are usually defined in separate packages.

@GrahamDumpleton
Copy link
Owner

FWIW, when trying to work out some tests, the AI copilot flagged that code could be written as:

def discover_post_import_hooks(group):
    from importlib.metadata import entry_points

    try:
        # Python 3.10+ style with select parameter
        entrypoints = entry_points(group=group)
    except TypeError:
        # Python 3.8-3.9 style that returns a dict
        entrypoints = entry_points().get(group, ())
        
    for entrypoint in entrypoints:
        callback = entrypoint.load()  # Use the loaded callback directly
        register_post_import_hook(callback, entrypoint.name)

Still evaluating this as well as whether tests it generated even test it. 🤣

@GSVarsha
Copy link
Contributor Author

GSVarsha commented Aug 1, 2025

Oh my bad, will keep that in mind. I saw that the package we install from pip contained the old code from master and that's what I checked my code with too.

I actually checked my code with this on instana

This alternative works as well

        callback = entrypoint.load()  # Use the loaded callback directly
        register_post_import_hook(callback, entrypoint.name)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants