alibi-detect icon indicating copy to clipboard operation
alibi-detect copied to clipboard

Intermittent typing error in pytorch mmd.py

Open mauicv opened this issue 2 years ago • 1 comments

I get the following error intermittently both locally and in CI:

alibi_detect/cd/pytorch/mmd.py:120: error: Item "List[Any]" of "Union[ndarray[Any, Any], List[Any]]" has no attribute "shape"  [union-attr]
Found 1 error in 1 file (checked 182 source files)

Initially, I first saw it only locally when first setting up alibi-detect but then later it disappeared (Can't recall exactly why?). Subsequently while working on #539 it turned up in CI. I couldn't recreate it locally until I deleted and rebuilt the conda environment at which point it cropped up again?

The underlying error seems to be a result of us ignoring assignments earlier in the code block. For the purposes of completing #539 I've just ignored type-checking for that line.

mauicv avatar Jun 23 '22 09:06 mauicv

@mauicv I can't figure out why this error only appears intermittently (I actually can't reproduce locally), but it is part of a larger issue we have with x_ref/x being typed as Union[np.ndarray, list], but then np.ndarray being expected after preprocessing (we already have a lot of type ignore comments elsewhere to deal with this i.e. see score in LSDDDrift).

IMO we either need to enable --allow-redefinition with mypy, or rework the preprocessing variable names, for example change:

x_ref = torch.from_numpy(x_ref).to(self.device)

to

x_ref_torch = torch.from_numpy(x_ref).to(self.device)

(or change x_ref to x_ref_orig before preprocessing etc...)

ascillitoe avatar Jun 27 '22 11:06 ascillitoe