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

Debugging using print gives weird result (multiple run in if section) #1833

Open
spastorclovr opened this issue Jan 21, 2025 · 3 comments
Open

Comments

@spastorclovr
Copy link

Bug Report

1. Minimal reproduce step (Required)

_oxr: {str:} = {
    metadata = {
        name = "fastapi-gateway"
    }
    spec = {
    }
}
print ("running main")
if _oxr.spec.values == Undefined:
    print("In if")
    if not _oxr.metadata.labels:
      _namespace = "default_namespace"
    else:
      _namespace = _oxr.metadata.labels["crossplane.io/claim-namespace"]
    print(_namespace)
    _items = [{"namespace": _namespace}]
else:
    print("in else")
    _items = []

items = _items

2. What did you expect to see?

I would expect to see in the terminal:

running main
in if
items:
- namespace: default_namespace

3. What did you see instead (Required)

running main
In if
In if
In if
default_namespace
default_namespace
items:
- namespace: default_namespace

It feels like the program goes multiple times in the if 🤔 ? Adding the print(_namespace) and _items = [{"namespace": _namespace}] brought 2 more "In if". Accessing more the _namespace variable (using print or otherwise) would not add more "In if"...
The end result of the execution is correct, but this is confusing when trying to debug things.
Maybe something I am not doing right? Please advise.

Thx!

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

kcl version 0.11.0

@Freewalkr
Copy link

I have encountered the same problem. Seems like it's related to #1410 somehow.

@Freewalkr
Copy link

Freewalkr commented Jan 21, 2025

(del: I realized it's another problem and moved it to #1835, but it may be related)

@Freewalkr
Copy link

Freewalkr commented Jan 21, 2025

Here's my example where this behavior affects not only prints but also the rendered data:

import manifests

schema ThingsMixin:
    enable: bool = False
    things: [str] = []

    if enable:
        print("enabled")
        things += ["default_thing"]

        print("checking for java")
        if language == "java":
            things += ["java_thing"]
            print("added java thing")

        print("checking for python")
        if language.startswith("python"):
            things += ["python_thing"]
            print("added python thing")

schema Things:
    mixin [ThingsMixin]
    language: str = "other"

    _res: [str] = []
    _res += things
    res: [str] = _res

t = Things {
    enable = True
    language = "java"
}

renders to

enabled
enabled
enabled
checking for java
checking for java
added java thing
checking for python
enabled
checking for java
added java thing
checking for python
t:
  language: java
  res:
  - default_thing
  - java_thing
  - java_thing
  enable: true
  things:
  - default_thing
  - java_thing

Double java_thing in res shouldn't be there I guess.
If I use ternary to fill things, the result is correct.
(I use this ternary: (things: [str] = (["default_thing"] + (["java_thing"] if language == "java" else []) + (["python_thing"] if language.startswith("python") else [])) if enable else [])

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