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

Subclasses of UserObjects are omitted as valid input if no subclasses were defined at the time they were referenced #55

Closed
jtv8 opened this issue Jan 16, 2021 · 1 comment · Fixed by #56
Assignees

Comments

@jtv8
Copy link
Contributor

jtv8 commented Jan 16, 2021

from typing import List

from abc import ABC, abstractmethod

from wysdom import UserObject, UserProperty, SchemaArray, SchemaConst
from wysdom.mixins import RegistersSubclasses


class Pet(UserObject, RegistersSubclasses, ABC):
    pet_type: str = UserProperty(str)
    name: str = UserProperty(str)

    @abstractmethod
    def speak(self):
        pass


class Person(UserObject):
    first_name: str = UserProperty(str)
    last_name: str = UserProperty(str)
    pets: List[Pet] = UserProperty(SchemaArray(Pet))


class Dog(Pet):

    def speak(self):
        return f"{self.name} says Woof!"


class Greyhound(Dog):
    pet_type: str = UserProperty(SchemaConst("greyhound"))

    def speak(self):
        return f"{self.name}, the greyhound, says Woof!"


class Cat(Pet):
    pet_type: str = UserProperty(SchemaConst("cat"))

    def speak(self):
        return f"{self.name} says Miaow!"

example_dict_input = {
  "first_name": "Marge",
  "last_name": "Simpson",
  "pets": [
    {
      "pet_type": "greyhound",
      "name": "Santa's Little Helper",
    },
    {
      "pet_type": "cat",
      "name": "Snowball II",
    }
  ]
}
example = Person(example_dict_input)

Results in:

Traceback (most recent call last):
  File "late_subclass.py", line 57, in <module>
    example = Person(example_dict_input)
  File "/mnt/c/Users/U568912/Gitroot/wysdom/wysdom/user_objects/UserObject.py", line 136, in __init__
    super().__init__({**(value or {}), **kwargs}, json_dom_info)
  File "/mnt/c/Users/U568912/Gitroot/wysdom/wysdom/dom/DOMObject.py", line 44, in __init__
    self[key] = value
  File "/mnt/c/Users/U568912/Gitroot/wysdom/wysdom/dom/DOMObject.py", line 73, in __setitem__
    element_key=key
  File "/mnt/c/Users/U568912/Gitroot/wysdom/wysdom/object_schema/SchemaArray.py", line 36, in __call__
    item_type=self.items
  File "/mnt/c/Users/U568912/Gitroot/wysdom/wysdom/dom/DOMList.py", line 50, in __init__
    self[:] = value
  File "/mnt/c/Users/U568912/Gitroot/wysdom/wysdom/dom/DOMList.py", line 75, in __setitem__
    self.__json_element_data__[i] = (self._new_child_item(x) for x in o)
  File "/mnt/c/Users/U568912/Gitroot/wysdom/wysdom/dom/DOMList.py", line 75, in <genexpr>
    self.__json_element_data__[i] = (self._new_child_item(x) for x in o)
  File "/mnt/c/Users/U568912/Gitroot/wysdom/wysdom/dom/DOMList.py", line 82, in _new_child_item
    parent=self
  File "/mnt/c/Users/U568912/Gitroot/wysdom/wysdom/object_schema/SchemaObject.py", line 49, in __call__
    return self.object_type(value, dom_info)
TypeError: Can't instantiate abstract class Pet with abstract methods speak
@jtv8 jtv8 self-assigned this Jan 16, 2021
@jtv8 jtv8 closed this as completed in #56 Jan 16, 2021
jtv8 added a commit that referenced this issue Jan 16, 2021
* Defer evaluation of a UserObject schema entirely to resolve #55
* Bump version to 0.2.3
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 a pull request may close this issue.

1 participant