Код: Выделить всё
from typing import Type, TypeAlias
from ctypes import POINTER, pointer, c_char, c_char_p
#normal python string
p_useable_p_str = str
#the result of a my_str.decode("utf-8")
c_useable_p_str = bytes
#used for string literals returned by the C, which should not be freed
p_useable_c_str = c_char_p
#used for allocated strings returned by the C, which need to be freed later
c_useable_c_str = POINTER(c_char) #the problematic line
def example(hello: c_useable_c_str): # source of the MyPy error
pass
Однако, анализируя вышеизложенное с помощью MyPy, я получаю:
Код: Выделить всё
playground.py:10: error: Variable "playground.c_useable_c_str" is not valid as a type
playground.py:10: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)
Единственный синтаксис, который, кажется, делает MyPy счастье – это
Код: Выделить всё
c_useable_c_str = pointer[c_char]
Код: Выделить всё
Traceback (most recent call last):
File "/home/fulguritude/ProfessionalWork/LEDR/Orchestra-AvesTerra/Python_binding/playground2.py", line 92, in
c_useable_c_str = pointer[c_char]
TypeError: 'builtin_function_or_method' object is not subscriptable
TLDR: как правильно вводить подсказку и псевдоним, «указатель на X» с MyPy и CTypes?
Подробнее здесь: https://stackoverflow.com/questions/710 ... py-accepts