Describe the bug
When instantiating a Pydantic model with a custom field type that inherits from datetime
and utilizes Arrow for datetime parsing, PyCharm's Pydantic plugin generates a type checking warning. The warning indicates a mismatch between the expected custom type and the actual Arrow type provided, even though the custom type is designed to accept Arrow objects or strings and convert them into datetime
instances. The warning message is: "Expected type 'ArrowPydantic', got 'Arrow' instead."
To Reproduce
Steps to reproduce the behavior:
ArrowPydantic
that inherits from datetime
and includes a validator to convert inputs into datetime
instances, accepting either Arrow
objects or strings.# Based on https://stackoverflow.com/a/75949606
class ArrowPydantic(datetime):
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, value: Union[Arrow, str]) -> datetime:
if type(value) is str:
value: Arrow = arrow.get(value)
return value._datetime
AuditLogRecord
that uses ArrowPydantic
for one of its fields.class AuditLogRecord(JsonResourceModel):
"""
Record of an audit log, part of a project package.
"""
actorEmailAddress: str = Field(
description='Email address of the person/system performing the action.',
)
actorName: str = Field(
description='Name of the person/system performing the action.',
)
action: str = Field(
description='The action that was performed.',
)
performedAt: ArrowPydantic = Field(
description='The time that the action was performed.',
)
class Config:
arbitrary_types_allowed = True
AuditLogRecord
with an Arrow
object for the ArrowPydantic
field. # Okay, now create a record
auditLogRecord = AuditLogRecord(
actorEmailAddress=actorEmailAddress,
actorName=actorName,
action=action,
performedAt=performedAt,
)
Expected behavior
The expected behavior is that the PyCharm Pydantic plugin correctly recognizes the custom type's ability to handle both Arrow
objects and strings, converting them into datetime
instances, without generating a type checking warning. The instantiation of AuditLogRecord
with an Arrow
object should be considered valid, given the custom logic defined in ArrowPydantic
.
Environments (please complete the following information):
Additional context
None
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too