Skip to content

Throw clear error when add_data first argument is not a custom data class#9473

Merged
Martin-Molinero merged 2 commits into
QuantConnect:masterfrom
AlexCatarino:bug-add-data-string-clear-error
May 12, 2026
Merged

Throw clear error when add_data first argument is not a custom data class#9473
Martin-Molinero merged 2 commits into
QuantConnect:masterfrom
AlexCatarino:bug-add-data-string-clear-error

Conversation

@AlexCatarino
Copy link
Copy Markdown
Member

Description

Calls like self.add_data("VIX", Resolution.DAILY) (passing a string where a custom data class is expected) used to fall into Extensions.CreateType, which silently built a dynamic assembly named after the string and returned a fake Type whose PythonActivator.Factory tried to Invoke() the str like a function. The downstream 'str' object is not callable PythonException surfaced confusingly and, depending on where the engine caught it during initialization, could appear to the user as a hang.

This PR validates the PyObject up front in the AddData(PyObject, ...) entry points using the existing TryCreateType helper and throws an ArgumentException that points the user at AddEquity / AddForex / etc. when the argument is not a custom data class.

Related Issue

N/A — no public issue was filed for this.

Motivation and Context

A common user mistake (treating add_data like add_equity) produced a confusing failure mode ('str' object is not callable / apparent hang) instead of a clear, actionable error.

Requires Documentation Change

No.

How Has This Been Tested?

Added AlgorithmAddDataTests.AddDataWithStringAsTypeArgumentThrowsClearError. The test was first run against the unfixed code to confirm the original symptom (PythonException: 'str' object is not callable thrown from PythonActivator.Factory via GetBaseDataInstance), then re-run against the fixed code to confirm an ArgumentException mentioning AddData and AddEquity is now thrown. The pre-existing PythonCustomDataTypes_AreAddedToSubscriptions_Successfully, PythonCustomDataTypes_AreAddedToConsolidator_Successfully, and AddingInvalidDataTypeThrows tests still pass.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

…ent from Python

Calls like self.add_data("VIX", Resolution.DAILY) routed through CreateType, which
silently built a dynamic assembly named after the string and returned a fake type
whose activator factory tried to invoke the str like a function. The downstream
'str' object is not callable PythonException surfaced confusingly (and could
manifest as an apparent hang depending on where it was caught), making the actual
mistake hard to diagnose.

Validate the PyObject up front via TryCreateType in the AddData(PyObject, ...)
entry points and throw an ArgumentException pointing the user at AddEquity /
AddForex / etc. when the argument is not a custom data class.
Use only two alternative methods as examples, as we don't need an exhaustive list.
@Martin-Molinero Martin-Molinero merged commit 521b333 into QuantConnect:master May 12, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants