Skip to content

Stubgen crashes on dataclass field #18886

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

Open
KapJI opened this issue Apr 5, 2025 · 2 comments · May be fixed by #18888
Open

Stubgen crashes on dataclass field #18886

KapJI opened this issue Apr 5, 2025 · 2 comments · May be fixed by #18888

Comments

@KapJI
Copy link
Contributor

KapJI commented Apr 5, 2025

Crash Report

When running stubgen on home assistant codebase on the latest master, it crashes with the following error.

Traceback

Traceback (most recent call last):
  File ".venv/bin/stubgen", line 10, in <module>
    sys.exit(main())
             ~~~~^^
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 1984, in main
    generate_stubs(options)
    ~~~~~~~~~~~~~~^^^^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 1787, in generate_stubs
    generate_stub_for_py_module(
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        mod,
        ^^^^
    ...<7 lines>...
        all_modules=all_module_names,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 1757, in generate_stub_for_py_module
    mod.ast.accept(gen)
    ~~~~~~~~~~~~~~^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/nodes.py", line 361, in accept
    return visitor.visit_mypy_file(self)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 504, in visit_mypy_file
    super().visit_mypy_file(o)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^
  File ".venv/lib/python3.13/site-packages/mypy/traverser.py", line 119, in visit_mypy_file
    d.accept(self)
    ~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/nodes.py", line 1200, in accept
    return visitor.visit_class_def(self)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 811, in visit_class_def
    super().visit_class_def(o)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^
  File ".venv/lib/python3.13/site-packages/mypy/traverser.py", line 155, in visit_class_def
    o.defs.accept(self)
    ~~~~~~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/nodes.py", line 1281, in accept
    return visitor.visit_block(self)
           ~~~~~~~~~~~~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 906, in visit_block
    super().visit_block(o)
    ~~~~~~~~~~~~~~~~~~~^^^
  File ".venv/lib/python3.13/site-packages/mypy/traverser.py", line 123, in visit_block
    s.accept(self)
    ~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/nodes.py", line 1368, in accept
    return visitor.visit_assignment_stmt(self)
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 943, in visit_assignment_stmt
    init = self.get_init(item.name, o.rvalue, annotation)
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 1254, in get_init
    initializer = self.get_assign_initializer(rvalue)
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 1275, in get_assign_initializer
    return f" = {rvalue.accept(p)}"
                 ~~~~~~~~~~~~~^^^
  File ".venv/lib/python3.13/site-packages/mypy/nodes.py", line 1990, in accept
    return visitor.visit_call_expr(self)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/stubgen.py", line 296, in visit_call_expr
    args.append(f"{name}={arg.accept(self)}")
                          ~~~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/nodes.py", line 2071, in accept
    return visitor.visit_unary_expr(self)
           ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File ".venv/lib/python3.13/site-packages/mypy/visitor.py", line 527, in visit_unary_expr
    raise NotImplementedError()
NotImplementedError

To Reproduce

from dataclasses import dataclass, field

@dataclass
class ClassName:
    field_name: float = field(default=-1, init=False)

Your Environment

  • Mypy version used: 67b70ce00ee9f2da1c49e75a044aad7ab7c137ff
  • Mypy command-line flags: stubgen test.py
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.13.2
  • Operating system and version: Linux, MacOS
@KapJI KapJI added the crash label Apr 5, 2025
@KapJI
Copy link
Contributor Author

KapJI commented Apr 5, 2025

cc @cdce8p In case you are interested.

@sterliakov
Copy link
Collaborator

This is just a simple visitor method missing, will fix today. There are other missing methods (dicts/lists/tuples are handled, while sets are not, for example), let's try to add a few more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants