Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 9, 2025

Replaces dataclasses with Pydantic BaseModel for all output type definitions to enable better validation and maintain compatibility with existing code.

Changes

Core Infrastructure

  • Base OutputType class: Migrated from @dataclass to Pydantic BaseModel
    • Added model_config with arbitrary_types_allowed, extra='allow', and populate_by_name=True
    • Implemented custom __getattribute__ and __setattr__ for backward compatibility with underscore-prefixed fields (_source, _type, _timestamp, etc.)
    • Updated toDict() to use model_dump() and keys() to use model_fields
    • Converted load() method to iterate over model_fields instead of fields()

Field Aliasing Pattern

Pydantic v2 disallows field names starting with underscores. All underscore-prefixed fields now use aliases:

# Before (dataclass)
_source: str = field(default='', repr=True, compare=False)
_timestamp: int = field(default_factory=lambda: time.time(), compare=False)

# After (Pydantic)
source_: str = Field(default='', alias='_source')
timestamp_: int = Field(default_factory=lambda: time.time(), alias='_timestamp')

# Access remains unchanged due to custom __getattribute__
item._source  # Still works
item._timestamp  # Still works

Output Type Classes

  • Converted all 18 output type classes (Ip, Url, Port, Vulnerability, etc.)
  • Replaced field() with Field() for factory defaults
  • Moved class attributes (_table_fields, _sort_by) outside class definitions
  • Converted __post_init__ methods to @model_validator(mode='after') decorators

Supporting Changes

  • CSV Exporter: Updated to use model_fields.keys() instead of fields()
  • Imports: Added proper type hints (Dict, List, Optional) across all output types

Compatibility

All existing code accessing output type attributes continues to work without modification. The custom attribute handlers transparently map item._source to item.source_ internally.

Original prompt

This section details on the original issue you should resolve

<issue_title>feat: use pydantic for output types validation</issue_title>
<issue_description>Switch from dataclasses to Pydantic for output types définitions. Make sure new invocations of OutputType base and derived classes doesn't break existing code. Look thoroughly through the code and adjust what's needed for it to function properly.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 9, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Co-authored-by: ocervell <9629314+ocervell@users.noreply.github.com>
Copilot AI changed the title [WIP] Switch from dataclasses to Pydantic for output types validation Migrate output types from dataclasses to Pydantic for validation Dec 9, 2025
Copilot AI requested a review from ocervell December 9, 2025 12:16
@ocervell ocervell marked this pull request as ready for review December 13, 2025 10:17
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

Successfully merging this pull request may close these issues.

feat: use pydantic for output types validation

2 participants