-
|
For MicroPython typing with pyright I would like the return type of the built-in method When I try adding that to
Is there a better way to express this properly ? # __builtins__.pyi
from typing import Tuple, TypeVar, Final
Const_T = TypeVar("Const_T", int, float, str, bytes, Tuple) # constant
def const(expr: Const_T) -> Final[Const_T]:
""" Used to declare that the expression is a constant so that the compiler can optimise it."""
...Code sample in pyright playground |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
It sounds like what you want is that for any assignment I see a few possible ways forward:
|
Beta Was this translation helpful? Give feedback.
I don't think it makes sense to special-case this in the type system.
@Josveri, if you want to use a static type checker with micropython code, my recommendation is to use an explicit
Finalwith the variable declaration.This informs a static type checker that the symbol cannot be reassigned, and the
constfunction tells the runtime to enforce this. If you don't want to use a static type checker with your code, you can omit theFinalqualifier.