Skip to content

Commit

Permalink
chore(python): unbreak optional fields on Python 3.7
Browse files Browse the repository at this point in the history
The error was: TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

See https://stackoverflow.com/questions/76712720/typeerror-unsupported-operand-types-for-type-and-nonetype

Correction for 67a664a commit.

Part of #16
Relate to #13
  • Loading branch information
php-coder committed Apr 4, 2024
1 parent 67a664a commit 7e1ff65
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions examples/python/fastapi/postgres/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@

from pydantic import BaseModel

from typing import Optional

from db import db_connection

router = APIRouter()

class CreateCategoryDto(BaseModel):
name: str | None = None
name_ru: str | None = None
slug: str | None = None
user_id: int | None = None
name: Optional[str] = None
name_ru: Optional[str] = None
slug: Optional[str] = None
user_id: Optional[int] = None


@router.get('/v1/categories/count')
Expand Down
5 changes: 4 additions & 1 deletion src/templates/routes.py.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ from fastapi import APIRouter, Depends, HTTPException, status
<%# LATER: add only when POST/PUT endpoints are present -%>
from pydantic import BaseModel

<%# LATER: add only when POST/PUT endpoints are present -%>
from typing import Optional

from db import db_connection

router = APIRouter()
Expand Down Expand Up @@ -114,7 +117,7 @@ function query2dto(parser, method) {
function dto2model(dto) {
let result = `class ${dto.name}(BaseModel):\n`;
dto.props.forEach(prop => {
result += ` ${prop.name}: ${prop.type} | None = None\n`
result += ` ${prop.name}: Optional[${prop.type}] = None\n`
});
return result;
}
Expand Down

0 comments on commit 7e1ff65

Please sign in to comment.