{"record":{"id":"c782be17c81644dd","repo":"cube-js/cube","slug":"unable-to-register-variable-name-s-is-already","errorCode":null,"errorMessage":"unable to register variable: name '%s' is already in use for function","messagePattern":"unable to register variable: name '(.+?)' is already in use for function","errorType":"exception","errorClass":"TemplateException","httpStatus":null,"severity":"error","filePath":"packages/cubejs-backend-native/python/cube/src/__init__.py","lineNumber":187,"sourceCode":"class TemplateContext:\n    functions: dict[str, Callable]\n    variables: dict[str, Any]\n    filters: dict[str, Callable]\n\n    def __init__(self):\n        self.functions = {}\n        self.variables = {}\n        self.filters = {}\n\n    def add_function(self, name, func):\n        if not callable(func):\n            raise TemplateException(\"function registration must be used with functions, actual: '%s'\" % type(func).__name__)\n\n        self.functions[name] = func\n\n    def add_variable(self, name, val):\n        if name in self.functions:\n            raise TemplateException(\"unable to register variable: name '%s' is already in use for function\" % name)\n\n        self.variables[name] = val\n\n    def add_filter(self, name, func):\n        if not callable(func):\n            raise TemplateException(\"function registration must be used with functions, actual: '%s'\" % type(func).__name__)\n\n        self.filters[name] = func\n\n    def function(self, func):\n        if isinstance(func, str):\n            return TemplateFunctionRef(self, func)\n\n        self.add_function(func.__name__, func)\n        return func\n\n    def filter(self, func):\n        if isinstance(func, str):","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/cube-js/cube/blob/7d981676b36392fec34088b9afab6bdcad40207c/packages/cubejs-backend-native/python/cube/src/__init__.py#L169-L205","documentation":"add_variable() refuses to register a variable whose name is already taken by a registered function, because the template engine resolves names unambiguously between the two namespaces. This is an early conflict check to prevent silent shadowing.","triggerScenarios":"Calling Template.add_variable(name, val) after add_function(name, ...) with the same name string.","commonSituations":"Large templates where functions and variables share naming conventions (e.g. 'now'), dynamic registration loops that collide, refactors that renamed one side but not the other.","solutions":["Rename the variable to a name not used by any function.","Rename or remove the conflicting function registration if the variable should take precedence.","Track registered names before registering to detect collisions in dynamic code."],"exampleFix":"// before\ntemplate.add_function('now', get_now)\ntemplate.add_variable('now', '2024-01-01')  # conflict\n// after\ntemplate.add_function('now_fn', get_now)\ntemplate.add_variable('now', '2024-01-01')","handlingStrategy":"validation","validationCode":"if name in template.functions:\n    raise ValueError(f\"'{name}' already registered as a function\")\ntemplate.add_variable(name, val)","typeGuard":null,"tryCatchPattern":"try:\n    template.add_variable(name, val)\nexcept TemplateException:\n    template.add_variable(name + '_var', val)  # or choose another name","preventionTips":["Keep separate naming prefixes for functions vs variables","Check existing registrations before dynamic registration","Centralize name registration in one helper that deconflicts"],"tags":["python","template","name-conflict"],"backgroundTag":"name-already-in-use","analyzedSha":"7d981676b36392fec34088b9afab6bdcad40207c","analyzedAt":"2026-09-02T03:45:10.400Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}