Skip to content

Commit 7e1ff65

Browse files
committed
chore(python): unbreak optional fields on Python 3.7
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
1 parent 67a664a commit 7e1ff65

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

examples/python/fastapi/postgres/routes.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55

66
from pydantic import BaseModel
77

8+
from typing import Optional
9+
810
from db import db_connection
911

1012
router = APIRouter()
1113

1214
class CreateCategoryDto(BaseModel):
13-
name: str | None = None
14-
name_ru: str | None = None
15-
slug: str | None = None
16-
user_id: int | None = None
15+
name: Optional[str] = None
16+
name_ru: Optional[str] = None
17+
slug: Optional[str] = None
18+
user_id: Optional[int] = None
1719

1820

1921
@router.get('/v1/categories/count')

src/templates/routes.py.ejs

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ from fastapi import APIRouter, Depends, HTTPException, status
77
<%# LATER: add only when POST/PUT endpoints are present -%>
88
from pydantic import BaseModel
99

10+
<%# LATER: add only when POST/PUT endpoints are present -%>
11+
from typing import Optional
12+
1013
from db import db_connection
1114

1215
router = APIRouter()
@@ -114,7 +117,7 @@ function query2dto(parser, method) {
114117
function dto2model(dto) {
115118
let result = `class ${dto.name}(BaseModel):\n`;
116119
dto.props.forEach(prop => {
117-
result += ` ${prop.name}: ${prop.type} | None = None\n`
120+
result += ` ${prop.name}: Optional[${prop.type}] = None\n`
118121
});
119122
return result;
120123
}

0 commit comments

Comments
 (0)