Skip to content

Attribute 'arguments' of 'FuncItem' undefined in get_type_analyze_hook with caching #17300

Open
@qsantos

Description

@qsantos

Crash Report

Accessing attribute.node.arguments when using the get_type_analyze_hook hook sometimes after populating the cache crashes mypy with an undefiend access error.

Traceback

<class 'mypy.nodes.SymbolTableNode'>
<class 'mypy.nodes.FuncDef'>
<class 'list'>
Success: no issues found in 1 source file
<class 'mypy.nodes.SymbolTableNode'>
<class 'mypy.nodes.FuncDef'>
a.py:2: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build
Please report a bug at https://github.com/python/mypy/issues
version: 1.10.0
Traceback (most recent call last):
  File "venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "venv/lib/python3.12/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "venv/lib/python3.12/site-packages/mypy/main.py", line 103, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "venv/lib/python3.12/site-packages/mypy/main.py", line 187, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "venv/lib/python3.12/site-packages/mypy/build.py", line 193, in build
    result = _build(
  File "venv/lib/python3.12/site-packages/mypy/build.py", line 268, in _build
    graph = dispatch(sources, manager, stdout)
  File "venv/lib/python3.12/site-packages/mypy/build.py", line 2950, in dispatch
    process_graph(graph, manager)
  File "venv/lib/python3.12/site-packages/mypy/build.py", line 3348, in process_graph
    process_stale_scc(graph, scc, manager)
  File "venv/lib/python3.12/site-packages/mypy/build.py", line 3443, in process_stale_scc
    mypy.semanal_main.semantic_analysis_for_scc(graph, scc, manager.errors)
  File "venv/lib/python3.12/site-packages/mypy/semanal_main.py", line 93, in semantic_analysis_for_scc
    process_top_levels(graph, scc, patches)
  File "venv/lib/python3.12/site-packages/mypy/semanal_main.py", line 220, in process_top_levels
    deferred, incomplete, progress = semantic_analyze_target(
  File "venv/lib/python3.12/site-packages/mypy/semanal_main.py", line 349, in semantic_analyze_target
    analyzer.refresh_partial(
  File "venv/lib/python3.12/site-packages/mypy/semanal.py", line 614, in refresh_partial
    self.refresh_top_level(node)
  File "venv/lib/python3.12/site-packages/mypy/semanal.py", line 625, in refresh_top_level
    self.accept(d)
  File "venv/lib/python3.12/site-packages/mypy/semanal.py", line 6976, in accept
    node.accept(self)
  File "venv/lib/python3.12/site-packages/mypy/nodes.py", line 1349, in accept
    return visitor.visit_assignment_stmt(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/mypy/semanal.py", line 3077, in visit_assignment_stmt
    self.process_type_annotation(s)
  File "venv/lib/python3.12/site-packages/mypy/semanal.py", line 3591, in process_type_annotation
    analyzed = self.anal_type(s.type, allow_tuple_literal=allow_tuple_literal)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/mypy/semanal.py", line 7127, in anal_type
    typ = typ.accept(a)
          ^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/mypy/types.py", line 941, in accept
    return visitor.visit_unbound_type(self)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/mypy/typeanal.py", line 271, in visit_unbound_type
    typ = self.visit_unbound_type_nonoptional(t, defining_literal)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "venv/lib/python3.12/site-packages/mypy/typeanal.py", line 324, in visit_unbound_type_nonoptional
    return hook(AnalyzeTypeContext(t, t, self))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c.py", line 22, in handler
    print(type(attribute.node.arguments))
               ^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: attribute 'arguments' of 'FuncItem' undefined

To Reproduce

Run the shell script below:

#!/usr/bin/env bash
set -Eeuo pipefail

# Create a.py, b.py, c.py and mypy.ini
cat >a.py <<EOF
from b import C, X
x: X[C]
EOF


cat >b.py <<EOF
from typing import TypeVar, Generic

T = TypeVar('T')


class X(Generic[T]):
    pass


class C:
    def f(self) -> None:
        pass
EOF

cat >c.py <<EOF
from typing import Callable

from mypy.nodes import FuncDef
from mypy.plugin import AnalyzeTypeContext, Plugin
from mypy.types import Type


class MypyPlugin(Plugin):
    def get_type_analyze_hook(self, fullname: str) -> Callable[[AnalyzeTypeContext], Type] | None:
        if fullname != "b.X":
            return None
        return handler


def handler(ctx: AnalyzeTypeContext) -> Type:
    api = ctx.api.api
    base_type = api.anal_type(ctx.type.args[0])
    for name, attribute in base_type.type.names.items():
        if isinstance(attribute.node, FuncDef):
            print(type(attribute))
            print(type(attribute.node))
            print(type(attribute.node.arguments))
    return base_type


def plugin(version: str) -> type[MypyPlugin]:
    return MypyPlugin
EOF

cat >mypy.ini <<EOF
[mypy]
plugins = c.py
EOF

rm -Rf .mypy_cache/
# Populate the cache
mypy --show-traceback a.py
# Modify the hash of a.py
echo >>a.py
# Trigger the crash
mypy --show-traceback a.py

Your Environment

  • Mypy version used: 1.6.1, 1.7.1, 1.10.0, git (f60f458);
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): plugins = c.py
  • Python version used: 3.10, 3.12
  • Operating system and version: Mac Sonoma 14.5 (23F79)
$ pip freeze
mypy @ ../mypy
mypy-extensions==0.4.4
tomli==2.0.1
typed-ast==1.4.3
typing_extensions==4.12.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    crashtopic-pluginsThe plugin API and ideas for new plugins

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions