a-recknagel / stenotype

Support for shorthand type annotations.
MIT License
3 stars 2 forks source link

[ENHANCEMENT]: Allow for automatic declarations #29

Open maxfischer2781 opened 4 years ago

maxfischer2781 commented 4 years ago

Is your feature request related to a problem? Please describe. Several typing features need separate declaration and type hints. This includes the planned TypeVar, Protocol (as Callable replacement) but also the potential NewType and type aliases.

The current unparse and IR has no concept for required declarations. Thus, stenotype cannot declare them automatically.

Describe the solution you'd like Introduce declared types to the IR, which represent both the type hint and declaration. For example, a TypeVar `T would be the hint T plus the declaration T = TypeVar("T").

The unparse (and perhaps normalize) function(s) should be aware of declared types. String representations should be available both with and without declarations.

# steno
foo: '(a: A, b: B) -> R'

# typing normalized, unparsed
class a_A_b_B_to_R(Protocol):
    def __call__(self, a: A, b: B) -> R: ...

foo: a_A_b_B_to_R

Describe alternatives you've considered This is most critical for call signatures. The Protocol has more capabilities than Callable. Callable is only suitable for declaring positional and return types. Variadic or named arguments require Protocol.

Generics could require manual declarations of TypeVars.

There hasn't been any proposal for adding structural types, new types or type aliases to stenotypes. Any requirement for them are pure speculation at the moment.