Skip to content

Generate Class Actions

Use BetterPy's Generate menu entries to scaffold common Python class shapes without leaving the editor. Each action inserts the class at the caret, adds any required imports, and starts a live template for editing the generated declaration in place.

Available generators

  • dataclass inserts a plain @dataclass.
  • pytest test class inserts a pytest class with one test, named from the first configured python_classes and python_functions patterns.
  • frozen dataclass (kw_only) inserts @dataclass(frozen=True, kw_only=True).
  • pydantic model (with config) inserts a BaseModel plus an empty ConfigDict().

How it behaves

  • The actions are available from Code | Generate (⌘N / Alt+Insert) in Python files.
  • The test-class action appears only when the current module or containing class matches the configured pytest collection naming. It uses the first configured class and function pattern when multiple patterns are present.
  • Test classes are always inserted at module scope so pytest can discover them.
  • Other generated classes are inserted into the surrounding class with matching indentation when the caret is inside a class body.
  • When the file is missing imports such as dataclass, BaseModel, or ConfigDict, BetterPy adds them without duplicating existing imports.
  • After insertion, use Tab to move through the live-template fields. Test classes expose the editable class and method name stems plus the test body; the other generators expose the class name and body.

Examples

class TestClass:
    def test_method(self):
        assert False, "Not implemented"
from dataclasses import dataclass


@dataclass
class NewDataclass:
    pass
from dataclasses import dataclass


@dataclass(frozen=True, kw_only=True)
class NewFrozenDataclass:
    pass
from pydantic import BaseModel, ConfigDict


class NewModel(BaseModel):
    model_config = ConfigDict()
    pass

Placement rules

If the caret is between existing statements, BetterPy inserts the generated class at that position instead of always appending it to the end of the file. That makes it practical to scaffold nested helper classes exactly where you want them.