Open
Description
Pyfunctional seems to work fine in root files, but fails when used in modules.
There is a very difficult to debug error that gets thrown when using pyfunctional in the modules folder. It will refer to incorrect lines in more complex examples.
test.py
from functional import seq
from temp.show_error import demonstrate_pyfunctional_issue
# This works fine at the root level
number_data = [1, 2, 3, 4, 5]
result = seq(number_data).map(lambda x: x * 2).filter(lambda x: x > 5).list()
log.debug(f"Root level pyfunctional works: {result}")
# Test with camera data
camera_data = ["sensor.camera1", "sensor.camera2", "sensor.camera3"]
camera_name = "USB Video" # Same camera name as in show_error.py
# This works at the root level
camera_is_active_state_triggers = seq(camera_data).map(lambda camera_id: f"{camera_id} == '{camera_name}'").list()
log.debug(f"Root level camera triggers: {camera_is_active_state_triggers}")
# This will cause an error when the module tries to use pyfunctional
demonstrate_pyfunctional_issue()
modules/temp/show_error.py
from typing import List
from functional import seq
def demonstrate_pyfunctional_issue():
"""
Demonstrate the issue with pyfunctional in modules.
This function attempts to use pyfunctional in a module, which causes an error.
The same code works fine at the root level (see test.py).
"""
log.debug("Attempting to use pyfunctional in a module...")
# Sample data
camera_data = ["sensor.camera1", "sensor.camera2", "sensor.camera3"]
camera_name = "USB Video"
try:
# Simple example with numbers
number_data = [1, 2, 3, 4, 5]
result = seq(number_data).map(lambda x: x * 2).filter(lambda x: x > 5).list()
log.debug(f"Results: {result}")
# This will cause an error when used in a module
camera_is_active_state_triggers: List[str] = (
seq(camera_data)
.map(lambda camera_id: f"{camera_id} == '{camera_name}'")
.list()
)
log.debug(f"Module level pyfunctional works: {camera_is_active_state_triggers}")
except Exception as e:
log.debug(f"Error using pyfunctional in module: {e}")
# Show that standard Python works fine
camera_is_active_state_triggers = [
f"{camera_id} == '{camera_name}'" for camera_id in camera_data
]
log.debug(f"Standard Python list comprehension works: {camera_is_active_state_triggers}")
requirements.txt
pyfunctional==1.5.0
2025-05-26 20:14:10.866 INFO (MainThread) [custom_components.pyscript] Unloaded /config/pyscript/modules/temp/show_error.py
2025-05-26 20:14:10.872 INFO (MainThread) [custom_components.pyscript.global_ctx] Loaded /config/pyscript/modules/temp/show_error.py
2025-05-26 20:14:10.872 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling seq([1, 2, 3, 4, 5], {})
2025-05-26 20:14:10.873 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling map(<function __lambda_defn_temp__ at 0xfffed9a80720>, {})
2025-05-26 20:14:10.873 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling filter(<function __lambda_defn_temp__ at 0xfffed9a82020>, {})
2025-05-26 20:14:10.873 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling list(, {})
2025-05-26 20:14:10.873 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling debug("Root level pyfunctional works: [6, 8, 10]", {})
2025-05-26 20:14:10.873 DEBUG (MainThread) [custom_components.pyscript.file.test] Root level pyfunctional works: [6, 8, 10]
2025-05-26 20:14:10.873 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling seq(['sensor.camera1', 'sensor.camera2', 'sensor.camera3'], {})
2025-05-26 20:14:10.874 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling map(<function __lambda_defn_temp__ at 0xfffed9a80720>, {})
2025-05-26 20:14:10.874 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling list(, {})
2025-05-26 20:14:10.874 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling debug("Root level camera triggers: ["sensor.camera1 == 'USB Video'", "sensor.camera2 == 'USB Video'", "sensor.camera3 == 'USB Video'"]", {})
2025-05-26 20:14:10.874 DEBUG (MainThread) [custom_components.pyscript.file.test] Root level camera triggers: ["sensor.camera1 == 'USB Video'", "sensor.camera2 == 'USB Video'", "sensor.camera3 == 'USB Video'"]
2025-05-26 20:14:10.874 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling demonstrate_pyfunctional_issue(, {})
2025-05-26 20:14:10.874 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling debug("Attempting to use pyfunctional in a module...", {})
2025-05-26 20:14:10.874 DEBUG (MainThread) [custom_components.pyscript.file.test] Attempting to use pyfunctional in a module...
2025-05-26 20:14:10.874 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling seq([1, 2, 3, 4, 5], {})
2025-05-26 20:14:10.875 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling map(<function __lambda_defn_temp__ at 0xfffed9a80720>, {})
2025-05-26 20:14:10.875 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling filter(<function __lambda_defn_temp__ at 0xfffed9a82020>, {})
2025-05-26 20:14:10.875 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling list(, {})
2025-05-26 20:14:10.875 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling debug("Results: [6, 8, 10]", {})
2025-05-26 20:14:10.875 DEBUG (MainThread) [custom_components.pyscript.file.test] Results: [6, 8, 10]
2025-05-26 20:14:10.875 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling seq(['sensor.camera1', 'sensor.camera2', 'sensor.camera3'], {})
2025-05-26 20:14:10.875 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling map(<function __lambda_defn_temp__ at 0xfffed9a80720>, {})
2025-05-26 20:14:10.875 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling list(, {})
2025-05-26 20:14:10.876 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling debug("Error using pyfunctional in module: name 'camera_name' is not defined", {})
2025-05-26 20:14:10.876 DEBUG (MainThread) [custom_components.pyscript.file.test] Error using pyfunctional in module: name 'camera_name' is not defined
2025-05-26 20:14:10.876 DEBUG (MainThread) [custom_components.pyscript.eval] file.test: calling debug("Standard Python list comprehension works: ["sensor.camera1 == 'USB Video'", "sensor.camera2 == 'USB Video'", "sensor.camera3 == 'USB Video'"]", {})
2025-05-26 20:14:10.876 DEBUG (MainThread) [custom_components.pyscript.file.test] Standard Python list comprehension works: ["sensor.camera1 == 'USB Video'", "sensor.camera2 == 'USB Video'", "sensor.camera3 == 'USB Video'"]
Metadata
Metadata
Assignees
Labels
No labels