Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pythongen may inject additional unknown_ attributes into __post_init__ when type designators are used #2107

Open
cmungall opened this issue May 13, 2024 · 0 comments
Labels
bug Something that should work but isn't, with an example and a test case. generator-python

Comments

@cmungall
Copy link
Member

When type_designators are used on a slot s, pythongen can inject an additional attribute unknown_s into the post_init. This generally doesn't cause issues but can be confusing if an object is introspected with vars.

This can be seen on the current compliance tests for type_designator. The test passes, but can be seen in the generated class: tests/test_compliance/output/test_designates_type/Rstring/generated/python_dataclasses.py:

    def __post_init__(self, *_: List[str], **kwargs: Dict[str, Any]):
        self.type = str(self.class_name)

        super().__post_init__(**kwargs)
        self.unknown_type = str(self.class_name)


    def __new__(cls, *args, **kwargs):

        type_designator = "type"
        if not type_designator in kwargs:
            return super().__new__(cls,*args,**kwargs)
        else:
            type_designator_value = kwargs[type_designator]
            target_cls = cls._class_for("class_name", type_designator_value)


            if target_cls is None:
                raise ValueError(f"Wrong type designator value: class {cls.__name__} "
                                 f"has no subclass with ['class_name']='{kwargs[type_designator]}'")
            return super().__new__(target_cls,*args,**kwargs)
cmungall added a commit to linkml/linkml-owl that referenced this issue May 13, 2024
@nlharris nlharris added bug Something that should work but isn't, with an example and a test case. generator-python labels May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that should work but isn't, with an example and a test case. generator-python
Projects
None yet
Development

No branches or pull requests

2 participants