У меня есть следующий код
from typing import Protocol, runtime_checkable
@runtime_checkable
class A(Protocol):
def f(self): ...
class B:
def f(self): ...
if __name__ == '__main__':
assert isinstance(B(), A)
Если я удалю декоратор runtime_checkable, я получу следующее исключение:
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
Однако, если я добавлю атрибут в класс A, декоратор не понадобится.
class A(Protocol):
x: int
def f(self): ...
результат:
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
AssertionError
Протокол, который содержит хотя бы один член, не являющийся методом, называется протоколом данных (typing.python.org). Почему протоколам данных не нужен декоратор runtime_checkable? Я не могу найти ничего об этом в документации.
РЕДАКТИРОВАТЬ:
без атрибута:
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Protocol
>>> class A(Protocol):
... def f(self): ...
...
>>> class B:
... def f(self): ...
...
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\typing.py", line 1146, in __instancecheck__
issubclass(instance.__class__, cls)):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\abc.py", line 123, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\typing.py", line 1208, in _proto_hook
raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
с атрибутом:
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Protocol
>>> class A(Protocol):
... x: int
... def f(self): ...
...
>>> class B:
... def f(self): ...
...
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
AssertionError
В версии Python >= 3.10 я получаю ошибку TypeError:
Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Protocol
>>> class A(Protocol):
... x: int
... def f(self): ...
...
>>> class B:
... def f(self): ...
...
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\typing.py", line 1498, in __instancecheck__
raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
3.12
Python 3.12.10 (tags/v3.12.10:0cc8128, Apr 8 2025, 12:21:36) [MSC v.1943 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Protocol
>>> class A(Protocol):
... x: int
... def f(self): ...
...
>>> class B:
... def f(self): ...
...
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.2800.0_x64__qbz5n2kfra8p0\Lib\typing.py", line 1917, in __instancecheck__
raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
Подробнее здесь: https://stackoverflow.com/questions/799 ... -checkable
Typing.Protocol и typing.runtime_checkable ⇐ Python
Программы на Python
-
Anonymous
1773087059
Anonymous
У меня есть следующий код
from typing import Protocol, runtime_checkable
@runtime_checkable
class A(Protocol):
def f(self): ...
class B:
def f(self): ...
if __name__ == '__main__':
assert isinstance(B(), A)
Если я удалю декоратор runtime_checkable, я получу следующее исключение:
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
Однако, если я добавлю атрибут в класс A, декоратор не понадобится.
class A(Protocol):
x: int
def f(self): ...
результат:
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
AssertionError
Протокол, который содержит хотя бы один член, не являющийся методом, называется протоколом данных (typing.python.org). Почему протоколам данных не нужен декоратор runtime_checkable? Я не могу найти ничего об этом в документации.
РЕДАКТИРОВАТЬ:
без атрибута:
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Protocol
>>> class A(Protocol):
... def f(self): ...
...
>>> class B:
... def f(self): ...
...
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\typing.py", line 1146, in __instancecheck__
issubclass(instance.__class__, cls)):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\abc.py", line 123, in __subclasscheck__
return _abc_subclasscheck(cls, subclass)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\typing.py", line 1208, in _proto_hook
raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
с атрибутом:
Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Protocol
>>> class A(Protocol):
... x: int
... def f(self): ...
...
>>> class B:
... def f(self): ...
...
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
AssertionError
В версии Python >= 3.10 я получаю ошибку TypeError:
Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Protocol
>>> class A(Protocol):
... x: int
... def f(self): ...
...
>>> class B:
... def f(self): ...
...
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\typing.py", line 1498, in __instancecheck__
raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
3.12
Python 3.12.10 (tags/v3.12.10:0cc8128, Apr 8 2025, 12:21:36) [MSC v.1943 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Protocol
>>> class A(Protocol):
... x: int
... def f(self): ...
...
>>> class B:
... def f(self): ...
...
>>> assert isinstance(B(), A)
Traceback (most recent call last):
File "", line 1, in
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.2800.0_x64__qbz5n2kfra8p0\Lib\typing.py", line 1917, in __instancecheck__
raise TypeError("Instance and class checks can only be used with"
TypeError: Instance and class checks can only be used with @runtime_checkable protocols
Подробнее здесь: [url]https://stackoverflow.com/questions/79903931/typing-protocol-and-typing-runtime-checkable[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия