AnswerDotAI / AnswerDotAI/nbdev

`show_doc` and `_non_empty_keys` fail when input default value is an array

Offen
#1,139 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug
Vorherrschende Sprache
Jupyter Notebook
Sterne
5.3k
Forks
514
Ø Merge
2 T. 30 Min.
Gemergte PRs (30 T.)
8

Beschreibung

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:

![image](https://user-images.githubusercontent.com/25931612/193239618-9bf64efa-4980-4738-ba7f-ce5fbdd4edf1.png)

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Der Traceback verweist auf showdoc.py und _non_empty_keys, die von show_doc verwendet werden; beginne dort und reproduziere das Problem mit der gezeigten Funktion mit NumPy-Standardwert. Prüfe, wie leere Standardwerte und Array-Standardwerte unterschieden werden, wenn Vergleiche möglicherweise keinen Skalar zurückgeben. Die Aufgabe ist erledigt, wenn show_doc das Beispiel ohne ValueError verarbeitet und dabei das bestehende Verhalten für andere Arten von Standardwerten beibehält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
jupyter, numpy, python
Bereich
documentation, tooling
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
42/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.