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

AttributeError: type object 'Constructor' has no attribute 'add_to_loader_class' #133

Closed
LichAcademy opened this issue Apr 9, 2024 · 7 comments

Comments

@LichAcademy
Copy link

I cannot get this script to work on Forge or A1111. This is the error:

*** Error loading script: infinity_grid.py
    Traceback (most recent call last):
      File "C:\Users\ttrpg\sd\stable-diffusion-webui\modules\scripts.py", line 527, in load_scripts
        script_module = script_loading.load_module(scriptfile.path)
      File "C:\Users\ttrpg\sd\stable-diffusion-webui\modules\script_loading.py", line 10, in load_module
        module_spec.loader.exec_module(module)
      File "<frozen importlib._bootstrap_external>", line 883, in exec_module
      File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
      File "C:\Users\ttrpg\sd\stable-diffusion-webui\extensions\sd-infinity-grid-generator-script\scripts\infinity_grid.py", line 22, in <module>
        import gridgencore as core
      File "C:\Users\ttrpg\sd\stable-diffusion-webui\extensions\sd-infinity-grid-generator-script\gridgencore.py", line 256, in <module>
        YamlIncludeConstructor.add_to_loader_class(loader_class=GridYamlLoader, base_dir=ASSET_DIR)
    AttributeError: type object 'Constructor' has no attribute 'add_to_loader_class'

I'm not a programmer. This is what ChatGPT said to me:

===

The error occurs when the system tries to load the infinity_grid.py script, which in turn imports gridgencore.py. The actual error is thrown in gridgencore.py where it tries to use add_to_loader_class on YamlIncludeConstructor.

Given the code and the traceback, here are some thoughts:

  1. Import Error Handling: The script infinity_grid.py imports gridgencore which seems to rely on YamlIncludeConstructor. If the fallback import is used (from yaml_include import Constructor as YamlIncludeConstructor), it indicates that the direct import of YamlIncludeConstructor failed. This could mean that the yamlinclude library isn't installed properly or the wrong library is being imported due to a name mismatch.

  2. Attribute Error: The error AttributeError: type object 'Constructor' has no attribute 'add_to_loader_class' suggests that the imported Constructor class (aliased as YamlIncludeConstructor) doesn't have the method add_to_loader_class. This might indicate a version mismatch or incorrect library usage.

  3. Dynamic Module Loading: The function load_module dynamically loads a module from the given path. This dynamic loading happens in script_loading.py, which successfully loads the module but fails when executing its contents due to the attribute error mentioned.

  4. Path and Module Resolution: The load_module function uses importlib.util.spec_from_file_location to create a module spec from the given file location, then loads the module based on this spec. The module loads, but the issue arises when executing its content, specifically the part involving YamlIncludeConstructor.

@heftig
Copy link

heftig commented Apr 9, 2024

Looks like add_to_loader_class was removed from pyyaml-include 2.0's Constructor. Given what the previous version looked like, maybe using the following code will work with either version?

yaml.add_constructor("!include", yaml_include.Constructor(base_dir=ASSET_DIR), GridYamlLoader)

@mcmonkey4eva
Copy link
Member

(Asking ChatGPT for help, does not help. I love AI and all but, like, it's an LLM with no understanding of context and just says things)

Does the new commit fix it?

@heftig
Copy link

heftig commented Apr 9, 2024

No, it actually made things worse and broke loading the old library as well. Applying this seems to work:

diff --git a/gridgencore.py b/gridgencore.py
index 76d093d..a1f9416 100644
--- a/gridgencore.py
+++ b/gridgencore.py
@@ -5,15 +5,10 @@ from copy import copy
 from PIL import Image
 from git import Repo

-class GridYamlLoader(yaml.SafeLoader):
-    pass
-
 try:
     from yamlinclude import YamlIncludeConstructor
-    YamlIncludeConstructor.add_to_loader_class(loader_class=GridYamlLoader, base_dir=ASSET_DIR)
-except:
+except ImportError:
     from yaml_include import Constructor as YamlIncludeConstructor
-    yaml.add_constructor("!include", Constructor(base_dir=ASSET_DIR), GridYamlLoader)

 ######################### Core Variables #########################

@@ -256,6 +251,10 @@ def validate_single_param(p: str, v):

 ######################### YAML Parsing and Processing #########################

+class GridYamlLoader(yaml.SafeLoader):
+    pass
+yaml.add_constructor("!include", YamlIncludeConstructor(base_dir=ASSET_DIR), GridYamlLoader)
+
 class AxisValue:
     def __init__(self, axis, grid, key: str, val):
         self.axis = axis

mcmonkey4eva added a commit that referenced this issue Apr 9, 2024
@mcmonkey4eva
Copy link
Member

ohp, that's what I get for not doing a test launch -- fixed on old lib. New lib still don't know if it works or not naturally.

@LichAcademy
Copy link
Author

LichAcademy commented Apr 10, 2024

(Asking ChatGPT for help, does not help. I love AI and all but, like, it's an LLM with no understanding of context and just says things)

I thought it would explain my issue better than I could, that's all. Sorry, I didn't mean any offense.

Does the new commit fix it?

No, unfortunately. Still same error. I updated both WebUI and Forge. Thank you for trying, and thank you for getting back to me so promptly. I do appreciate it. 👍

@mcmonkey4eva
Copy link
Member

still no?! Argh. What's the new error?

@heftig
Copy link

heftig commented Apr 11, 2024

It seems to load fine for me with either version of pyyaml-include, now. Maybe they didn't update the extension?

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

3 participants