AnswerDotAI / AnswerDotAI/nbdev

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

Aperta
#1,139 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
Jupyter Notebook
Stelle
5.3k
Fork
513
Merge medio
2g 30m
PR unite (30g)
8

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Il traceback punta a showdoc.py e _non_empty_keys, utilizzati da show_doc; inizia da lì e riproduci il problema con la funzione mostrata con un valore predefinito NumPy. Verifica come vengono distinti i valori predefiniti vuoti dai valori predefiniti degli array quando i confronti potrebbero non restituire uno scalare. Il lavoro è completato quando show_doc gestisce l’esempio senza ValueError, preservando il comportamento esistente per gli altri tipi di valori predefiniti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
jupyter, numpy, python
Ambito
documentation, tooling
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
42/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.