Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyPy checking typing.Protocol with Python 3.7 Support

I have a project which needs to support Python 3.7, but I would like to use typing.Protocol, which was added in 3.8. To support 3.7, I have a minor bit of fallback code which just uses object:

import typing

_Protocol = getattr(typing, 'Protocol', object)

class Foo(_Protocol):
    def bar(self) -> int:
        pass

This all functions as I would expect. The issue is, when running MyPy, I get the following error:

test.py:5: error: Variable "test._Protocol" is not valid as a type
test.py:5: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
test.py:5: error: Invalid base class "_Protocol"

The linked "Variables vs type aliases" section in that error message indicates that I should annotate _Protocol with : typing.Type[object] (which does not work) or use typing.TypeAlias (which isn't available until Python 3.9).

How can I indicate to MyPy that _Protocol is valid as a type?


Another workaround I tried was in "Python version and system platform checks":

if sys.version_info >= (3, 8):
    _Protocol = typing.Protocol
else:
    _Protocol = object

However, this ends with the same error.

like image 889
Travis Gockel Avatar asked Mar 05 '26 20:03

Travis Gockel


1 Answers

Use typing_extensions and typing.TYPE_CHECKING to import typing_extensions only when type-checking the code.

import typing

if typing.TYPE_CHECKING:
    from typing_extensions import Protocol
else:
    Protocol = object


class Foo(Protocol):
    def bar(self) -> int:
        ...

typing_extensions checks the Python version and uses typing.Protocol for versions >=3.8:

# 3.8+
if hasattr(typing, 'Protocol'):
    Protocol = typing.Protocol
# 3.7
else:
    ...
like image 126
Paweł Rubin Avatar answered Mar 08 '26 08:03

Paweł Rubin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!