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

Context/Scope of vars not working when schema/mixin imported from module #1777

Open
diefans opened this issue Dec 9, 2024 · 3 comments
Open

Comments

@diefans
Copy link

diefans commented Dec 9, 2024

Bug Report

I am very confused, how the scope/context is applied resp. how _default_config is resolved.

My naive understanding was (before I moved the definitions to the module) that the scope/context is created at "init-time" and is somehow passed/"shining" from module level through the deeper schema tree... but now this issue confuses my a lot...

1. Minimal reproduce step (Required)

If I move schema/mixin/protocol definitions to a module a context var is not found: the working example is here

# INFO: this is in schemata.foo
protocol LoginProtocol:
    a_url: str
    b_url: str

mixin DefaultLoginMixin for LoginProtocol:
    # INFO: If no a_url, b_url is defined in the final schema, we apply the
    # value from _default_login
    a_url: str = a_url or _default_login.a_url
    b_url: str = b_url or _default_login.b_url

schema Config:
    mixin [
        DefaultLoginMixin
    ]

schema Stage:
    [...str]: Config
    _default_login: LoginProtocol
import schemata.foo

ci00 = foo.Stage {
    # FIXME: how is _default_login reaching the mixin, when defined in one script,
    # but not if the mixin is imported from a module?
    _default_login = foo.LoginProtocol {
        a_url = "https://default.a_url"
        b_url = "https://default.b_url"
    }
    provider_1 = foo.Config {}
    provider_2 = foo.Config {
        # INFO: override the default
        a_url = "https://custom.a_url"
        b_url = "https://custom.b_url"
    }
}

2. What did you expect to see? (Required)

ci00:
  provider_1:
    a_url: https://default.a_url
    b_url: https://default.b_url
  provider_2:
    a_url: https://custom.a_url
    b_url: https://custom.b_url

3. What did you see instead (Required)

EvaluationError
 --> /home/olli/code/bm/integration-config/schemata/foo.k:8:1
  |
8 |     a_url: str = a_url or _default_login.a_url
  |  invalid value 'UndefinedType' to load attribute 'a_url'
  |

4. What is your KCL components version? (Required)

❯ kcl --version
kcl version v0.10.8
@diefans
Copy link
Author

diefans commented Dec 10, 2024

Even if I define the Config schema in the same module, as where _default_config is created/instantiated/scoped, it still does not work. So this seems to defy the meaning of "mixin"...

import schemata.foo

schema Config:
    # FIXME: seems like resolution of _default_login is just looking into,
    # where the mixin is defined and not where it is applied/used/mixed-in
    mixin [
        foo.DefaultLoginMixin
    ]

schema Stage:
    [...str]: Config
    _default_login: foo.LoginProtocol

ci00 = Stage {
    # FIXME: how is _default_login reaching the mixin, when defined in one script,
    # but not if the mixin is imported from a module?
    _default_login = foo.LoginProtocol {
        a_url = "https://default.a_url"
        b_url = "https://default.b_url"
    }
    provider_1 = Config {}
    provider_2 = Config {
        # INFO: override the default
        a_url = "https://custom.a_url"
        b_url = "https://custom.b_url"
    }
}

@diefans
Copy link
Author

diefans commented Dec 20, 2024

@Peefy can you confirm, that my observation makes sense to you...? I also tried to find the place in the code, where I can start to fix it by myself... (resolving this would greatly improve our kcl code)

@Peefy
Copy link
Contributor

Peefy commented Dec 20, 2024

cc @zong-zhe @He1pa

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