Skip to content

Commit

Permalink
Bugfix: new annotation type broke lists (#26)
Browse files Browse the repository at this point in the history
Annotations which changed from `def myfunc(a: List[int]):` to `def myfunc(a: list[int]):`, caused an error since `isclass(list)` returns true, but `issubclass(list, OtherClass)` raised an error saying the first arg should be a class.
  • Loading branch information
idling-mind authored Sep 23, 2024
1 parent 07945f4 commit e34fe72
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: flowfunc
Title: Dash wrapper for flume node editor
Version: 0.1.0
Version: 0.1.1
Description: Dash wrapper for flume node editor
Depends: R (>= 3.0.2)
Imports:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "Flowfunc"
uuid = "1b08a953-4be3-4667-9a23-112f0d177a99"
authors = ["Najeem Muhammed <najeem@gmail.com>"]
version = "0.1.0"
version = "0.1.1"

[deps]
Dash = "1b08a953-4be3-4667-9a23-3db579824955"
Expand Down
4 changes: 2 additions & 2 deletions R/internal.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
.flowfunc_js_metadata <- function() {
deps_metadata <- list(`flowfunc` = structure(list(name = "flowfunc",
version = "0.1.0", src = list(href = NULL,
version = "0.1.1", src = list(href = NULL,
file = "deps"), meta = NULL,
script = 'flowfunc.min.js',
stylesheet = NULL, head = NULL, attachment = NULL, package = "flowfunc",
all_files = FALSE), class = "html_dependency"),
`flowfunc` = structure(list(name = "flowfunc",
version = "0.1.0", src = list(href = NULL,
version = "0.1.1", src = list(href = NULL,
file = "deps"), meta = NULL,
script = 'flowfunc.min.js.map',
stylesheet = NULL, head = NULL, attachment = NULL, package = "flowfunc",
Expand Down
2 changes: 1 addition & 1 deletion deps/flowfunc.min.js

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions examples/assets/funcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ window.dash_clientside = Object.assign({}, window.dash_clientside, {
flowfunc: {
dynamic_ports: function (ports, inputData, connections, context) {
// Example from flume.dev
console.log(connections, context);
const template = (inputData && inputData.template && inputData.template.in_string) || "";
console.log(inputData);
const template =
(inputData &&
inputData.template &&
Object.values(inputData.template)[0]) ||
'';
const re = /\{(.*?)\}/g;
let res, ids = []
while ((res = re.exec(template)) !== null) {
Expand Down
8 changes: 4 additions & 4 deletions flowfunc/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from pydantic import BaseModel

from .models import Color, ConfigModel, ControlType, Node, Port, Control
from .utils import issubclass_safe


def arg_or_kwarg(par: inspect.Parameter):
Expand Down Expand Up @@ -183,7 +184,7 @@ def control_from_field(
Control: A flowfunc Control object corresponding to the type annotation
"""
control_types = [x.name for x in ControlType]
if inspect.isclass(arg_type) and issubclass(arg_type, Enum):
if inspect.isclass(arg_type) and issubclass_safe(arg_type, Enum):
# Enum
clabel = f"{arg_name} (enum)"
options = [{"label": x.name, "value": x.value} for x in arg_type]
Expand All @@ -193,12 +194,11 @@ def control_from_field(
if (
get_origin(arg_type) is list
and inspect.isclass(get_args(arg_type)[0])
and issubclass(get_args(arg_type)[0], Enum)
and issubclass_safe(get_args(arg_type)[0], Enum)
):
# List of enums
clabel = f"{arg_name} (list)"
options = [{"label": x.name, "value": x.value} for x in get_args(arg_type)[0]]
print("listenum", options)
return Control(
type=ControlType.multiselect, name=arg_name, label=clabel, options=options
)
Expand Down Expand Up @@ -245,7 +245,7 @@ def ports_from_nodes(nodes: List[Node]) -> List[Port]:
port = deepcopy(port_)
ports.append(port)
# Copy is made so that the port instance in Node object is unaffected
if inspect.isclass(port.py_type) and issubclass(port.py_type, BaseModel):
if inspect.isclass(port.py_type) and issubclass_safe(port.py_type, BaseModel):
# Use a pydantic model
port.controls = []
for arg_name, field in port.py_type.model_fields.items():
Expand Down
2 changes: 1 addition & 1 deletion flowfunc/flowfunc.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion flowfunc/package-info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flowfunc",
"version": "0.1.0",
"version": "0.1.1",
"description": "Dash wrapper for flume node editor",
"main": "build/index.js",
"scripts": {
Expand Down
13 changes: 12 additions & 1 deletion flowfunc/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import logging

logger = logging.getLogger(__name__)
logger = logging.getLogger(__name__)


def issubclass_safe(cls, classinfo):
"""Check if a class is a subclass of another class.
Some classes like list, dict, etc. returns true when checked inspect.isclass
however, they raise TypeError when checked with issubclass.
"""
try:
return issubclass(cls, classinfo)
except TypeError:
return False
2 changes: 1 addition & 1 deletion inst/deps/flowfunc.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flowfunc",
"version": "0.1.0",
"version": "0.1.1",
"description": "Dash wrapper for flume node editor",
"main": "build/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/Flowfunc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Flowfunc
using Dash

const resources_path = realpath(joinpath( @__DIR__, "..", "deps"))
const version = "0.1.0"
const version = "0.1.1"

include("jl/''_flowfunc.jl")

Expand Down

0 comments on commit e34fe72

Please sign in to comment.