Skip to content

Commit 437615d

Browse files
committed
Updated docstring and use Annotation for some methods
1 parent ed2cdb5 commit 437615d

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

sample_fastapi/app/resources/calculator/services.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@ def __init__(self):
44
pass
55

66
def dec2hex(self, value: int) -> str:
7-
"""Convert integer to hexadecimal string with '0x' prefix"""
7+
"""Convert integer to hexadecimal string with '0x' prefix.
8+
9+
:param int value: Integer value to convert (can be negative, zero and positive)
10+
:return str: The converted hexadecimal with '0x' prefix
11+
"""
812
return hex(value)
913

1014
def hex2dec(self, value: str) -> int:
1115
"""Convert hexadecimal string (with or without prefix) into integer.
1216
13-
Raises `ValueError` for incorrect input"""
17+
:param str value: Hexadecimal value to convert
18+
:return int: The converted integer if `value` is a valid hexadecimal string
19+
:raises ValueError: Raised for incorrect input"""
1420
return int(value, 16)

sample_fastapi/app/resources/calculator/views.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
from typing import Annotated
2+
13
from fastapi import Depends, HTTPException, status
24

35
from .models import ConversionDecResponse, ConversionHexResponse
46
from .services import ConverterService
57

68

7-
async def conv_dec2hex(value: int, converter: ConverterService = Depends(ConverterService)) -> ConversionHexResponse:
9+
async def conv_dec2hex(
10+
value: int, converter: Annotated[ConverterService, Depends(ConverterService)]
11+
) -> ConversionHexResponse:
812
"""Convert integer input into hexadecimal string"""
913
return ConversionHexResponse(
1014
value=converter.dec2hex(value),
1115
)
1216

1317

14-
async def conv_hex2dec(value: str, converter: ConverterService = Depends(ConverterService)) -> ConversionDecResponse:
18+
async def conv_hex2dec(
19+
value: str, converter: Annotated[ConverterService, Depends(ConverterService)]
20+
) -> ConversionDecResponse:
1521
"""Convert hexadecimal string input into integer"""
1622
try:
1723
return ConversionDecResponse(

sample_fastapi/app/resources/hello/views.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Annotated
2+
13
from fastapi import Query
24

35
from .models import HelloMessage
@@ -8,6 +10,6 @@ async def respond_hello() -> HelloMessage:
810
return HelloMessage(message="Hello, World!")
911

1012

11-
async def respond_name_greet(name: str = Query()) -> HelloMessage:
13+
async def respond_name_greet(name: Annotated[str, Query()]) -> HelloMessage:
1214
"""Respond with "Hello, <name>!" message in a JSON"""
1315
return HelloMessage(message=f"Hello, {name}!")

0 commit comments

Comments
 (0)