Skip to content

TypeError: unsupported operand type(s) for |: 'type' and 'type' in group plugin #6166

@pawamoy

Description

@pawamoy

Context

mkdocs-material 9.4.4+insiders.4.42.1

Bug description

The group plugin has this code in config.py:

plugins = Type(list | dict)

Even though it does import from __future__ import annotations, it's not enough to patch the runtime interpreter: types still can't be ORed with the pipe syntax at runtime on Python less than 3.10.

Using Type(Union[list, dict]) instead does not fix the issue. Without future annotations in group/plugin.py:

    File "/media/data/dev/copier-pdm/tests/tmp/__pypackages__/3.8/lib/material/plugins/group/plugin.py", line 36, in <module>
      class GroupPlugin(BasePlugin[GroupConfig]):
    File "/media/data/dev/copier-pdm/tests/tmp/__pypackages__/3.8/lib/material/plugins/group/plugin.py", line 120, in GroupPlugin
      def _patch(self, methods: list[Callable], config: MkDocsConfig):
  TypeError: 'type' object is not subscriptable

With future annotations:

    File "/media/data/dev/copier-pdm/tests/tmp/__pypackages__/3.8/lib/mkdocs/plugins.py", line 97, in load_config
      return self.config.validate()
    File "/media/data/dev/copier-pdm/tests/tmp/__pypackages__/3.8/lib/mkdocs/config/base.py", line 230, in validate
      run_failed, run_warnings = self._validate()
    File "/media/data/dev/copier-pdm/tests/tmp/__pypackages__/3.8/lib/mkdocs/config/base.py", line 188, in _validate
      self[key] = config_option.validate(value)
    File "/media/data/dev/copier-pdm/tests/tmp/__pypackages__/3.8/lib/mkdocs/config/config_options.py", line 182, in validate
      return self.run_validation(value)
    File "/media/data/dev/copier-pdm/tests/tmp/__pypackages__/3.8/lib/mkdocs/config/config_options.py", line 340, in run_validation
      if not isinstance(value, self._type):
    File "/home/pawamoy/.basher-packages/pyenv/pyenv/versions/3.8.18/lib/python3.8/typing.py", line 769, in __instancecheck__
      return self.__subclasscheck__(type(obj))
    File "/home/pawamoy/.basher-packages/pyenv/pyenv/versions/3.8.18/lib/python3.8/typing.py", line 777, in __subclasscheck__
      raise TypeError("Subscripted generics cannot be used with"
  TypeError: Subscripted generics cannot be used with class and instance checks

See discussion in MkDocs: mkdocs/mkdocs#3417

Related links

Reproduction

9.4.4+insiders.4.42.1-type-error-list-or-dict.zip

Steps to reproduce

With 3.9:

$ python3.9
Python 3.9.18 (main, Aug 26 2023, 14:52:42) 
[GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from material.plugins.group import config
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/media/data/dev/copier-pdm/tests/tmp/__pypackages__/3.9/lib/material/plugins/group/config.py", line 31, in <module>
    class GroupConfig(Config):
  File "/media/data/dev/copier-pdm/tests/tmp/__pypackages__/3.9/lib/material/plugins/group/config.py", line 33, in GroupConfig
    plugins = Type(list | dict)
TypeError: unsupported operand type(s) for |: 'type' and 'type'

With 3.10:

$ python3.10
Python 3.10.13 (main, Aug 26 2023, 14:53:11) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from material.plugins.group import config
>>>

Browser

No response

Before submitting

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue reports a bugresolvedIssue is resolved, yet unreleased if open

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions