I'm wondering if it's possible to get PyCharm to support code completion for monkey patched functions?
I have the following class:
class _PathExt(Path):
def ensure_exists(self):
if not self.exists():
if not self.parent.exists():
os.makedirs(self.parent.absolute(), exist_ok=True)
os.makedirs(self.absolute())
return self
def subdir(self, sub_name):
return Path(os.path.join(self.absolute(), sub_name))
Which I then monkey patch with:
def monkey_patch(from_type, to_type):
for name in [x for x in dir(from_type) if not x.startswith('_') and x not in dir(to_type)]:
value = from_type.__getattribute__(from_type, name)
if inspect.isfunction(value):
setattr(to_type, name, value)
monkey_patch(_PathExt, Path)
It's understandable that this wouldn't work as it's dynamic, so I then tried:
Path.subdir = _PathExt.subdir
But still PyCharm won't detect it. Is this possible at all?
Edit:
In this specific case I've decided to derive my class from Path and then import my class with an as alias, so it's a drop in replacement, but I'd still like to know if anything can be done to support monkey patching in PyCharm.