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

Could not resolve alias when running griffe + this on Strawberry's codebase #7

Closed
patrick91 opened this issue Oct 23, 2023 · 12 comments · Fixed by #8
Closed

Could not resolve alias when running griffe + this on Strawberry's codebase #7

patrick91 opened this issue Oct 23, 2023 · 12 comments · Fixed by #8

Comments

@patrick91
Copy link

Running griffe by itself works well, but as soon as I add griffe_typingdoc it breaks, see the full traceback:

griffe dump -e griffe_typingdoc strawberry
INFO       Loading package strawberry
Traceback (most recent call last):
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/dataclasses.py", line 1179, in _resolve_target
    resolved = self.modules_collection.get_member(self.target_path)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/mixins.py", line 78, in get_member
    return self.members[parts[0]].get_member(parts[1:])  # type: ignore[attr-defined]
           ~~~~~~~~~~~~^^^^^^^^^^
KeyError: 'strawberry'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/patrickarminio/.local/bin/griffe", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/cli.py", line 496, in main
    return commands[subcommand](**opts_dict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/cli.py", line 347, in dump
    loader = _load_packages(
             ^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/cli.py", line 94, in _load_packages
    loader.load_module(package)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/loader.py", line 135, in load_module
    top_module = self._load_package(package, submodules=submodules)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/loader.py", line 426, in _load_package
    top_module = self._load_module(package.name, package.path, submodules=submodules)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/loader.py", line 445, in _load_module
    return self._load_module_path(module_name, module_path, submodules=submodules, parent=parent)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/loader.py", line 474, in _load_module_path
    self._load_submodules(module)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/loader.py", line 479, in _load_submodules
    self._load_submodule(module, subparts, subpath)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/loader.py", line 514, in _load_submodule
    self._load_module(
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/loader.py", line 445, in _load_module
    return self._load_module_path(module_name, module_path, submodules=submodules, parent=parent)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/loader.py", line 468, in _load_module_path
    module = self._visit_module(code, module_name, module_path, parent)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/loader.py", line 536, in _visit_module
    module = visit(
             ^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/agents/visitor.py", line 108, in visit
    ).get_module()
      ^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/agents/visitor.py", line 178, in get_module
    self.visit(top_node)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/agents/visitor.py", line 189, in visit
    getattr(self, f"visit_{ast_kind(node)}", self.generic_visit)(node)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/agents/visitor.py", line 224, in visit_module
    self.generic_visit(node)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/agents/visitor.py", line 202, in generic_visit
    self.visit(child)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/agents/visitor.py", line 189, in visit
    getattr(self, f"visit_{ast_kind(node)}", self.generic_visit)(node)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/agents/visitor.py", line 487, in visit_functiondef
    self.handle_function(node)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/agents/visitor.py", line 475, in handle_function
    self.extensions.call("on_function_instance", node=node, func=function)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/extensions/base.py", line 338, in call
    getattr(extension, event)(node=node, **kwargs)
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe_typingdoc/_extension.py", line 84, in on_function_instance
    other_params_section := module._other_parameters_docs(node, func),
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe_typingdoc/_static.py", line 136, in _other_parameters_docs
    for attr in typed_dict.members.values()
                ^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.4/Frameworks/Python.framework/Versions/3.11/lib/python3.11/functools.py", line 1001, in __get__
    val = self.func(instance)
          ^^^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/dataclasses.py", line 867, in members
    final_target = self.final_target
                   ^^^^^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/dataclasses.py", line 1145, in final_target
    target = target.target  # type: ignore[assignment]
             ^^^^^^^^^^^^^
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/dataclasses.py", line 1114, in target
    self.resolve_target()
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/dataclasses.py", line 1173, in resolve_target
    self._resolve_target()
  File "/Users/patrickarminio/.local/pipx/venvs/griffe/lib/python3.11/site-packages/griffe/dataclasses.py", line 1181, in _resolve_target
    raise AliasResolutionError(self) from error
griffe.exceptions.AliasResolutionError: Could not resolve alias strawberry.schema.execute.ParseOptions pointing at strawberry.types.execution.ParseOptions (in strawberry/schema/execute.py:40)

I didn't spend too much time trying to figure the issue, I only tried to move ParseOptions around, but didn't fix the issue :)

I'm running griffe against here: https://github.com/strawberry-graphql/strawberry/

@pawamoy
Copy link
Member

pawamoy commented Oct 23, 2023

Thanks for the link to the repo, I'll try it 🙂 Is there a branch I should check out?

@patrick91
Copy link
Author

patrick91 commented Oct 23, 2023

@pawamoy main :D not using typing.Doc there yet though

@pawamoy
Copy link
Member

pawamoy commented Oct 23, 2023

Oh OK, so just adding the extension without actually adding typing.Doc to the code makes Griffe fail?

@patrick91
Copy link
Author

Oh OK, so just adding the extension without actually adding typing.Doc to the code makes Griffe fail?

correct!

@pawamoy
Copy link
Member

pawamoy commented Oct 23, 2023

Alright, thanks! Will investigate 🙂

@pawamoy
Copy link
Member

pawamoy commented Oct 23, 2023

OK, that's a bit dumb: I think it's an ordering issue.The typing doc extension finds an Unpack of a typed dict, which is imported from a file that appears after the current one, alphabetically, so it's not loaded yet, and upon trying to access its members, it cannot resolve it, and fails with an alias resolution error.

I guess the whole extension should run as a post-process, rather than during data loading.
This will require additional events/hooks in Griffe. The only workaround I see in the meantime is to move ParseOptions right above parse_document 😕 Far from ideal, but that can get you going in order to at least test the extension and its output.

@pawamoy
Copy link
Member

pawamoy commented Oct 23, 2023

I'm trying it myself right now, and found another bug 😅
If you want to patch it:

    if func.parent and func.parent.is_class and func.parameters and func.parameters[0].name in {"self", "cls"}:

(in the extension's _no_self_params function)

With this griffe dump finishes without crashing.

@pawamoy
Copy link
Member

pawamoy commented Oct 23, 2023

Actually I just pushed v0.2.3, fixing the index error, but not the alias resolution error yet.

@patrick91
Copy link
Author

I guess the whole extension should run as a post-process, rather than during data loading.
This will require additional events/hooks in Griffe. The only workaround I see in the meantime is to move ParseOptions right above parse_document 😕 Far from ideal, but that can get you going in order to at least test the extension and its output

I think that's what I did, but let me try again with 0.2.3, I'm fine with the workaround for now!

@patrick91
Copy link
Author

mmh, that doesn't seem to work, there's two similarly named files at play btw:

  1. strawberry.schema.execute.ParseOptions
  2. strawberry.types.execution.ParseOptions

@pawamoy
Copy link
Member

pawamoy commented Oct 23, 2023

Here's my diff:

diff --git a/strawberry/schema/execute.py b/strawberry/schema/execute.py
index 7a355604..21121c95 100644
--- a/strawberry/schema/execute.py
+++ b/strawberry/schema/execute.py
@@ -27,7 +27,7 @@ from strawberry.types import ExecutionResult
 from .exceptions import InvalidOperationTypeError
 
 if TYPE_CHECKING:
-    from typing_extensions import Unpack
+    from typing_extensions import Unpack, NotRequired, TypedDict
 
     from graphql import ExecutionContext as GraphQLExecutionContext
     from graphql import ExecutionResult as GraphQLExecutionResult
@@ -37,10 +37,13 @@ if TYPE_CHECKING:
 
     from strawberry.extensions import SchemaExtension
     from strawberry.types import ExecutionContext
-    from strawberry.types.execution import ParseOptions
     from strawberry.types.graphql import OperationType
 
 
+class ParseOptions(TypedDict):
+    max_tokens: NotRequired[int]
+
+
 def parse_document(query: str, **kwargs: Unpack[ParseOptions]) -> DocumentNode:
     return parse(query, **kwargs)

@patrick91
Copy link
Author

thanks! even it is a bit of duplication it works for me <3

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

Successfully merging a pull request may close this issue.

2 participants