kcl-lang / kcl

KCL Programming Language (CNCF Sandbox Project). https://kcl-lang.io
https://kcl-lang.io
Apache License 2.0
1.61k stars 113 forks source link

`schema.instances()` is missing instances from deeper subclassed parents imported from module #1626

Closed diefans closed 3 weeks ago

diefans commented 3 weeks ago

Bug Report

While testing instances() I observed a strange behavior, where instances are lost.

1. Minimal reproduce step (Required)

I have prepared everything in https://github.com/bettermarks/klc-instances-issue

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

ìnstances() returns all instances of all sub schemas

3. What did you see instead (Required)

Instances are lost, if they are derived from an imported module schema at the second inheritance level.

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

v0.10.0-beta.2

Peefy commented 3 weeks ago

Hello you can set the full_pkg parameter to True of the schema.instances to see all schema instances. e.g.,

print("# v1/main.k #####")
print("v1.Base instances in v1/main.k: ", Base.instances(True))
print("v1.Config instances in v1/main.k: ", Config.instances(True))
print("v1.FooConfig instances in v1/main.k: ", FooConfig.instances(True))
print("\n\n")

and

print("# foo/bar/test.k #####")
print("v1.Base instances in foo/bar/test.k ${len(v1.Base.instances())}:", v1.Base.instances(True))
print("\nv1.Config instances in foo/bar/test.k ${len(v1.Config.instances())}:", v1.Config.instances(True))
print("\nv1.FooConfig instances in foo/bar/test.k ${len(v1.FooConfig.instances())}:", v1.FooConfig.instances(True))
print("\n\n")
Peefy commented 3 weeks ago

Because obtaining instances across packages is a side effect function, by default only schema instances from the main package will be obtained.

diefans commented 3 weeks ago

@Peefy ... so I guess then it is a documentation issue at https://www.kcl-lang.io/docs/reference/lang/spec/schema#members this parameter is never mentioned...

Thank you for your fast reply!

Peefy commented 3 weeks ago

Thank you! I will add the lost document with this feature.

diefans commented 3 weeks ago

...so I tested with this parameter and confirm that it is working...