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

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

Closed
4 tasks done
pawamoy opened this issue Oct 9, 2023 · 5 comments
Closed
4 tasks done
Labels
bug Issue reports a bug resolved Issue is resolved, yet unreleased if open

Comments

@pawamoy
Copy link
Sponsor Contributor

pawamoy commented Oct 9, 2023

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

@squidfunk
Copy link
Owner

Thanks for reporting. Sorry, no exceptions. Providing a minimal reproduction should be straight forward and makes it faster for us to isolate and fix the problem. Please provide one, we're happy to look into it then.

@squidfunk squidfunk added the needs reproduction Issue lacks a minimal reproduction .zip file label Oct 9, 2023
@pawamoy
Copy link
Sponsor Contributor Author

pawamoy commented Oct 9, 2023

I'm a bad user!
Done with 3.10 since the error happens at import time on 3.8/3.9 (like the mentioned issue) 🙂

@pawamoy pawamoy changed the title TypeError: unsupported operand type(s) for |: 'type' and 'type' (again) TypeError: unsupported operand type(s) for |: 'type' and 'type' in group plugin Oct 9, 2023
@squidfunk
Copy link
Owner

Thanks for reporting and providing the reproduction! Fixed in 4c192d3, together with another type annotation problem.

@squidfunk squidfunk added bug Issue reports a bug resolved Issue is resolved, yet unreleased if open and removed needs reproduction Issue lacks a minimal reproduction .zip file labels Oct 10, 2023
@squidfunk
Copy link
Owner

Released as part of 9.4.5.

@pawamoy
Copy link
Sponsor Contributor Author

pawamoy commented Oct 10, 2023

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue reports a bug resolved Issue is resolved, yet unreleased if open
Projects
None yet
Development

No branches or pull requests

2 participants