1

I am building a project based on telethon library. I would like to use the optional typing with mypy, but it fails on all telethon imports

error: Skipping analyzing "telethon.tl.types": found module but no type hints or library stubs

I can see that there are some type hints in class'es constructors in telethon.tl.types.

I tried installing type hints with mypy --install-types, but no luck here.

Is it possible to use telethon in my project and benefit from the type annotations?

Edit: A simple test case to recreate the issue

from telethon.tl.types import WebPage

def example(example: WebPage) -> str:
    a: str = example.url
    return a
Alex Waygood
  • 6,304
  • 3
  • 24
  • 46
CucumisSativus
  • 310
  • 3
  • 11
  • you could explicitly type annotate variables. for example `client: TelegramClient` – Alen Paul Varghese Sep 11 '21 at 17:14
  • @AlenPaulVarghese unfortunately if I have it imported and I am using types from telethon in methods I still get this error. I added a small piece of code which fails on mypy check even thought I annotated everything I could – CucumisSativus Sep 11 '21 at 17:27
  • 3
    use `--ignore-missing-imports` flag with mypy to ignore this issue. – Alen Paul Varghese Sep 11 '21 at 17:30
  • @AlenPaulVarghese Now, the whole idea is clear to me. I went with adding comments to the problematic lines. If you create an answer with this suggestion i am happy to accept it :) – CucumisSativus Sep 11 '21 at 17:49
  • I don't get the issue, are you facing any other problem other than mypy error? – Alen Paul Varghese Sep 11 '21 at 18:06
  • The `telethon` package has type hints, but doesn't make them visible to type checkers since having no PEP 561 support. You can add it as a quick workaround: `python -c 'import telethon; print(telethon.__path__[0]) | xargs -I [] touch []/py.typed`. Test whether type hints are recognized now: smth like `mypy -c 'from telethon.types import WebPage; reveal_type(WebPage().url)'` should output `str`. – hoefling Sep 12 '21 at 10:40
  • Of course, a better solution would be either add a PR to telethon that adds PEP 561 support, or create local incomplete stubs and add them to `mypy_path` to point `mypy` to type hints in the installed sources. Creating `py.typed` manually has the drawback that the file isn't managed by `pip` and will stay if e.g. `pip uninstall telethon` is executed. – hoefling Sep 12 '21 at 10:44

0 Answers0