AnswerDotAI / AnswerDotAI/nbdev
`show_doc` and `_non_empty_keys` fail when input default value is an array
- Dominant language
- Jupyter Notebook
- Stars
- 5.3k
- Forks
- 513
- Avg merge
- 2d 30m
- Merged PRs (30d)
- 8
Description
Hi, I opened an [issue in the fastcore repo](https://github.com/fastai/fastcore/issues/489) but after digging into the problem, it may be better suited here.
As commented there, when you a function with a default numpy array input like:
```
def foo(c: np.array = np.arange(4)
):
'''
Test function with numpy default
'''
```
you end up with the following error.
```
---------------------------------------------------------------------------
[ ..... other unnecessary error info ......]
File ~\miniconda3\lib\site-packages\nbdev\showdoc.py:25, in (.0)
---> 25 def _non_empty_keys(d:dict): return L([k for k,v in d.items() if v != inspect._empty])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
```
The problem arises in `_non_empty_keys`, when this function checks whether the default value of the given variable is empty with `v != inspect._empty`. When the default `v` is a `np.array` of size bigger than one, the previous operation outputs a `np.array`, which makes that the if statements fails, as shown here:

When working only with arrays, using `.any()` or `.all()` solves the issue. However, in this case is not applicable as `v` can be of any type. See that for most of the other defaults, the inequality outputs a `bool`. I don't exactly know what would be the preferred way to solve this issue, that works for any `v` type.
Contributor guide
Research direction
The traceback points to showdoc.py and _non_empty_keys, used by show_doc; start there and reproduce the issue with the NumPy-default function shown. Check how empty defaults and array defaults are distinguished when comparisons may not return a scalar. Done means show_doc handles the example without ValueError while preserving existing behavior for other default types.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- jupyter, numpy, python
- Domain
- documentation, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100