jaraco / keyring

MIT License
1.26k stars 159 forks source link

Cannot List Backends #616

Closed hendursaga closed 1 year ago

hendursaga commented 1 year ago

I'm running Python 3.9.16 (under asdf-vm) and keyring 23.13.1, and cannot list backends from the command-line program. I'm not sure which backends, if any, are configured.

$ keyring --list-backends
Traceback (most recent call last):
  File "/home/user/.asdf/installs/python/3.9.16/bin/keyring", line 8, in <module>
    sys.exit(main())
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/keyring/cli.py", line 134, in main
    return cli.run(argv)
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/keyring/cli.py", line 59, in run
    for k in backend.get_all_keyring():
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/keyring/util/__init__.py", line 22, in wrapper
    func.always_returns = func(*args, **kwargs)
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/keyring/backend.py", line 215, in get_all_keyring
    _load_plugins()
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/keyring/backend.py", line 199, in _load_plugins
    for ep in metadata.entry_points(group='keyring.backends'):
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/importlib_metadata/__init__.py", line 856, in entry_points
    return EntryPoints(eps).select(**params)
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/importlib_metadata/__init__.py", line 853, in <genexpr>
    eps = itertools.chain.from_iterable(
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/importlib_metadata/_itertools.py", line 16, in unique_everseen
    k = key(element)
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/importlib_metadata/_py39compat.py", line 18, in normalized_name
    return dist._normalized_name
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/importlib_metadata/__init__.py", line 778, in _normalized_name
    or super()._normalized_name
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/importlib_metadata/__init__.py", line 445, in _normalized_name
    return Prepared.normalize(self.name)
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/site-packages/importlib_metadata/__init__.py", line 700, in normalize
    return re.sub(r"[-_.]+", "-", name).lower().replace('-', '_')
  File "/home/user/.asdf/installs/python/3.9.16/lib/python3.9/re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or bytes-like object
jaraco commented 1 year ago

You have corruption in your Python packaging environment. The issue you've encountered is known to happen if one of the Distribution objects returned has None for the name. You can replicate the issue without interacting with keyring by running python -c "import importlib_metadata; metadata.entry_points()".

I can see only one other environment where a similar issue occurred (https://github.com/python/importlib_metadata/issues/403), and I wasn't able to replicate their issue.

It's possible that upgrading importlib_metadata will help (pip install -U importlib_metadata).

If not, you'll probably need to dive into the internals of importlib_metadata and help ascertain the factors that cause the issue. Consider something like:

import importlib_metadata as md
for dist in md.distributions:
  print(dist.name, dist._path)