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

EAFP: Add try/except around each attempt to load discover_this #474

Closed
KyleKing opened this issue Jan 16, 2022 · 4 comments
Closed

EAFP: Add try/except around each attempt to load discover_this #474

KyleKing opened this issue Jan 16, 2022 · 4 comments

Comments

@KyleKing
Copy link
Contributor

I have an issue with pdocs, that the package being documented (cz_Legacy) isn't fully initialized when commitizen is looking for discover_this. I know this is an edge case and there are plans to improve plugins (#287), but I wondered if you would be open to a small rewrite:

plugins = {
name: importlib.import_module(name).discover_this # type: ignore
for finder, name, ispkg in pkgutil.iter_modules()
if name.startswith("cz_")
}

To something like this:

def _is_likely_a_cz_plugin(name: str) -> bool:
    import warnings
    try:
        importlib.import_module(name).discover_this
        return True
    except AttributeError:
        warnings.warn(f"{name} is not a commitizen plugin")
        return False

# ...

plugins = {
    name: importlib.import_module(name).discover_this  # type: ignore
    for finder, name, ispkg in pkgutil.iter_modules()
    if name.startswith("cz_") and _is_likely_a_cz_plugin(name)
}

Or could be something like:

# Could lru_cache, but probably won't make a difference
def _get_plugin_class(name: str):
    import warnings
    try:
        return importlib.import_module(name).discover_this
    except AttributeError:
        warnings.warn(f"{name} is not a commitizen plugin")

# ...

plugins = {
    name: _get_plugin_class(name)  # type: ignore
    for finder, name, ispkg in pkgutil.iter_modules()
    if name.startswith("cz_") and _get_plugin_class(name)
}
@woile
Copy link
Member

woile commented Jan 17, 2022

I'm in to improving the module section (it's not the best), though I don't understand what issue are you having with pdocs, could you elaborate on the error?

@KyleKing
Copy link
Contributor Author

I'm not sure. I think the issue is how pdocs surveys the available modules, so it could be from here: https://github.com/timothycrosley/pdocs/blob/c122cdf873aad4fa131bc7ed968da3dd393df43b/pdocs/extract.py#L37-L78 or somewhere else in the codebase

Suppressing the discover_this error for pdocs still properly shows the right code on my documentation site: https://cz_legacy.kyleking.me/modules/cz_legacy/cz_legacy/ and doesn't impact the plugins use otherwise

There are very few packages (maybe ~4 in total on pypi) with the prefix cz_ and no one has reported any issues, so while this change would preemptively address packages not related to commitizen, in practice that really isn't an issue

@KyleKing
Copy link
Contributor Author

Here is the full traceback:

~/Developer/packages/cz_legacy main ?1 > poetry run pdocs as_markdown cz_legacy
Error importing cz_legacy: partially initialized module 'cz_legacy' has no attribute 'discover_this' (most likely due to a circular import)

@Lee-W
Copy link
Member

Lee-W commented Mar 13, 2022

Hi @KyleKing , it seems https://github.com/commitizen-tools/commitizen/pull/480/files might have been tackled on https://github.com/commitizen-tools/commitizen/pull/480/files. I'll close this issue for now. But feel free to let me know or reopen the issue, if I misunderstand the purpose of this issue

@Lee-W Lee-W closed this as completed Mar 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants