I'm looking for how can I to type adequately the return value of __import__(module)
#1975
-
I'm looking for how can I type adequately the return value of I need to type a variable, which is bound to a module, to patch values correctly. from sys import modules
def get_patched_module(overwrite_value: str) -> Any:
import my.module
my_module = modules.pop('my.module')
my_module.value_to_overwrite = overwrite_value
return my_module As far as my understanding, the return type of the Does anyone know, or does Python lack a way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
There aren't great options...
|
Beta Was this translation helpful? Give feedback.
-
If you change the way your code works a bit (so that it accepts the module to modify as an argument, instead of importing it internally), then you can do this with a generic function: def patch_module[T](module: T, name: str, value: str) -> T:
setattr(module, name, value)
return module
import functools
functools = patch_module(functools, "wraps", "foo")
reveal_type(functools.wraps) Both mypy and pyright understand that the return value from Both are also fine with the |
Beta Was this translation helpful? Give feedback.
There aren't great options...
import my.module
will preserve the original modules typesif TYPE_CHECKING
hack at the call site, but this will be not so ergonomic