Back to Ruff

Invalid Protocol

crates/ty_python_semantic/resources/lint_docs/invalid-protocol.md

0.15.18634 B
Original Source

What it does

Checks for protocol classes that will raise TypeError at runtime.

Why is this bad?

An invalidly defined protocol class may lead to the type checker inferring unexpected things. It may also lead to TypeErrors at runtime.

Examples

A Protocol class cannot inherit from a non-Protocol class; this raises a TypeError at runtime:

pycon
>>> from typing import Protocol
>>> class Foo(int, Protocol): ...
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    class Foo(int, Protocol): ...
TypeError: Protocols can only inherit from other protocols, got <class 'int'>