Skip to content
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

Missing typing import for optional field #38

Closed
jankatins opened this issue Feb 9, 2024 · 1 comment · Fixed by #39
Closed

Missing typing import for optional field #38

jankatins opened this issue Feb 9, 2024 · 1 comment · Fixed by #39

Comments

@jankatins
Copy link
Contributor

jankatins commented Feb 9, 2024

Describe the bug

The following (shortend) proto file creates a non-valid pydantic python file:

syntax = "proto3";
package whatever.v1;

message Blub {
  int64 whatever = 1;
}

message CheckCreateCommitFromPreparationResponse {
  optional Blub blib = 1;
}

Converting with protobuf-to-pydantic gives this

# This is an automatically generated file, please do not change
# gen by protobuf_to_pydantic[v0.2.3](https://github.com/so1n/protobuf_to_pydantic)
# Protobuf Version: 4.25.2 
# Pydantic Version: 2.6.1 
from google.protobuf.message import Message  # type: ignore
from pydantic import BaseModel
from pydantic import Field


class Blub(BaseModel):

    whatever: int = Field(default=0) 


class CheckCreateCommitFromPreparationResponse(BaseModel):

    blib: typing.Optional[Blub] = Field(default=None) 

Which misses the import for typing and therefore the last lines errors on import.

To Reproduce
Steps to reproduce the behavior:

  1. Install the above versions (see python version)
  2. convert proto to pydantic
  3. try to import the pydantic file
  4. See error: NameError: name 'typing' is not defined. Did you forget to import 'typing'

Expected behavior
There is a typing import in the generated file and I can import the generated file without an error

@so1n
Copy link
Owner

so1n commented Feb 12, 2024

Thank you for your feedback, the fix was a few days late as I was on vacation (Chinese New Year).

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 a pull request may close this issue.

2 participants