Skip to content
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

version 0.0.6 improper type handling on Optional[List] #425

Open
NeejWeej opened this issue Jan 7, 2025 · 1 comment
Open

version 0.0.6 improper type handling on Optional[List] #425

NeejWeej opened this issue Jan 7, 2025 · 1 comment
Labels
type: bug Concrete, reproducible bugs

Comments

@NeejWeej
Copy link
Collaborator

NeejWeej commented Jan 7, 2025

Describe the bug
Having an Optional[List[]] in acsp.node or graph causes an argument mismatch if a non-null value is supplied.

To Reproduce

import csp

from csp import ts
from datetime import datetime, timedelta
from typing import Optional, List

@csp.node
def my_node_fails(
    x: ts[int],
    my_data: Optional[List[int]] = None
) -> ts[int]:
    if csp.ticked(x):
        return my_data[0] if my_data else x

@csp.node
def my_node_works(
    x: ts[int],
    my_data: Optional[list] = None
)-> ts[int]:
    if csp.ticked(x):
        return my_data[0] if my_data else x

res = csp.run(
    my_node_works,
    x=csp.const(10),
    my_data=[],
    starttime=datetime.utcnow(),
    endtime=timedelta(1)
)
assert res[0][0][1] == 10

res2 = csp.run(
    my_node_fails,
    x=csp.const(10),
    my_data=None,  # works
    starttime=datetime.utcnow(),
    endtime=timedelta(1)
)
assert res2[0][0][1] == 10

# FAILS
res3 = csp.run(
    my_node_fails,
    x=csp.const(10),
    my_data=[],
    starttime=datetime.utcnow(),
    endtime=timedelta(1)
)
assert res3[0][0][1] == 10

Expected behavior
No arg mismatch error

Error Message

ArgTypeMismatchError                      Traceback (most recent call last)
Cell In[17], [line 41](vscode-notebook-cell:?execution_count=17&line=41)
     [32](vscode-notebook-cell:?execution_count=17&line=32) res2 = csp.run(
     [33](vscode-notebook-cell:?execution_count=17&line=33)     my_node_fails,
     [34](vscode-notebook-cell:?execution_count=17&line=34)     x=csp.const(10),
   (...)
     [37](vscode-notebook-cell:?execution_count=17&line=37)     endtime=timedelta(1)
     [38](vscode-notebook-cell:?execution_count=17&line=38) )
     [39](vscode-notebook-cell:?execution_count=17&line=39) assert res2[0][0][1] == 10
---> [41](vscode-notebook-cell:?execution_count=17&line=41) res3 = csp.run(
     [42](vscode-notebook-cell:?execution_count=17&line=42)     my_node_fails,
     [43](vscode-notebook-cell:?execution_count=17&line=43)     x=csp.const(10),
     [44](vscode-notebook-cell:?execution_count=17&line=44)     my_data=[],
     [45](vscode-notebook-cell:?execution_count=17&line=45)     starttime=datetime.utcnow(),
     [46](vscode-notebook-cell:?execution_count=17&line=46)     endtime=timedelta(1)
     [47](vscode-notebook-cell:?execution_count=17&line=47) )
     [48](vscode-notebook-cell:?execution_count=17&line=48) assert res3[0][0][1] == 10


ArgTypeMismatchError: In function my_node_fails: Expected typing.Optional[typing.List[int]] for argument 'my_data', got [] (list)

Runtime Environment
Output from
import sys, csp; print(csp.__version__); print(sys.version); print(sys.platform).

0.0.6
3.11.10 | packaged by conda-forge | (main, Oct 16 2024, 01:27:36) [GCC 13.3.0]
linux
@NeejWeej NeejWeej added the type: bug Concrete, reproducible bugs label Jan 7, 2025
@NeejWeej
Copy link
Collaborator Author

NeejWeej commented Jan 9, 2025

Seems there is also issues with Callable

import csp

from csp import ts
from datetime import datetime, timedelta
from typing import Callable, Optional, List

csp.set_print_full_exception_stack(True)

@csp.node
def my_node_callable(
    x: ts[int],
    my_data: Callable[[int], int] = None
)-> ts[int]:
    if csp.ticked(x):
        if my_data:
            return my_data(x) if callable(my_data) else 12

# This should fail but it does not
res = csp.run(
    my_node_callable,
    x=csp.const(10),
    my_data=None,
    starttime=datetime.utcnow(),
    endtime=timedelta(1)
)

@csp.node
def my_node_callable2(
    x: ts[int],
    my_data: Callable = None
)-> ts[int]:
    if csp.ticked(x):
        if my_data:
            return my_data(x) if callable(my_data) else 12

# This should fail but it does not
res = csp.run(
    my_node_callable2,
    x=csp.const(10),
    my_data=None,
    starttime=datetime.utcnow(),
    endtime=timedelta(1)
)
# This should not fail but it does
res = csp.run(
    my_node_callable2,
    x=csp.const(10),
    my_data=lambda x: 2*x,
    starttime=datetime.utcnow(),
    endtime=timedelta(1)
)

NeejWeej added a commit to NeejWeej/csp that referenced this issue Jan 9, 2025
NeejWeej added a commit to NeejWeej/csp that referenced this issue Jan 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Concrete, reproducible bugs
Projects
None yet
Development

No branches or pull requests

1 participant