{"record":{"id":"7f405c227991b684","repo":"QuantConnect/Lean","slug":"types-deriving-from-universeselectionmodel-must","errorCode":null,"errorMessage":"Types deriving from 'UniverseSelectionModel' must implement the 'def CreateUniverses(QCAlgorithm) method.","messagePattern":"Types deriving from 'UniverseSelectionModel' must implement the 'def CreateUniverses\\(QCAlgorithm\\) method\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"Algorithm/Selection/UniverseSelectionModel.py","lineNumber":33,"sourceCode":"\nclass UniverseSelectionModel:\n    '''Provides a base class for universe selection models.'''\n\n    def get_next_refresh_time_utc(self) -> datetime:\n        '''Gets the next time the framework should invoke the `CreateUniverses` method to refresh the set of universes.'''\n        if hasattr(self, \"GetNextRefreshTimeUtc\") and callable(self.GetNextRefreshTimeUtc):\n            return self.GetNextRefreshTimeUtc()\n        return datetime.max\n\n    def create_universes(self, algorithm: QCAlgorithm) -> list[Universe]:\n        '''Creates the universes for this algorithm. Called once after <see cref=\"IAlgorithm.Initialize\"/>\n        Args:\n            algorithm: The algorithm instance to create universes for</param>\n        Returns:\n            The universes to be used by the algorithm'''\n        if hasattr(self, \"CreateUniverses\") and callable(self.CreateUniverses):\n            return self.CreateUniverses(algorithm)\n        raise NotImplementedError(\"Types deriving from 'UniverseSelectionModel' must implement the 'def CreateUniverses(QCAlgorithm) method.\")\n","sourceCodeStart":15,"sourceCodeEnd":34,"githubUrl":"https://github.com/QuantConnect/Lean/blob/d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892/Algorithm/Selection/UniverseSelectionModel.py#L15-L34","documentation":"UniverseSelectionModel.create_universes is the entry point Lean calls (after Initialize) to obtain the algorithm's universes. The base class supports both the legacy PascalCase spelling CreateUniverses(algorithm) and the snake_case create_universes. If a subclass defines neither, it raises NotImplementedError telling you to implement CreateUniverses(QCAlgorithm). This is a contract-enforcement check that fires the first time Lean asks the model for its universes.","triggerScenarios":"You subclass UniverseSelectionModel and pass an instance to set_universe_selection, but your subclass does not define create_universes (snake_case) nor CreateUniverses (PascalCase). Lean calls the base create_universes, finds neither attribute via hasattr+callable, and raises. It also fires if you misspell the method (e.g. create_universe) or make it a non-callable attribute.","commonSituations":"A user subclasses UniverseSelectionModel intending to provide custom selection but forgets to implement the create_universes method. A Lean version migration changed the expected method name (PascalCase vs snake_case) and the subclass only had the old spelling. The method was defined but with a typo, so neither spelling is found.","solutions":["Implement def create_universes(self, algorithm) in your subclass returning a list[Universe].","If you are porting from an older Lean API, you may already have CreateUniverses — either keep it (the base supports it) or rename it to create_universes.","Verify the method name spelling exactly; a typo (e.g. create_universe) means neither spelling matches.","Ensure the method returns a list of Universe instances, not None."],"exampleFix":"# before — subclass with no create method\nclass MyUniverseSelectionModel(UniverseSelectionModel):\n    pass\nself.set_universe_selection(MyUniverseSelectionModel())  # raises when Lean calls it\n\n# after — implement the contract\nclass MyUniverseSelectionModel(UniverseSelectionModel):\n    def create_universes(self, algorithm):\n        return [algorithm.universe.add(MyUniverseSelectionModel.my_func)]\nself.set_universe_selection(MyUniverseSelectionModel())","handlingStrategy":"validation","validationCode":"# Verify the subclass implements the method before passing it to Lean\ndef has_create_universes(model):\n    return hasattr(model, 'create_universes') and callable(model.create_universes)\nmodel = MyUniverseSelectionModel()\nassert has_create_universes(model), \"Subclass must implement create_universes(self, algorithm)\"\nself.set_universe_selection(model)","typeGuard":"def implements_universe_contract(model):\n    \"\"\"True when the selection model exposes create_universes (or legacy CreateUniverses).\"\"\"\n    return ((hasattr(model, 'create_universes') and callable(model.create_universes))\n            or (hasattr(model, 'CreateUniverses') and callable(model.CreateUniverses)))","tryCatchPattern":null,"preventionTips":["Always implement def create_universes(self, algorithm) -> list[Universe] in subclasses.","Check the exact method spelling; typos cause neither spelling to be found.","When porting from older Lean, keep or rename the legacy CreateUniverses method."],"tags":["quantconnect","lean","universe-selection","interface-contract","python"],"backgroundTag":null,"analyzedSha":"d2c3659f877bfc2b5d9dc0fc89a9c7566f45e892","analyzedAt":"2026-08-13T13:52:21.013Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}