Add instance factory function
See original GitHub issueI am using XSData to read dataclass instances which need additional checks.
The additional checks should be implemented on user supplied classes which derive from the XSData generated class:
XSData (raw) class:
@dataclass
class TModule(TTypeDefinition):
import_: List[TImport] = field(
default_factory=list,
metadata={
"name": "import",
"type": "Element",
"namespace": "http://xxx",
}
)
My class with additional checks:
class TModule(raw.TModule):
def check(self):
if condition ...
However, the problem is that my TModule is not instanciated during parsing. Currently, i use a “type hacking approach”
@functions_to_super
class TModule(raw.TModule):
def check(self):
if condition ...
where i copy the defined functions to the base class. This however is not ideal, since i want to present only my TModule to the user, not the raw classes.
Is there the option (or could this be added) to give a factory function to the parser
def createInstance(clazz);
if clazz.__name__ = "TModule":
return myTModule()
by which i can define for which dataclass which user supplied class is instanciated, so that the parsed objects dont contain any “raw” classes?
Kind regards
Hauke
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Thank you for the suggestion @hcw70
No currently there are no validations, even though the generator adds restrictions as fields metadata, they are just hints for the developer. At some point I wish to work on something like this but it’s not really high on my priorities.
The binding process can only fail on unknown properties and errors during type conversions, both of them are configurable.