Skip to content

Commit

Permalink
Ensure Python 3.7 compatibility in check_console_script (kislyuk#436)
Browse files Browse the repository at this point in the history
* Ensure Py3.7 compatibility in check_console_script

The importlib backport, `importlib_metadata`, returns a tuple of
entry points instead of a dictionary keyed by entry group. This inconsistency
caused a KeyError exception to be thrown. To resolve this issue, the script has
been adjusted to maintain compatibility with Python 3.7, as specified in
`setup.py`.
  • Loading branch information
flu0r1ne committed Jun 5, 2023
1 parent 8af749c commit f4d046c
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion argcomplete/_check_console_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

try:
from importlib.metadata import entry_points as importlib_entry_points
from importlib.metadata import EntryPoint
use_entry_points_backport = False
except ImportError:
from importlib_metadata import entry_points as importlib_entry_points # type:ignore
from importlib_metadata import EntryPoint # type:ignore
use_entry_points_backport = True

from ._check_module import ArgcompleteMarkerNotFound, find
from typing import Iterable


def main():
Expand All @@ -29,7 +34,17 @@ def main():
# Find the module and function names that correspond to this
# assuming it is actually a console script.
name = os.path.basename(script_path)
entry_points = [ep for ep in importlib_entry_points()["console_scripts"] if ep.name == name]

entry_points : Iterable[EntryPoint] = importlib_entry_points() # type:ignore

# The importlib_metadata backport returns a tuple of entry point objects
# whereas the official library returns a SelectableGroups object
if not use_entry_points_backport:
entry_points = entry_points["console_scripts"] # type:ignore

entry_points = [ep for ep in entry_points \
if ep.name == name and ep.group == "console_scripts" ] # type:ignore

if not entry_points:
raise ArgcompleteMarkerNotFound("no entry point found matching script")
entry_point = entry_points[0]
Expand Down

0 comments on commit f4d046c

Please sign in to comment.