Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error handling in _build_lookup_table entry points #1474

Merged
merged 2 commits into from
Nov 29, 2024
Merged

Conversation

ncoop57
Copy link

@ncoop57 ncoop57 commented Nov 29, 2024

This PR adds error handling when attempting to resolve a particular entry point. When attempting to do this resolution, a particular library might throw an error. For example, this happens with a testcell throws an error due to how it attempts to look into the IPython registration of magics.

entries = {}
for o in pkg_resources.iter_entry_points(group='nbdev'):
    if incl_libs is not None and o.dist.key not in incl_libs: continue
    try: entries[o.name] = _qual_syms(o.resolve())
    except Exception: pass

I've added tests to explicitly mock a good entry point and a bad entry point to test this new functionality:

# Create mock entry points
class BadEntryPoint:
    name = 'bad_entry'
    dist = type('Dist', (), {'key': 'bad_lib'})()
    def resolve(self): raise AttributeError("Simulated error")

class GoodEntryPoint:
    name = 'good_entry'
    dist = type('Dist', (), {'key': 'good_lib'})()
    def resolve(self): return {'syms': {}, 'settings': {}}

from unittest.mock import patch as xpatch
# Clear the cache before testing
_build_lookup_table.cache_clear()

# Patch iter_entry_points
with xpatch('pkg_resources.iter_entry_points', return_value=[BadEntryPoint(), GoodEntryPoint()]):
    entries, py_syms = _build_lookup_table()
    
    # Should only contain the good entry
    assert 'bad_entry' not in entries
    assert 'good_entry' in entries
    assert len(entries) == 1
entries

And all tests are still passing.

Add try-except to handle errors and prevent entry point resolution failures
@ncoop57 ncoop57 requested a review from jph00 November 29, 2024 16:41
@jph00 jph00 merged commit aec2cfc into main Nov 29, 2024
10 checks passed
@jph00 jph00 deleted the error branch November 29, 2024 18:30
@jph00 jph00 added the enhancement New feature or request label Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants