Instagram / Instagram/LibCST

Allow external codemods to be registered via setuptools entry points

Open
#434 0 comments 1 reaction 0 assignees View on GitHub
enhancement machinery
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

I'm looking at shipping some codemods in https://github.com/HypothesisWorks/hypothesis/issues/2705, to automatically upgrade uses of deprecated or just plain inefficient APIs, making it easier to migrate to newer versions of Hypothesis.

One thing I've noticed is that the *end user* has to explicitly declare where the codemods can be found - in my case, adding a list entry

```yaml
# List of modules that contain codemods inside of them.
modules:
- 'libcst.codemod.commands'
- 'hypothesis.extra.codemods'
```

I think it would be a better end-user experience if I could register the codemods from Hypothesis though, and I'd therefore like to propose [using setuptools entry points](https://amir.rachum.com/blog/2017/07/28/python-entry-points/) as another source of modules to search for codemods. Then

```python
# In Hypothesis' setup.py
entry_points={
"pytest11": ["hypothesispytest = hypothesis.extra.pytestplugin"],
"console_scripts": ["hypothesis = hypothesis.extra.cli:main"],
"libcst_codemod_modules": ["_= hypothesis.extra.codemods"],
},
```

and a small amount of code in LibCST, and we're done!

(for reference, almost the code in [Hypothesis' plugin loader](https://github.com/HypothesisWorks/hypothesis/blob/master/hypothesis-python/src/hypothesis/entry_points.py) is just to get `importlib.metadata`, `importlib_metadata`, or `pkg_resources` to give us the list in a generally compatible way... but you can reuse that logic, at least!)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.