Skip to content

fix: class generator produces duplicate imports when regenerating files #2656

@rnetser

Description

@rnetser

Summary

Fixes the class generator's user_code_parser.py to recognize the new MissingRequiredArgumentError import location (ocp_resources.exceptions) as a template import, preventing duplicate imports when regenerating resource classes with --overwrite.

Problem / Motivation

When regenerating resource files (e.g., class-generator --kind X --overwrite), the parse_user_code_from_file() function checks imports against a hardcoded template_imports set to distinguish template-generated imports from user-added imports. The new correct import pattern from ocp_resources.exceptions import MissingRequiredArgumentError (from the Jinja2 template) is missing from this set, causing the parser to treat it as user-added code and preserve it — resulting in duplicate imports alongside the freshly rendered template imports.

This was observed in PR #2647 where CodeRabbit flagged Ruff F811 (redefinition of unused imports).

Root Cause

class_generator/parsers/user_code_parser.py line 59-65: the template_imports set is missing:

"from ocp_resources.exceptions import MissingRequiredArgumentError"

Requirements

  1. Add from ocp_resources.exceptions import MissingRequiredArgumentError to template_imports in user_code_parser.py
  2. Update related tests in test_user_code_parser.py
  3. Verify all tests pass

Deliverables

  • Add from ocp_resources.exceptions import MissingRequiredArgumentError to template_imports in user_code_parser.py
  • Update related tests in test_user_code_parser.py
  • All tests pass

Notes

This is a one-line fix to the template_imports set, plus corresponding test updates.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions