Forum Discussion
call function in user data functions
- 9 months ago
use this code (tested & working):
import datetime import fabric.functions as fn import logging udf = fn.UserDataFunctions() async def get_environment_local(varLib: fn.FabricVariablesClient) -> str: variables = varLib.getVariables() # sync call return variables["omgeving"] async def get_result_local(varLib: fn.FabricVariablesClient) -> str: return await get_environment_local(varLib) @udf.connection(argName="varLib", alias="varlib") @udf.function() async def hello_fabric(name: str, varLib: fn.FabricVariablesClient) -> str: logging.info("Running hello_fabric") omgeving = await get_result_local(varLib) return ( f"Welcome to Fabric Functions, {name}. " f"Omgeving: {omgeving}. " f"Time: {datetime.datetime.now()}" )
When a function has @udf.function(), it becomes a remote Fabric function, not a normal Python function. Calling it directly (await get_environment(varLib)) bypasses Fabric's runtime, so the injected connection becomes None, and Fabric later tries to access None.headers, causing the NoneType error. Make helper functions local (not decorated) so they behave like real Python functions.
Therefore I modified the code to remove the @udf.function() decorators from the helper functions and turn them into normal local Python functions, so they no longer get wrapped by the Fabric runtime and can be safely called from inside the main UDF with a valid varLib connection.
Hi bruinsmm
(Edited my reply to fix the code, due the error mentioned here)
To get the correct value, modify get_result so it awaits get_environment. In Python with async/await, this means:
@udf.connection(argName="varLib", alias="varlib")
@udf.function()
async def get_environment(varLib: fn.FabricVariablesClient) -> str:
variables = varLib.getVariables()
omgeving = variables["omgeving"]
return omgeving
@udf.function()
async def get_result() -> str:
result = await get_environment(varLib) # <--- FIX
return result
Hope that helps
Onur
πIf this post helped you, feel free to give it some Kudos! π
β And if it answered your question, please mark it as the accepted solution.
Hi OnurOz
I've try your solution, but it don't work, i got the error message:
AttributeError: 'NoneType' object has no attribute 'headers'
I think the problem is in the call function without VarLib parameter: get_environment()
- OnurOz9 months agoResolver III
sorry my bad, I've forgotten to add varlib to the call get_environment(), following code should do the work:
@udf.function() @udf.connection(argName="varLib", alias="varlib") async def get_environment(varLib: fn.FabricVariablesClient) -> str: variables = varLib.getVariables() return variables["omgeving"] @udf.function() @udf.connection(argName="varLib", alias="varlib") async def get_result(varLib: fn.FabricVariablesClient) -> str: result = await get_environment(varLib) # I've forgotten this return result- bruinsmm9 months agoFrequent Visitor
I have try the code. It don't work :βΉοΈ
got the same error message: "error_message": "'NoneType' object has no attribute 'headers'"
@udf.connection(argName="varLib", alias="varlib")@udf.function()async def get_environment(varLib: fn.FabricVariablesClient) -> str:variables = varLib.getVariables()return variables["omgeving"]@udf.connection(argName="varLib", alias="varlib")@udf.function()async def get_result(varLib: fn.FabricVariablesClient) -> str:result = await get_environment(varLib) # I've forgotten thisreturn result- OnurOz9 months agoResolver III
use this code (tested & working):
import datetime import fabric.functions as fn import logging udf = fn.UserDataFunctions() async def get_environment_local(varLib: fn.FabricVariablesClient) -> str: variables = varLib.getVariables() # sync call return variables["omgeving"] async def get_result_local(varLib: fn.FabricVariablesClient) -> str: return await get_environment_local(varLib) @udf.connection(argName="varLib", alias="varlib") @udf.function() async def hello_fabric(name: str, varLib: fn.FabricVariablesClient) -> str: logging.info("Running hello_fabric") omgeving = await get_result_local(varLib) return ( f"Welcome to Fabric Functions, {name}. " f"Omgeving: {omgeving}. " f"Time: {datetime.datetime.now()}" )
When a function has @udf.function(), it becomes a remote Fabric function, not a normal Python function. Calling it directly (await get_environment(varLib)) bypasses Fabric's runtime, so the injected connection becomes None, and Fabric later tries to access None.headers, causing the NoneType error. Make helper functions local (not decorated) so they behave like real Python functions.
Therefore I modified the code to remove the @udf.function() decorators from the helper functions and turn them into normal local Python functions, so they no longer get wrapped by the Fabric runtime and can be safely called from inside the main UDF with a valid varLib connection.