Ja, funktioniert gut:
Code: Alles auswählen
import hashlib
from typing import Protocol
class Hash(Protocol):
def hexdigest(self) -> str: ...
def foo() -> Hash:
return hashlib.sha256(b"")
foo().hexdigest()
foo().digest()
Ich habe mit voller Absicht nur eine Methode implementiert. Wenn man den Code ausführt, gibt es keinen Fehler. Wenn man das mit mypy überprüft, meckert mypy:
[deadeye@nexus ~]$ mypy hh.py
hh.py:12: error: "Hash" has no attribute "digest"; maybe "hexdigest"? [attr-defined]
Found 1 error in 1 file (checked 1 source file)
Jetzt nochmal mit Absicht den falschen Typen angegeben:
Code: Alles auswählen
import hashlib
from typing import Protocol
class Hash(Protocol):
def hexdigest(self) -> bytes: ...
def foo() -> Hash:
return hashlib.sha256(b"")
foo().hexdigest()
hh.py:8: error: Incompatible return value type (got "HASH", expected "Hash") [return-value]
hh.py:8: note: Following member(s) of "HASH" have conflicts:
hh.py:8: note: Expected:
hh.py:8: note: def hexdigest(self) -> bytes
hh.py:8: note: Got:
hh.py:8: note: def hexdigest(self) -> str
Found 1 error in 1 file (checked 1 source file)
Mir gefällt das
Aber:
[deadeye@nexus ~]$ git/micropython/ports/unix/build-standard/micropython hh.py
Traceback (most recent call last):
File "hh.py", line 2, in <module>
ImportError: no module named 'typing'
[deadeye@nexus ~]$
Ok, Lösung gefunden und das fehlende Typing Modul installiert:
Code: Alles auswählen
git/micropython/ports/unix/build-standard/micropython -m mip install github:josverl/micropython-stubs/mip/typing.mpy
Und dann stellt man fest, dass es bei Micropython hexdigest nicht gibt. Diese Methode ist nicht implementiert worden.
[deadeye@nexus ~]$ git/micropython/ports/unix/build-standard/micropython hh.py
Traceback (most recent call last):
File "hh.py", line 11, in <module>
AttributeError: 'sha256' object has no attribute 'hexdigest'
Gibt es dafür auch eine Lösung?
Hier noch der Link zu den Micropython-Stubs:
https://micropython-stubs.readthedocs.i ... g_mpy.html
Ist aber schon ziemlich verrückt, den Code auch noch auf den Microcontroller zu packen.
sourceserver.info - sourceserver.info/wiki/ - ausgestorbener Support für HL2-Server