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

Factory cannot access its parent component's Field #204

Closed
HamLaertes opened this issue Nov 18, 2020 · 2 comments
Closed

Factory cannot access its parent component's Field #204

HamLaertes opened this issue Nov 18, 2020 · 2 comments

Comments

@HamLaertes
Copy link

Describe the bug

In the official factory.py, there is an example which is

    @factory
    class F:
        a: int = Field()
        def build(self):
            return self.a + 4

    @component
    class C:
        a: int = Field(3)
        f: int = ComponentField(F)

    print(C().f)

I test the same code using the latest zookeeper vision

from zookeeper import factory, Field, ComponentField, component

@factory
class F:
    a: int = Field()
    def build(self) -> int:
       return self.a + 4

@component
class C:
    a: int = Field(3)
    f: int = ComponentField(F)

print(C().f)

But it raised an error:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    print(C().f)
  File "/usr/local/lib/python3.6/dist-packages/zookeeper/core/component.py", line 221, in wrapped_fn
    return result.build()
  File "/usr/local/lib/python3.6/dist-packages/zookeeper/core/factory.py", line 20, in wrapped_fn
    result = fn(factory_instance)
  File "test.py", line 7, in build
    return self.a + 4
  File "/usr/local/lib/python3.6/dist-packages/zookeeper/core/component.py", line 217, in wrapped_fn
    result = base_wrapped_fn(instance, name)
  File "/usr/local/lib/python3.6/dist-packages/zookeeper/core/component.py", line 194, in base_wrapped_fn
    raise e from None
  File "/usr/local/lib/python3.6/dist-packages/zookeeper/core/component.py", line 181, in base_wrapped_fn
    result = field.get_default(instance)
  File "/usr/local/lib/python3.6/dist-packages/zookeeper/core/field.py", line 146, in get_default
    f"Field '{self.name}' has no default or configured value."
AttributeError: Field 'a' has no default or configured value.

It seems that the factory didn't use it's parent component to configure its Field. I don't know what to do.

To Reproduce

Expected behavior

Environment

TensorFlow version: 2.3.1
Zookeeper version: 1.0.4

@AdamHillier
Copy link
Contributor

Hi, apologies for that! It seems the docstring is slightly out of date. The example does work correctly, it's just that you have to configure the component instance first:

from zookeeper import factory, Field, ComponentField, component, configure

@factory
class F:
    a: int = Field()
    def build(self) -> int:
       return self.a + 4

@component
class C:
    a: int = Field(3)
    f: int = ComponentField(F)

c = C()
configure(c, {})
print(c.f)

This is basically because configuration is required to properly set up all of the fields and the connections between them. I hope that helps! Thanks for filing the issue, I will update the docs.

@HamLaertes
Copy link
Author

I get it! Thanks for ur answer and all the works there very much.

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

No branches or pull requests

2 participants