Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error parsing annotation: 'type' object is not subscriptable #677

Open
staceybellerose opened this issue Apr 7, 2024 · 2 comments
Open

Comments

@staceybellerose
Copy link

Problem Description

I receive the following warning when working with certain tkinter event annotations. In addition, the type hinting is tagged in the html as a string, and has quotes around it.

Warn: Error parsing type annotation tk.Event[tk.Text] for sample.MyText.open_link: 'type' object is not subscriptable (.../lib/python3.10/site-packages/pdoc/doc_types.py:122)

Sample code

from __future__ import annotations
import tkinter as tk
import webbrowser

class MyText(tk.Text):
    """Sample class."""
    def open_link(self, event: tk.Event[tk.Text]) -> None:
        """Open the clicked link in a web browser."""
        click_position = f"@{event.x},{event.y}"
        tag_range = event.widget.tag_nextrange("linkurl", click_position)
        webbrowser.open_new_tab(event.widget.get(*tag_range))
    def test(self, event: tk.Event) -> None:
        """Test event."""
        pass

Screenshot_2024-04-07_06-35-39

Steps to reproduce the behavior:

  1. Save code above as "sample.py"
  2. Run pdoc sample.py
  3. Warning appears in output, and type hinting in html is tagged as a literal string (span class="s1"), rather than a proper name (span class="n").

System Information

pdoc: 14.4.0
Python: 3.10.14
Platform: Linux-5.10.0-18-amd64-x86_64-with-glibc2.31

Notes

mypy version 1.9.0 correctly interprets the above sample code, and the VS Code mypy extension correctly indicates that event.widget is a tk.Text instance, allowing proper syntax highlighting on event.widget.* methods.

@mhils
Copy link
Member

mhils commented Apr 10, 2024

Where did you learn about this tk.Event[tk.Text] syntax?

This is not a pdoc bug, this is an issue with tkinter or with the annotation: If you open a Python shell and run tk.Event[tk.Text], you will get the same error. It's only due to from __future__ import annotations that you don't get errors when running your Python program.

@mhils mhils removed the bug label Apr 10, 2024
@staceybellerose
Copy link
Author

staceybellerose commented Apr 11, 2024

I saw it while using VS Code, and the mypy extension.

When hovering over a function, it showed a tooltip with the function signature, and all the parameters with their types. I think it's a feature of the Pylance extension to show the tooltips like this. One of them showed Event[Text]. See screenshot below for an example.

Adding it to my code allowed mypy to type check the inner members of an expression, like event.widget.tag_nextrange(), showing that tag_nextrange() is actually a method for the given widget. Since it didn't cause any issues running my code, I kept it and added it elsewhere. I was already using from __future__ import annotations to get the int | None type hinting functionality.

Screenshot_2024-04-11_02-49-58

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants