Skip to content

Commit

Permalink
pydantic v2: improving comments
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrodamascena committed Jul 10, 2023
1 parent bf6b31a commit ef98e88
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 3 deletions.
4 changes: 3 additions & 1 deletion aws_lambda_powertools/utilities/parser/models/apigw.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def check_message_id(cls, values):
raise ValueError("messageId is available only when the `eventType` is `MESSAGE`")
return values

# validator to normalize requestTimeEpoch
# Validator to normalize the requestTimeEpoch field
# Converts the provided timestamp value to a UTC datetime object
# See: https://github.com/pydantic/pydantic/issues/6518
@validator("requestTimeEpoch", pre=True)
def normalize_timestamp(cls, value):
date_utc = datetime.fromtimestamp(int(value) / 1000, tz=timezone.utc)
Expand Down
4 changes: 3 additions & 1 deletion aws_lambda_powertools/utilities/parser/models/apigwv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class RequestContextV2(BaseModel):
timeEpoch: datetime
http: RequestContextV2Http

# validator to normalize timestamp
# Validator to normalize the timeEpoch field
# Converts the provided timestamp value to a UTC datetime object
# See: https://github.com/pydantic/pydantic/issues/6518
@validator("timeEpoch", pre=True)
def normalize_timestamp(cls, value):
date_utc = datetime.fromtimestamp(int(value) / 1000, tz=timezone.utc)
Expand Down
3 changes: 3 additions & 0 deletions aws_lambda_powertools/utilities/parser/models/cloudwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class CloudWatchLogsLogEvent(BaseModel):
timestamp: datetime
message: Union[str, Type[BaseModel]]

# Validator to normalize the timestamp field
# Converts the provided timestamp value to a UTC datetime object
# See: https://github.com/pydantic/pydantic/issues/6518
@validator("timestamp", pre=True)
def coerc_timestamp(cls, value):
date_utc = datetime.fromtimestamp(value / 1000, tz=timezone.utc)
Expand Down
5 changes: 4 additions & 1 deletion aws_lambda_powertools/utilities/parser/models/kafka.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class KafkaRecordModel(BaseModel):
value: Union[str, Type[BaseModel]]
headers: List[Dict[str, bytes]]

# validators
# Added type ignore to keep compatibility between Pydantic v1 and v2
_decode_key = validator("key", allow_reuse=True)(base64_decode) # type: ignore[type-var, unused-ignore]

@validator("value", pre=True, allow_reuse=True)
Expand All @@ -34,6 +34,9 @@ def decode_headers_list(cls, value):
header[key] = bytes(values)
return value

# Validator to normalize the timestamp field
# Converts the provided timestamp value to a UTC datetime object
# See: https://github.com/pydantic/pydantic/issues/6518
@validator("timestamp", pre=True)
def normalize_timestamp(cls, value):
date_utc = datetime.fromtimestamp(int(value) / 1000, tz=timezone.utc)
Expand Down
3 changes: 3 additions & 0 deletions aws_lambda_powertools/utilities/parser/models/sqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class SqsAttributesModel(BaseModel):
SequenceNumber: Optional[str] = None
AWSTraceHeader: Optional[str] = None

# Validator to normalize the ApproximateFirstReceiveTimestamp and SentTimestamp fields
# Converts the provided timestamp value to a UTC datetime object
# See: https://github.com/pydantic/pydantic/issues/6518
@validator("ApproximateFirstReceiveTimestamp", "SentTimestamp", pre=True)
def normalize_timestamp(cls, value):
date_utc = datetime.fromtimestamp(int(value) / 1000, tz=timezone.utc)
Expand Down

0 comments on commit ef98e88

Please sign in to comment.