ManderaGeneral / generalimport

Handle all your optional dependencies with a single call!
https://pypi.org/project/generalimport/
Apache License 2.0
15 stars 1 forks source link

Allow typing from missing dependency #30

Closed ZanSara closed 1 year ago

ZanSara commented 1 year ago

Problem

In Haystack we often need to type method signatures with missing dependencies. Something like:

import numpy as  np
import torch

def my_complicated_function(device: Optional[torch.device]) -> Union[torch.tensor, np.ndarray]:
    ...

I found that torch.device, torch.tensor and np.ndarray trigger generalimport once wrapped in a container like Optional or Union, while they don't when they're unwrapped, like:

import numpy as  np
import torch

def my_complicated_function(device: torch.device) -> torch.tensor, np.ndarray:
    ...

Solution

I found that adding __args__ = [] in FakeModule seems to fix the issue. I'm going to open a small PR for this case.