Skip to content
This repository was archived by the owner on Oct 9, 2026. It is now read-only.
This repository was archived by the owner on Oct 9, 2026. It is now read-only.

[BUG] use of set as class attribute type causes exception #139

Description

@JoeHCQ1

Describe the bug
If you define a class attribute's type as set pytkdocs crashes.

To Reproduce

class Example:
    """The example class is a stand-in class for illustration"""

    my_legal_attribute: List[str]

    my_illegal_attribute: set[str]

Expected behavior
pytkdocs would parse the attributes without breaking.

Error text

ERROR    -  mkdocstrings: 'type' object is not subscriptable
            Traceback (most recent call last):
              File "/home/user/repo/avenv/lib/python3.8/site-packages/pytkdocs/cli.py", line 205, in main
                output = json.dumps(process_json(line))
              File "/home/user/repo/avenv/lib/python3.8/site-packages/pytkdocs/cli.py", line 114, in process_json
                return process_config(json.loads(json_input))
              File "/home/user/repo/avenv/lib/python3.8/site-packages/pytkdocs/cli.py", line 91, in process_config
                obj = loader.get_object_documentation(path, members)
              File "/home/user/repo/avenv/lib/python3.8/site-packages/pytkdocs/loader.py", line 358, in get_object_documentation
                root_object = self.get_module_documentation(leaf, members)
              File "/home/user/repo/avenv/lib/python3.8/site-packages/pytkdocs/loader.py", line 426, in get_module_documentation
                root_object.add_child(self.get_class_documentation(child_node))
              File "/home/user/repo/avenv/lib/python3.8/site-packages/pytkdocs/loader.py", line 483, in get_class_documentation
                merge(attributes_data, get_class_attributes(parent_class))
              File "/home/user/repo/avenv/lib/python3.8/site-packages/pytkdocs/parsers/attributes.py", line 115, in get_class_attributes
                type_hints = get_type_hints(cls)
              File "/usr/lib/python3.8/typing.py", line 1232, in get_type_hints
                value = _eval_type(value, base_globals, localns)
              File "/usr/lib/python3.8/typing.py", line 270, in _eval_type
                return t._evaluate(globalns, localns)
              File "/usr/lib/python3.8/typing.py", line 518, in _evaluate
                eval(self.__forward_code__, globalns, localns),
              File "<string>", line 1, in <module>
            TypeError: 'type' object is not subscriptable
ERROR    -  Error reading page 'reference/api/Example.md':
ERROR    -  Could not collect 'repo.Example'

System (please complete the following information):

  • pytkdocs version: 0.16.1
  • Python version: 3.8
  • OS: Linux

Workaround:

class Example:
    """The example class is a stand-in class for illustration"""

    my_legal_attribute: List[str]

    # my_illegal_attribute: set[str]
    my_illegal_attribute = set()

Activity

  1. pawamoy commented on Mar 8, 2022

    @pawamoy
    Member

    A few questions:

    • I believe you're using Python 3.8, can you confirm?
    • do you actually from __future__ import annotations which allows using types as subscriptable objects?
    • can you see if fix: Support future annotations #113 fixes it locally? it's a single line to add to pytkdocs/loader.py 🙂
  2. JoeHCQ1 commented on Mar 8, 2022

    @JoeHCQ1
    ContributorAuthor

    I can confirm I am using Python 3.8.

    I am using from __future__ import annotations.

    I tried your fix, which, added the line to my loader.py file like so:
    image

    And tried again, unfortunately I got the same error. ☹️

    I've had vs-code not save when I thought it did sometimes so I verified that the change was there via head ./avenv/lib/python3.8/site-packages/pytkdocs/loader.py and it was in fact there, just not helping.

    Thanks though 🙂

  3. pawamoy commented on Mar 8, 2022

    @pawamoy
    Member

    Thanks for trying! I'll get my hands into it as soon as I get some time 🙂

  4. pawamoy commented on Mar 30, 2022

    @pawamoy
    Member

    I can confirm that get_type_hints simply does not support future annotations on Python less than 3.9. So I'm inclined to say that the pytkdocs + future annotations combo is not supported on Python less than 3.9 😕
    If you feel like something could be improved in the code (to bring support or at least fail gracefully), please do open a PR, I will review it.

  5. JoeHCQ1 commented on Apr 1, 2022

    @JoeHCQ1
    ContributorAuthor

    Thanks for looking into this @pawamoy! I don't have any recommended changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions