New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can I add typehints? #725
Comments
Example. mypy is unhappy about elif isinstance(obj, ShapeCastable):
new_obj = obj.as_shape()
If class ShapeCastable(ABC):
@abstractmethod
def as_shape() -> Shape:
... I think static analysis would pass (and |
Another case where static analysis gets mad: The n = len(value) Then later on does self.value = Value.cast(value) The static analyzer gets mad because you can't take the |
One particular issue is that currently, there is no good way to add typing to Amaranth values, modules, interfaces directly. So far my position was that type hints would be a nice to have, but right now it's not clear that benefits outweigh the drawbacks. |
I'm not sure I understand? I'm just talking about typehinting the core code, like this: ValueCastableT = Union["Value", int, Enum, "ValueCastable"]
class Value(metaclass=ABCMeta):
src_loc: Optional[Tuple[str, int]] = None
@staticmethod
def cast(obj: ValueCastableT) -> Union["Value", "Const"]:
"""Converts ``obj`` to an Amaranth value. This way, if you're using an IDE that supports static analysis (e.g. vscode), it will immediately complain when you try to write code like Another benefit is static analysis of the core code itself. For example, in if name is not None:
new_name = str(name)
elif name_suffix is not None:
new_name = other.name + str(name_suffix) But |
The thing is that people will want to do something like (Otherwise you can't make e.g. FIFOInterface a type.) |
Oh, I wasn't talking about going all the way like that. class FIFOInterface:
def __init__(self, *, width: int, depth: int, fwft: bool):
... So that if I try this: w = Const(3)
d = Const(5)
x = SyncFIFO(w, d) The IDE will immediately complain, rather than having to wait until runtime to get an exception. |
I currently use type stubs + Pyright in my (unfortunately currently private) project. I can share them if you're interested. Unfortunately, I had to use recursive type definitions, which are unsupported by mypy, so that things like record layouts can be typechecked. Off-topic remark: I'm a huge fan of #693, because Amaranth records suck badly. |
Another +1 - I actually considered adding types to Glasgow, then realized it makes no sense without Amaranth first. I think #725 (comment) is the way to go - start with simple types with liberal use of Any and no generics. That would already make mypy happy in projects that import amaranth, more fancy stuff can be added later. |
What are your thoughts about adding PEP 484 / PEP 526 type hints to the amaranth.* code? I know that many functions are documented in docstrings, but this lets static analyzers have a go at validating user-written code. If you're game, I'd like to try.
The text was updated successfully, but these errors were encountered: