| 2014 | # This metaclass is somewhat unfortunate, |
| 2015 | # but is necessary for several reasons... |
| 2016 | def __new__(mcls, name, bases, namespace, /, **kwargs): |
| 2017 | if name == "Protocol" and bases == (Generic,): |
| 2018 | pass |
| 2019 | elif Protocol in bases: |
| 2020 | for base in bases: |
| 2021 | if not ( |
| 2022 | base in {object, Generic} |
| 2023 | or base.__name__ in _PROTO_ALLOWLIST.get(base.__module__, []) |
| 2024 | or ( |
| 2025 | issubclass(base, Generic) |
| 2026 | and getattr(base, "_is_protocol", False) |
| 2027 | ) |
| 2028 | ): |
| 2029 | raise TypeError( |
| 2030 | f"Protocols can only inherit from other protocols, " |
| 2031 | f"got {base!r}" |
| 2032 | ) |
| 2033 | return super().__new__(mcls, name, bases, namespace, **kwargs) |
| 2034 | |
| 2035 | def __init__(cls, *args, **kwargs): |
| 2036 | super().__init__(*args, **kwargs) |