click-type-test 2.0.0__tar.gz → 2.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: click-type-test
3
- Version: 2.0.0
3
+ Version: 2.0.2
4
4
  Summary: Test that type annotations match click parameter types
5
5
  Author-email: Stephen Rosen <sirosen@globus.org>
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: click-type-test
3
- Version: 2.0.0
3
+ Version: 2.0.2
4
4
  Summary: Test that type annotations match click parameter types
5
5
  Author-email: Stephen Rosen <sirosen@globus.org>
6
6
  License: MIT
@@ -9,6 +9,7 @@ click-type-test
9
9
  from __future__ import annotations
10
10
 
11
11
  import datetime
12
+ import enum
12
13
  import inspect
13
14
  import types
14
15
  import typing as t
@@ -273,7 +274,9 @@ def deduce_type_from_parameter(param: click.Parameter) -> type:
273
274
  # inferred from its value, use that as one of the possible types
274
275
  param_default = param.to_info_dict()["default"]
275
276
  if param_default is not None:
276
- possible_types.add(_type_of_value(param_default))
277
+ value_type = _type_of_value(param_default)
278
+ if not _type_or_literal_is_included(param_default, value_type, possible_types):
279
+ possible_types.add(value_type)
277
280
 
278
281
  # before returning, convert None -> NoneType
279
282
  try:
@@ -528,3 +531,61 @@ def _type_of_value(value: t.Any, *, _depth: int = 0) -> type:
528
531
  # fallthrough: use the class
529
532
  else:
530
533
  return value.__class__ # type: ignore[no-any-return]
534
+
535
+
536
+ def _type_or_literal_is_included(
537
+ value: t.Any, value_type: type, possible_types: set[type | None]
538
+ ) -> bool:
539
+ if value_type in possible_types:
540
+ return True
541
+ # int-bool-ness means this makes more sense to encode than ignore
542
+ if value_type is bool and int in possible_types:
543
+ return True
544
+
545
+ # check if the value is listed in a literal
546
+ if isinstance(value, (int, str, bool)) or issubclass(type(value), enum.Enum):
547
+ collected_literal_values: set[int | str | bool | enum.Enum] = set()
548
+ for possible in possible_types:
549
+ if t.get_origin(possible) is t.Literal:
550
+ collected_literal_values |= set(t.get_args(possible))
551
+
552
+ if value in collected_literal_values:
553
+ return True
554
+
555
+ # if the value is a tuple of Literal-viable types, it may match a Literal tuple in
556
+ # a multiple use opt
557
+ if (
558
+ isinstance(value, tuple)
559
+ and any(t.get_origin(x) is tuple for x in possible_types)
560
+ and set(t.get_args(value_type)).issubset({int, str, bool})
561
+ ):
562
+ for tuple_type in possible_types:
563
+ # we're only interested in types of the form tuple[T, ...]
564
+ if t.get_origin(tuple_type) is not tuple:
565
+ continue
566
+ typeargs = t.get_args(tuple_type)
567
+ if len(typeargs) != 2 or typeargs[1] is not Ellipsis:
568
+ continue
569
+
570
+ multi_use_type = typeargs[0]
571
+
572
+ # if it's a literal, check if all of the values are matched
573
+ if t.get_origin(multi_use_type) is t.Literal and all(
574
+ member in t.get_args(multi_use_type) for member in value
575
+ ):
576
+ return True
577
+
578
+ # otherwise, not a literal, check if all of the types used are within the
579
+ # type (potential union)
580
+ member_types = {_type_of_value(member) for member in value}
581
+ if _is_union(multi_use_type):
582
+ union_members = set(t.get_args(multi_use_type))
583
+ if member_types.issubset(union_members):
584
+ return True
585
+ else:
586
+ if len(member_types) == 1 and _compare_types(
587
+ member_types.pop(), multi_use_type
588
+ ):
589
+ return True
590
+
591
+ return False
@@ -10,7 +10,7 @@ lint = ["pre-commit"]
10
10
 
11
11
  [project]
12
12
  name = "click-type-test"
13
- version = "2.0.0"
13
+ version = "2.0.2"
14
14
  description = "Test that type annotations match click parameter types"
15
15
  readme = "README.md"
16
16
  requires-python = ">=3.10"