Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pendulum.DateTime instantiation from dt.datetime #158

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pydantic_extra_types/pendulum_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

try:
from pendulum import DateTime as _DateTime
from pendulum import parse
from pendulum import parse, instance
except ModuleNotFoundError: # pragma: no cover
raise RuntimeError(
'The `pendulum_dt` module requires "pendulum" to be installed. You can install it with "pip install pendulum".'
)
import datetime as dt
from typing import Any, List, Type

from pydantic import GetCoreSchemaHandler
Expand Down Expand Up @@ -65,6 +66,8 @@ def _validate(cls, value: Any, handler: core_schema.ValidatorFunctionWrapHandler
# if we are passed an existing instance, pass it straight through.
if isinstance(value, _DateTime):
return handler(value)
elif isinstance(value, dt.datetime):
return instance(value)

# otherwise, parse it.
try:
Expand Down
Loading