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

TypeError: 'NoneType' object is not subscriptable in nested function parsing #68

Closed
BlueGlassBlock opened this issue May 7, 2022 · 1 comment

Comments

@BlueGlassBlock
Copy link
Contributor

BlueGlassBlock commented May 7, 2022

Describe the bug
See below.

To Reproduce

  1. Put the following code into test.py
class C:
    def __init__(self):
        def pl(i: int):
            return i + 1
  1. run griffe ./test.py
  2. get this (cropped from rich traceback)
│                                                                                                  │
│ E:\PythonProjects\GraiaProject\Ariadne\temp_env\lib\site-packages\griffe\agents\visitor.py:446   │
│ in handle_function                                                                               │
│                                                                                                  │
│   443 │   │                                                                                      │
│   444 │   │   if self.current.kind is Kind.CLASS and function.name == "__init__":                │
│   445 │   │   │   self.current = function  # type: ignore[assignment]  # temporary assign a f    │
│ ❱ 446 │   │   │   self.generic_visit(node)                                                       │
│   447 │   │   │   self.current = self.current.parent  # type: ignore[assignment]                 │
│   448 │                                                                                          │
│   449 │   def visit_functiondef(self, node: ast.FunctionDef) -> None:                            │
│                                                                                                  │
│ ╭─────────────────────────────────────── locals ───────────────────────────────────────╮         │
│ │          annotation = None                                                           │         │
│ │                 arg = <ast.arg object at 0x00000226391052E0>                         │         │
│ │ args_kinds_defaults = <reversed object at 0x00000226395BC610>                        │         │
│ │       base_property = None                                                           │         │
│ │          decorators = []                                                             │         │
│ │             default = None                                                           │         │
│ │            function = <Function('__init__', 2, 4)>                                   │         │
│ │                kind = <ParameterKind.positional_or_keyword: 'positional or keyword'> │         │
│ │     kwargs_defaults = <reversed object at 0x00000226395BC4F0>                        │         │
│ │              labels = set()                                                          │         │
│ │              lineno = 2                                                              │         │
│ │                node = <ast.FunctionDef object at 0x0000022639100700>                 │         │
│ │            overload = False                                                          │         │
│ │          parameters = <griffe.dataclasses.Parameters object at 0x00000226395BC5B0>   │         │
│ │         posonlyargs = []                                                             │         │
│ │   property_function = None                                                           │         │
│ │                self = <griffe.agents.visitor.Visitor object at 0x00000226395BC580>   │         │
│ ╰──────────────────────────────────────────────────────────────────────────────────────╯         │
│                                                                                                  │
│ E:\PythonProjects\GraiaProject\Ariadne\temp_env\lib\site-packages\griffe\agents\visitor.py:198   │
│ in generic_visit                                                                                 │
│                                                                                                  │
│   195 │   │   for before_visitor in self.extensions.before_children_visit:                       │
│   196 │   │   │   before_visitor.visit(node)                                                     │
│   197 │   │   for child in node.children:  # type: ignore[attr-defined]  # noqa: WPS437          │
│ ❱ 198 │   │   │   self.visit(child)                                                              │
│   199 │   │   for after_visitor in self.extensions.after_children_visit:                         │
│   200 │   │   │   after_visitor.visit(node)                                                      │
│   201                                                                                            │
│                                                                                                  │
│ ╭─────────────────────────────── locals ───────────────────────────────╮                         │
│ │ child = <ast.FunctionDef object at 0x00000226391058E0>               │                         │
│ │  node = <ast.FunctionDef object at 0x0000022639100700>               │                         │
│ │  self = <griffe.agents.visitor.Visitor object at 0x00000226395BC580> │                         │
│ ╰──────────────────────────────────────────────────────────────────────╯                         │
│                                                                                                  │
│ E:\PythonProjects\GraiaProject\Ariadne\temp_env\lib\site-packages\griffe\agents\visitor.py:185   │
│ in visit                                                                                         │
│                                                                                                  │
│   182 │   │   """                                                                                │
│   183 │   │   for before_visitor in self.extensions.before_visit:                                │
│   184 │   │   │   before_visitor.visit(node)                                                     │
│ ❱ 185 │   │   super().visit(node)                                                                │
│   186 │   │   for after_visitor in self.extensions.after_visit:                                  │
│   187 │   │   │   after_visitor.visit(node)                                                      │
│   188                                                                                            │
│                                                                                                  │
│ ╭───────────────────────────────── locals ─────────────────────────────────╮                     │
│ │ __class__ = <class 'griffe.agents.visitor.Visitor'>                      │                     │
│ │      node = <ast.FunctionDef object at 0x00000226391058E0>               │                     │
│ │      self = <griffe.agents.visitor.Visitor object at 0x00000226395BC580> │                     │
│ ╰──────────────────────────────────────────────────────────────────────────╯                     │
│                                                                                                  │
│ E:\PythonProjects\GraiaProject\Ariadne\temp_env\lib\site-packages\griffe\agents\base.py:19 in    │
│ visit                                                                                            │
│                                                                                                  │
│   16 │   │   Parameters:                                                                         │
│   17 │   │   │   node: The node to visit.                                                        │
│   18 │   │   """                                                                                 │
│ ❱ 19 │   │   getattr(self, f"visit_{node.kind}", self.generic_visit)(node)  # type: ignore[a     │
│   20 │                                                                                           │
│   21 │   def generic_visit(self, node: ast.AST) -> None:                                         │
│   22 │   │   """Visit the children of a node.                                                    │
│                                                                                                  │
│ ╭────────────────────────────── locals ───────────────────────────────╮                          │
│ │ node = <ast.FunctionDef object at 0x00000226391058E0>               │                          │
│ │ self = <griffe.agents.visitor.Visitor object at 0x00000226395BC580> │                          │
│ ╰─────────────────────────────────────────────────────────────────────╯                          │
│                                                                                                  │
│ E:\PythonProjects\GraiaProject\Ariadne\temp_env\lib\site-packages\griffe\agents\visitor.py:455   │
│ in visit_functiondef                                                                             │
│                                                                                                  │
│   452 │   │   Parameters:                                                                        │
│   453 │   │   │   node: The node to visit.                                                       │
│   454 │   │   """                                                                                │
│ ❱ 455 │   │   self.handle_function(node)                                                         │
│   456 │                                                                                          │
│   457 │   def visit_asyncfunctiondef(self, node: ast.AsyncFunctionDef) -> None:                  │
│   458 │   │   """Visit an async function definition node.                                        │
│                                                                                                  │
│ ╭────────────────────────────── locals ───────────────────────────────╮                          │
│ │ node = <ast.FunctionDef object at 0x00000226391058E0>               │                          │
│ │ self = <griffe.agents.visitor.Visitor object at 0x00000226395BC580> │                          │
│ ╰─────────────────────────────────────────────────────────────────────╯                          │
│                                                                                                  │
│ E:\PythonProjects\GraiaProject\Ariadne\temp_env\lib\site-packages\griffe\agents\visitor.py:438   │
│ in handle_function                                                                               │
│                                                                                                  │
│   435 │   │   │   │   base_property.labels.add("deletable")                                      │
│   436 │   │   else:                                                                              │
│   437 │   │   │   self.current[node.name] = function                                             │
│ ❱ 438 │   │   │   if self.current.overloads[function.name]:                                      │
│   439 │   │   │   │   function.overloads = self.current.overloads[function.name]                 │
│   440 │   │   │   │   del self.current.overloads[function.name]  # noqa: WPS420                  │
│   441                                                                                            │
│                                                                                                  │
│ ╭─────────────────────────────────────── locals ───────────────────────────────────────╮         │
│ │          annotation = Name(source='int', full='int')                                 │         │
│ │                 arg = <ast.arg object at 0x00000226395BC9A0>                         │         │
│ │ args_kinds_defaults = <reversed object at 0x00000226395C7280>                        │         │
│ │       base_property = None                                                           │         │
│ │          decorators = []                                                             │         │
│ │             default = None                                                           │         │
│ │            function = <Function('pl', 3, 4)>                                         │         │
│ │                kind = <ParameterKind.positional_or_keyword: 'positional or keyword'> │         │
│ │     kwargs_defaults = <reversed object at 0x00000226395C7220>                        │         │
│ │              labels = set()                                                          │         │
│ │              lineno = 3                                                              │         │
│ │                node = <ast.FunctionDef object at 0x00000226391058E0>                 │         │
│ │            overload = False                                                          │         │
│ │          parameters = <griffe.dataclasses.Parameters object at 0x00000226395C72E0>   │         │
│ │         posonlyargs = []                                                             │         │
│ │   property_function = None                                                           │         │
│ │                self = <griffe.agents.visitor.Visitor object at 0x00000226395BC580>   │         │
│ ╰──────────────────────────────────────────────────────────────────────────────────────╯         │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
TypeError: 'NoneType' object is not subscriptable

Both async def and def will cause this problem.

Expected behavior
The file is parsed correctly.

System

  • griffe version: 0.19.0
  • Python version: 3.9
  • OS: Windows

Additional context
Found this when building with the new mkdocstrings-python handler

BlueGlassBlock added a commit to GraiaProject/Ariadne that referenced this issue May 7, 2022
@pawamoy
Copy link
Member

pawamoy commented May 7, 2022

Fixed in 0.19.1! Thanks for the report 🙂

@pawamoy pawamoy closed this as completed May 7, 2022
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