type-hinting TensorVariable with the OptionalApplyType correctly #1386
-
from __future__ import annotations
from typing import Any
import sys
if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias
from pytensor.graph.basic import Apply
from pytensor.tensor.type import TensorType
from pytensor.tensor.variable import TensorVariable
from pyhs3.typing_compat import NotRequired, TypeAlias
TensorVar: TypeAlias = TensorVariable[TensorType, Apply[Any]]consider the above (which OpType = TypeVar("OpType", bound="Op")
class Apply(Node, Generic[OpType]): ...which at first glance looks fine, since OptionalApplyType = TypeVar("OptionalApplyType", None, "Apply", covariant=True)so it's not clear if I'm treating this wrong or if there's a bug in the code for the typehints here. The main reason I'm defining this import pytensor.tensor as pt
distributions: dict[str, TensorVar] = {}
distributions["sqrt"] = pt.sqrt(pt.scalar('x'))
distributions["sqrt"].eval({'x': 4})as an example. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
|
Can't you do |
Beta Was this translation helpful? Give feedback.
-
|
Re:
I don't know. The point is that a Variable can have an owner (an Apply[Op]) or no owner (None). Not sure we are doing something wrong? |
Beta Was this translation helpful? Give feedback.
Re:
I don't know. The point is that a Variable can have an owner (an Apply[Op]) or no owner (None). Not sure we are doing something wrong?