|
8 | 8 | import os |
9 | 9 | import pathlib |
10 | 10 | import sys |
| 11 | +import types |
11 | 12 | import typing |
12 | 13 | import typing as t |
13 | 14 | from typing import cast, get_args |
@@ -264,27 +265,6 @@ def convert( |
264 | 265 | return self._datetime_from_format(value, param, ctx) |
265 | 266 |
|
266 | 267 |
|
267 | | -class DateType(DateTimeType): |
268 | | - """ |
269 | | - A specialized type for handling date types that extends DateTimeType but returns date objects. |
270 | | - """ |
271 | | - |
272 | | - def convert( |
273 | | - self, value: typing.Any, param: typing.Optional[click.Parameter], ctx: typing.Optional[click.Context] |
274 | | - ) -> typing.Any: |
275 | | - if isinstance(value, ArtifactQuery): |
276 | | - return value |
277 | | - |
278 | | - # First convert using the parent DateTime logic |
279 | | - dt_value = super().convert(value, param, ctx) |
280 | | - |
281 | | - # Then convert to date if it's a datetime object |
282 | | - if isinstance(dt_value, datetime.datetime): |
283 | | - return dt_value.date() |
284 | | - |
285 | | - return dt_value |
286 | | - |
287 | | - |
288 | 268 | class DurationParamType(click.ParamType): |
289 | 269 | name = "[1:24 | :22 | 1 minute | 10 days | ...]" |
290 | 270 |
|
@@ -540,11 +520,7 @@ def literal_type_to_click_type(lt: LiteralType, python_type: typing.Type) -> cli |
540 | 520 | for i in range(len(lt.union_type.variants)): |
541 | 521 | variant = lt.union_type.variants[i] |
542 | 522 | variant_python_type = typing.get_args(python_type)[i] |
543 | | - # Special handling for datetime.date in union types |
544 | | - if variant_python_type is datetime.date: |
545 | | - ct = DateType() |
546 | | - else: |
547 | | - ct = literal_type_to_click_type(variant, variant_python_type) |
| 523 | + ct = literal_type_to_click_type(variant, variant_python_type) |
548 | 524 | cts.append(ct) |
549 | 525 |
|
550 | 526 | return UnionParamType(cts) |
@@ -585,7 +561,14 @@ def convert( |
585 | 561 | return value |
586 | 562 | try: |
587 | 563 | # If the expected Python type is datetime.date, adjust the value to date |
588 | | - if self._python_type is datetime.date and isinstance(value, datetime.datetime): |
| 564 | + is_union = typing.get_origin(self._python_type) in ( |
| 565 | + typing.Union, |
| 566 | + types.UnionType, |
| 567 | + ) |
| 568 | + args = typing.get_args(self._python_type) |
| 569 | + is_date_type = (is_union and datetime.date in args) or self._python_type is datetime.date |
| 570 | + |
| 571 | + if is_date_type and isinstance(value, datetime.datetime): |
589 | 572 | # Click produces datetime, so converting to date to avoid type mismatch error |
590 | 573 | value = value.date() |
591 | 574 | # If the input matches the default value in the launch plan, serialization can be skipped. |
|
0 commit comments