9

I would like to introduce partial type annotation to my project. For example for overloading. I found that pep561 introduce partial stub file support.

I develop my project with PyCharm and I add corresponding *.pyi file. And got expected information, but PyCharm reports that cannot find reference in pyi file.

It is possible to force PyCharm to look to orginal py file when there is no entry in pyi file? Or maybe it is also doable with partial entry for class?

I create sample project to show problem (orginal is to big): cannot find reference 'CC' in '__init__.pyi'

├── main.py
└── pep561_test
    ├── __init__.py
    └── __init__.pyi

main.py

from pep561_test import AA, BB, CC

AA().test1(1)
AA().test1(True)
AA().test1('a')
AA().test2(1)

BB().test1(1)
BB().test2(1)

__init__.py

class AA:
    def test1(self, a):
        pass

    def test2(self, a):
        pass


class BB:
    def test1(self, a):
        pass

    def test2(self, a):
        pass


class CC:
    def test1(self, a):
        pass

    def test2(self, a):
        pass

__init__.pyi

class AA:
    def test1(self, a: int) -> int: ...

    def test1(self, a: bool) -> str: ...

    def test2(self, a):
        pass


class BB:
    def test1(self, a):
        pass
Grzegorz Bokota
  • 1,736
  • 11
  • 19
  • I don't think it's possible to merge definitions from `py` and `pyi` stub for the same file, from the PEP 561: "This can be thought of as the functional equivalent of copying the stub package into the same directory as the corresponding runtime package ... and type checking the combined directory structure". This way `__init__.pyi` will override `__init__.py`. `mypy` can't do it as well as PyCharm: `main.py:1: error: Module 'pep561_test' has no attribute 'CC'` – Pavel Karateev Mar 17 '19 at 14:58
  • Why can't you add the type hints to your regular `py` file? – Wombatz Dec 21 '20 at 18:12
  • This is a solution that I use when I ask these questions. The main thing why I prefer to use a separate file is keeping clean code. When I write this question I also need to use `# noinspection PyOverloads` when adding type annotations for autogenerated methods.. – Grzegorz Bokota Dec 21 '20 at 20:02
  • 2
    FYI: I created a corresponding issue https://youtrack.jetbrains.com/issue/PY-46104 – Alex Maystrenko Dec 22 '20 at 15:11

1 Answers1

3

Indicating to look in the .py for missing top-level reference (CC)

According to PEP 484, this is possible, simply add the following line at the top-level of your .pyi (I checked that it works with PyCharm 11.0.7, EDIT: does not work in PyCharm 2020.3):

def __getattr__(name) -> Any: ...

Indicating to look in the .py for missing attribute/method (BB)

Other libraries (at least typeshed) allow a similar syntax to merge an incomplete class stub with the class definition in the .py

class Foo:
    def __getattr__(self, name: str) -> Any: ...  # incomplete
    x: int
    y: str

However this does not seem to be part of PEP 484, and is not implemented in PyCharm (as of 11.0.7) as far as I know. I just created a request for this feature: I stumbled on this stackoverflow question while looking for a way to merge my incomplete class stub with the class definition and concluded that it is not feasible yet.

ernesttg
  • 41
  • 5
  • This does not work for me in PyCharm 2020.3 – Alex Maystrenko Dec 20 '20 at 11:28
  • 1
    Thanks for the feedback. I just tested it (in case you had made a mistake, or there was something wrong in your specific PyCharm config). But I confirm that the suggestion above does not work in PyCharm 2020.3 I'm leaving it, since that *should* be the solution (it's part of PEP 484) and might be fixed in future PyCharm version. – ernesttg Dec 21 '20 at 18:07
  • thanks for confirmation! Do you know in which Pycharm version it was working, in year.month notation? – Alex Maystrenko Dec 22 '20 at 01:34