csp icon indicating copy to clipboard operation
csp copied to clipboard

typing.Any not handled properly as an output from a csp.node

Open NeejWeej opened this issue 10 months ago • 3 comments

Describe the bug typing.Any is not handled properly. This occurs both if CSP_PYDANTIC is set as an environment behavior or not

To Reproduce

import csp
import typing

from datetime import datetime, timedelta

import sys

print(csp.__version__); print(sys.version); print(sys.platform)

@csp.node
def val(x: csp.ts[object]) -> csp.ts[typing.Any]:
    return x

@csp.node
def val2(x: csp.ts[typing.Any]) -> csp.ts[object]:
    return x

csp.run(val2, csp.const(1), starttime=datetime.utcnow(), endtime=timedelta(seconds=3))
csp.run(val, csp.const(1), starttime=datetime.utcnow(), endtime=timedelta(seconds=3))  # fails here

Expected behavior Both of these should pass

Error Message

TypeError                                 Traceback (most recent call last)
Cell In[22], [line 20](vscode-notebook-cell:?execution_count=22&line=20)
     [17](vscode-notebook-cell:?execution_count=22&line=17)     return x
     [19](vscode-notebook-cell:?execution_count=22&line=19) csp.run(val2, csp.const(1), starttime=datetime.utcnow(), endtime=timedelta(seconds=3))
---> [20](vscode-notebook-cell:?execution_count=22&line=20) csp.run(val, csp.const(1), starttime=datetime.utcnow(), endtime=timedelta(seconds=3))  # fails here

File csp/csp/impl/wiring/runtime.py:167, in run(g, starttime, endtime, queue_wait_time, realtime, output_numpy, *args, **kwargs)
    [157](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.netcsp/csp/impl/wiring/runtime.py:157) def run(
    [158](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.netcsp/csp/impl/wiring/runtime.py:158)     g,
    [159](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.net/csp/csp/impl/wiring/runtime.py:159)     *args,
   (...)
    [165](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.net/csp/csp/impl/wiring/runtime.py:165)     **kwargs,
    [166](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.net/csp/csp/impl/wiring/runtime.py:166) ):
--> [167](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.net/csp/csp/impl/wiring/runtime.py:167)     with ExceptionContext():
    [168](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.net/csp/csp/impl/wiring/runtime.py:168)         starttime, endtime = _normalize_run_times(starttime, endtime, realtime)
    [170](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.net/csp/csp/impl/wiring/runtime.py:170)         engine_settings = {"queue_wait_time": queue_wait_time, "realtime": realtime, "output_numpy": output_numpy}

File csp/csp/impl/error_handling.py:40, in ExceptionContext.__exit__(self, exc_type, exc_val, exc_tb)
     [37](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.net/csp/csp/impl/error_handling.py:37) if exc_type is CspParseError and exc_val.filename is not None and exc_val.lineno is not None:
     [38](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.net/csp/csp/impl/error_handling.py:38)     new_tb = _cspimpl.create_traceback(new_tb, exc_val.frame, exc_val.lineno, exc_val.lineno)
---> [40](https://vscode-remote+ssh-002dremote-002bdev06.vscode-resource.vscode-cdn.net/csp/csp/impl/error_handling.py:40) raise exc_val.with_traceback(new_tb)

Cell In[22], [line 13](vscode-notebook-cell:?execution_count=22&line=13)
     [11](vscode-notebook-cell:?execution_count=22&line=11) @csp.node
     [12](vscode-notebook-cell:?execution_count=22&line=12) def val(x: csp.ts[object]) -> csp.ts[typing.Any]:
---> [13](vscode-notebook-cell:?execution_count=22&line=13)     return x

TypeError: PyOutputProxy.h:outputTick:56:TypeError: "val" node expected output type on output #0 to be of type "Any" got type "int"

Runtime Environment 0.0.7 3.12.7 | packaged by conda-forge | (main, Oct 4 2024, 16:05:46) [GCC 13.3.0] linux

NeejWeej avatar Jan 31 '25 21:01 NeejWeej

Outputs from csp nodes are defined on the c++ level, which means it is much more difficult to fix

NeejWeej avatar Mar 03 '25 23:03 NeejWeej

Outputs from csp nodes are defined on the c++ level, which means it is much more difficult to fix

If we treat Any as if it were object this would work, but im not sure if the semantics are exact ( or if it matters )

robambalu avatar Mar 04 '25 13:03 robambalu

Outputs from csp nodes are defined on the c++ level, which means it is much more difficult to fix

If we treat Any as if it were object this would work, but im not sure if the semantics are exact ( or if it matters )

So that kinda works, but according to the Python docs, Any should also be able to be assigned to anything.

So like this (note we can assign s = a below)

Image

source: https://docs.python.org/3/library/typing.html#the-any-type

ts[object] doesn't quite fit since I'd need to cast it back to the correct type. Like, this example

import csp

from datetime import datetime, timedelta

@csp.node
def func() -> csp.ts[object]:
    with csp.alarms():
        a_alarm = csp.alarm(bool)
    with csp.start():
        csp.schedule_alarm(a_alarm, datetime.now() + timedelta(seconds=1), 8)
    
    if csp.ticked(a_alarm):
        return a_alarm


@csp.node
def my_int(x: csp.ts[int]):
    if csp.ticked(x):
        print(x)

@csp.graph
def g():
    a = func()
    my_int(a)

csp.run(
    g,
    starttime=datetime.utcnow(),
    endtime=timedelta(1),
)

fails with this error without pydantic type checking

csp.impl.types.instantiation_type_resolver.TSArgTypeMismatchError: In function my_int: Expected ts[int] for argument 'x', got ts[object]

and with pydantic type checking

1 validation error for my_int
x
  Value error, cannot validate ts[object] as ts[int]: <class 'object'> is not a subclass of <class 'int'>. [type=value_error, input_value=Edge( tstype=csp.impl.typ...t_idx=0, basket_idx=-1 ), input_type=Edge]

NeejWeej avatar Mar 04 '25 17:03 NeejWeej