Repository: airctic/icevision

Issue:
autogen.py fails on tfms.A.aug_tfms
## 🐛 Bug
**Describe the bug**
Running `autogen.py` fails with the stack trace attached at the bottom.

The function causing the problem is `aug_tfms` in `tfms/albumentations/tfms.py`. I'm not sure what the problem is.

```python
Removing sources folder: /home/lgvaz/git/mantisshrimp/docs/sources
Cleaning up existing sources directory.
Populating sources directory with templates.
/home/lgvaz/miniconda3/envs/mantis/lib/python3.7/site-packages/sphinx/util/inspect.py:555: RemovedInSphinx40Warning: sphinx.util.inspect.Signature() is deprecated
  RemovedInSphinx40Warning)
...creating new page with autogenerated content: /home/lgvaz/git/mantisshrimp/docs/sources/parser.md
Traceback (most recent call last):
  File "autogen.py", line 282, in <module>
    generate(mantisshrimp_dir / "docs" / "sources")
  File "autogen.py", line 226, in generate
    doc_generator.generate(dest_dir)
  File "/home/lgvaz/miniconda3/envs/mantis/lib/python3.7/site-packages/keras_autodoc/autogen.py", line 82, in generate
    markdown_text += self._render(element)
  File "/home/lgvaz/miniconda3/envs/mantis/lib/python3.7/site-packages/keras_autodoc/autogen.py", line 109, in _render
    return self._render_from_object(object_, signature_override)
  File "/home/lgvaz/miniconda3/envs/mantis/lib/python3.7/site-packages/keras_autodoc/autogen.py", line 116, in _render_from_object
    object_, signature_override, self.max_signature_line_length
  File "/home/lgvaz/miniconda3/envs/mantis/lib/python3.7/site-packages/keras_autodoc/get_signatures.py", line 53, in get_signature
    return get_function_signature(object_, override, max_line_length)
  File "/home/lgvaz/miniconda3/envs/mantis/lib/python3.7/site-packages/keras_autodoc/get_signatures.py", line 37, in get_function_signature
    return format_signature(signature_start, signature_end, max_line_length)
  File "/home/lgvaz/miniconda3/envs/mantis/lib/python3.7/site-packages/keras_autodoc/get_signatures.py", line 68, in format_signature
    formatted_fake_python_code = black.format_str(fake_python_code, mode=mode)
  File "/home/lgvaz/miniconda3/envs/mantis/lib/python3.7/site-packages/black.py", line 725, in format_str
    src_node = lib2to3_parse(src_contents.lstrip(), mode.target_versions)
  File "/home/lgvaz/miniconda3/envs/mantis/lib/python3.7/site-packages/black.py", line 836, in lib2to3_parse
    raise exc from None
black.InvalidInput: Cannot parse: 1:712: def xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(size, presize=None, horizontal_flip=HorizontalFlip(always_apply=False, p=0.5), shift_scale_rotate=ShiftScaleRotate(always_apply=False, p=0.5, shift_limit=(-0.0625, 0.0625), scale_limit=(-0.09999999999999998, 0.10000000000000009), rotate_limit=(-45, 45), interpolation=1, border_mode=4, value=None, mask_value=None), rgb_shift=RGBShift(always_apply=False, p=0.5, r_shift_limit=(-20, 20), g_shift_limit=(-20, 20), b_shift_limit=(-20, 20)), lightning=RandomBrightnessContrast(always_apply=False, p=0.5, brightness_limit=(-0.2, 0.2), contrast_limit=(-0.2, 0.2), brightness_by_max=True), blur=Blur(always_apply=False, p=0.5, blur_limit=(1, 3)), crop_fn=functools.partial(<class 'albumentations.augmentations.transforms.RandomSizedBBoxSafeCrop'>, p=0.5), pad=functools.partial(<class 'albumentations.augmentations.transforms.PadIfNeeded'>, border_mode=0, value=[124, 116, 104])):
```



Discussion:
The problem here is that `keras-autodoc` is internally using `black` to format the code, but when using partials we have the following function signature:

```python
...pad=functools.partial(<class 'albumentations.augmentations.transforms.PadIfNeeded'>,...
```

What happens is that `<class...` is not valid python, and then black fails to format


A solution is to somehow monkey-patch `partial` signature to look like:
```python
functools.partial(albumentations.augmentations.transforms.PadIfNeeded...
```


Resolving patch:
diff --git a/docs/autogen.py b/docs/autogen.py
--- a/docs/autogen.py
+++ b/docs/autogen.py
@@ -26,7 +26,7 @@
         "mantisshrimp.data.dataset.Dataset.from_images",
     ],
     "albumentations_tfms.md": [
-        #     "mantisshrimp.tfms.albumentations.aug_tfms",
+        "mantisshrimp.tfms.albumentations.aug_tfms",
         "mantisshrimp.tfms.albumentations.Adapter",
     ],
     # "coco_metric.md": [
diff --git a/mantisshrimp/imports.py b/mantisshrimp/imports.py
--- a/mantisshrimp/imports.py
+++ b/mantisshrimp/imports.py
@@ -45,3 +45,25 @@
 
 if SoftDependencies.pytorch_lightning:
     import pytorch_lightning as pl
+
+
+# TODO: Stop importing partial from fastcore and move this to utils
+class partial:
+    """ Wraps functools.partial, same functionality.
+
+    Modifies the original partial `__repr__` and `__str__` in other to fix #270
+    """
+
+    def __init__(self, func, *args, **kwargs):
+        self._partial = functools.partial(func, *args, **kwargs)
+
+    def __call__(self, *args, **kwargs):
+        return self._partial(*args, **kwargs)
+
+    def __str__(self):
+        name = self._partial.func.__name__
+        partial_str = str(self._partial)
+        return re.sub(r"<.+>", name, partial_str)
+
+    def __repr__(self):
+        return str(self)


Test patch: