From dedbbe3f31a5b80c33e20e9b18eba7c5d000badb Mon Sep 17 00:00:00 2001 From: dwreeves Date: Mon, 30 Oct 2023 22:07:20 -0400 Subject: [PATCH] make rich-click cli work for python 3.12 --- .github/workflows/rich-codex.yml | 1 + .gitignore | 2 +- src/rich_click/_compat_click.py | 2 +- src/rich_click/cli.py | 21 +++++++++++++++++---- tests/test_help.py | 2 +- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rich-codex.yml b/.github/workflows/rich-codex.yml index 58fda2e1..b9bae841 100644 --- a/.github/workflows/rich-codex.yml +++ b/.github/workflows/rich-codex.yml @@ -7,6 +7,7 @@ on: jobs: rich_codex: + if: github.repository_owner == 'ewels' runs-on: ubuntu-latest steps: - name: Check out the repo diff --git a/.gitignore b/.gitignore index d58d2c48..3db4553d 100644 --- a/.gitignore +++ b/.gitignore @@ -277,7 +277,7 @@ ipython_config.py # pyenv # For a library or package, you might want to ignore these files since the code is # intended to run in multiple environments; otherwise, check them in: -# .python-version +.python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. diff --git a/src/rich_click/_compat_click.py b/src/rich_click/_compat_click.py index b5ca7794..89a13e74 100644 --- a/src/rich_click/_compat_click.py +++ b/src/rich_click/_compat_click.py @@ -2,7 +2,7 @@ from importlib import metadata # type: ignore[import,unused-ignore] except ImportError: # Python < 3.8 - import importlib_metadata as metadata # type: ignore[no-redef,import] + import importlib_metadata as metadata # type: ignore[no-redef,import-not-found] click_version = metadata.version("click") diff --git a/src/rich_click/cli.py b/src/rich_click/cli.py index 586289d9..963fc593 100644 --- a/src/rich_click/cli.py +++ b/src/rich_click/cli.py @@ -6,10 +6,10 @@ from typing import Any, List, Optional try: - from importlib.metadata import entry_points # type: ignore[import,unused-ignore] + from importlib import metadata # type: ignore[import,unused-ignore] except ImportError: - # Support Python <3.8 - from importlib_metadata import entry_points # type: ignore[import,no-redef] + # Python < 3.8 + import importlib_metadata as metadata # type: ignore[no-redef,import-not-found] import click from rich.console import Console @@ -73,6 +73,19 @@ def patch() -> None: click.MultiCommand = RichMultiCommand # type: ignore[assignment,misc] +def entry_points(*, group: str) -> "metadata.EntryPoints": # type: ignore[name-defined] + """entry_points function that is compatible with Python 3.7+.""" + if sys.version_info >= (3, 10): + return metadata.entry_points(group=group) + + epg = metadata.entry_points() + + if sys.version_info < (3, 8) and hasattr(epg, "select"): + return epg.select(group=group) + + return epg.get(group, []) + + def main(args: Optional[List[str]] = None) -> Any: """ The [link=https://github.com/ewels/rich-click]rich-click[/] CLI provides attractive help output from any @@ -100,7 +113,7 @@ def main(args: Optional[List[str]] = None) -> Any: sys.exit(0) else: script_name = args[0] - scripts = {script.name: script for script in entry_points().get("console_scripts", [])} + scripts = {script.name: script for script in entry_points(group="console_scripts")} if script_name in scripts: # a valid script was passed script = scripts[script_name] diff --git a/tests/test_help.py b/tests/test_help.py index 1a5933af..b08e315d 100644 --- a/tests/test_help.py +++ b/tests/test_help.py @@ -17,7 +17,7 @@ from importlib import metadata # type: ignore[import,unused-ignore] except ImportError: # Python < 3.8 - import importlib_metadata as metadata # type: ignore[no-redef,import] + import importlib_metadata as metadata # type: ignore[no-redef,import-not-found] rich_version = version.parse(metadata.version("rich"))