Skip to content
This repository was archived by the owner on Nov 11, 2021. It is now read-only.
This repository was archived by the owner on Nov 11, 2021. It is now read-only.

Allow type variables in Nodes #58

@tehrengruber

Description

@tehrengruber

It is sometimes useful to restrict types of fields of fields of a node.

Objectives

  • Reduce the amount of verification boilerplate code inside the translators.
  • Reduce the amount of state to be passed to child nodes inside the translators
  • Elimination of redundant nodes

Pydantic supports this out of the box (see pydantic/pydantic#556 and pydantic/pydantic#595).

Open questions:

  1. How to check for a field type to be a subtype of generic type? This will be a common question if we allow this, but Python does not allow subclass checks with generic, i.e. the following does not work:
from typing import Generic, TypeVar, List
# raises: TypeError: Subscripted generics cannot be used with class and instance checks
issubclass(List[int], List[T])
  1. Consequences of changing the Node base class to GenericModel
  2. Is this compatible with mypy?

Example 1

T = TypeVar('T')
class Constant(GT4PyAstNode, Generic[T]):
    value: T

class ExampleNode:
    some_int_constant: Constant[Int]

Example 2

T = TypeVar('T')

class Stencil(GTScriptAstNode, Generic[T]):
    iteration_spec: List[Union[IterationOrder, LocationSpecification, Interval]]
    body: List[T]

class Computation(GTScriptAstNode):
    name: str
    arguments: List[Argument]
    stencils: List[Union[Stencil[Stencil[Statement]], Stencil[Statement]]]

Metadata

Metadata

Assignees

Labels

eveRelated to eve infrastructure

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions