Skip to content

Incorrect Import Generation for Nested Enums #113

@aniketpalu

Description

@aniketpalu

Describe the bug
When protobuf messages contain fields with enum types defined inside other messages (nested enums), protobuf_to_pydantic generates incorrect imports that reference the enum by its name directly rather than as a nested class attribute. This causes type checking errors and requires manual patching.
Dependencies

python version: sys.version_info(major=3, minor=11, micro=8, releaselevel='final', serial=0)

############# dependencies ############## 
    grpc:            1.62.3
    pydantic:        2.10.6

########## Expand dependencies ########## 
    mypy-protobuf:   3.3.0
    toml:            0.10.2

########## Format dependencies ########## 
    autoflake:       Not Install
    black:           Not Install
    isort:           Not Install

Protobuf File Content

Filename: feast/types/Value.proto

syntax = "proto3";
package feast.types;

message ValueType {
enum Enum {
INVALID = 0;
INT32 = 1;
INT64 = 2;
STRING = 3;
}
}

Filename: feast/core/Entity.proto

syntax = "proto3";
package feast.core;

import "feast/types/Value.proto";

message Entity {
string name = 1;
feast.types.ValueType.Enum value_type = 2;
}

CLI (if use plugin mode)

python -m grpc_tools.protoc \
-I. \
--protobuf-to-pydantic_out=. \
feast/types/Value.proto feast/core/Entity.proto

Output content

Filename: feast/types/Value_p2p.py

from pydantic import BaseModel, Field
from enum import IntEnum

class ValueType(BaseModel):
class Enum(IntEnum):
INVALID = 0
INT32 = 1
INT64 = 2
STRING = 3

Filename: feast/core/Entity_p2p.py

from pydantic import BaseModel, Field
import typing
from ..types.Value_p2p import Enum # WRONG: should import ValueType

class Entity(BaseModel):
name: str = Field(default="")
value_type: Enum = Field(default=0) # WRONG: should be ValueType.Enum

Expected behavior
The generated code should correctly import the parent class and reference the nested enum:

Filename: feast/core/Entity_p2p.py

from pydantic import BaseModel, Field
import typing
from ..types.Value_p2p import ValueType # Correct

class Entity(BaseModel):
name: str = Field(default="")
value_type: ValueType.Enum = Field(default=0) # Correct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions