12

I want to know how I can add type hints (for PyCharm IDE code completion support) to a method def links(self) -> List[str] that I monkey-patched to an existing module's class:

My function

def issue_links(self) -> List[str]:
    links = []
    # ...
    return links

Monkey-patching the function to the Issue class of python-jira

from jira.resources import Issue

# {...} my defined function code is somewhere here

Issue.links = issue_links

Now I have the problem that PyCharm obviously not recognise this method when I call it on an Issue object. I tried to follow PEP 484 Stub Files and using the typing module in a .pyi file to get the IDE to find the definition of my monkey-patched function.

Issue.pyi

from typing import List

class Issue:
    def links(self) -> List[str]: ...

However, it won't work. My assumption is that the file structure is somehow not correct:

File Locations

jira-python module >> site packages (downloaded with pip install jira)

myFile.py >> /temp/myFile.py

Issue.pyi >> /temp/jira/resources/Issue.pyi folder

Christopher Graf
  • 1,929
  • 1
  • 17
  • 34
  • What IDE are you using? First you state that the typing may be used for IDE completion but later on the question you only talk about the IDE. You need to specify where your stubs are located. In `mypy` you do it with the `mypy_path` config option. See https://mypy.readthedocs.io/en/latest/config_file.html#config-file-import-discovery. IDEs may or may not recognize the config options from mypy. – Sebastian Kreft Nov 15 '19 at 19:22
  • @SebastianKreft PyCharm. I am not using `mypy` though. I hoped I could simply get this to work by adding the `.pyi` file somewhere near the `.py` file where I am using the monkey-patched function. – Christopher Graf Nov 15 '19 at 19:55
  • 1
    Did you follow the steps explained in the documentation? https://www.jetbrains.com/help/pycharm/stubs.html – Sebastian Kreft Nov 15 '19 at 21:22
  • @SebastianKreft that fixed the problem that it won't find the stub. However, it now only recognises the interface definition of the function I defined in there. The existing definitions (provided via DocString by the module developer are not used anymore for code completion). Can't both coexist? Meaning, can't my `.pyi` stub not extend the type hints already existing? Do I have to provide type hints for all members of the module now in my `.pyi` stub? Please feel free to post your answer(s) including the "RTFM" post as an answer, so you can get credit :) – Christopher Graf Nov 16 '19 at 07:59
  • @SebastianKreft additional remark: it also does not code complete the general property/method call. It only recognises `links()` as an available member of the class. So it seem the `.pyi` file overrules/overwrites all other IDE hints? – Christopher Graf Nov 16 '19 at 09:38
  • 1
    After some additional reading, I believe it is not possible to have typing hints from my `.pyi` file and the existing typing hints from the module combined... – Christopher Graf Nov 16 '19 at 17:42

0 Answers0