Skip to content

Compare to inspect.Parameter.empty by identity in docments - #949

Open
kevin9327 wants to merge 1 commit into
AnswerDotAI:mainfrom
kevin9327:fix/docments-identity-empty
Open

kevin9327 wants to merge 1 commit into
AnswerDotAI:mainfrom
kevin9327:fix/docments-identity-empty

Conversation

@kevin9327

Copy link
Copy Markdown

docments and DocmentTbl compare parameter defaults and annotations to inspect.Parameter.empty with == / !=. When a default's == doesn't return a plain bool, such as a numpy array, that comparison raises:

import numpy as np
from fastcore.docments import DocmentTbl
def f(a=np.array([1, 2])): ...
DocmentTbl(f)
# ValueError: The truth value of an array with more than one element is ambiguous.

This is the root cause of AnswerDotAI/nbdev#1322 (nbdev_readme / show_doc crashing on such a function). The traceback there points at the old nbdev copy of this code, but the same comparisons live in fastcore.docments now.

empty is a sentinel, so the fix compares by identity (is / is not) at every place in 04_docments.ipynb. fastcore/docments.py was regenerated with nbdev-export.

Test: a new cell uses a default whose __eq__/__ne__ raise, and checks docments(..., full=True) and DocmentTbl. It fails with ValueError before the change and passes after. nbdev-test nbs/04_docments.ipynb passes. The full nbdev-test run gives the same notebook failures with and without this change on my machine (network/optional deps: 02, 03, 03a, 03b, 05, 12, 13).

I used an AI assistant to help write this change; I reproduced the bug, checked the fix and ran the tests myself.

A default whose == can't be used as a bool (a numpy array, for one)
made docments and DocmentTbl raise ValueError, because they compared
values to empty with == and !=.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant