Skip to content

Commit

Permalink
Merge pull request #37 from mpkocher/fix-unhashable-bug
Browse files Browse the repository at this point in the history
Fix issue with assuming defaults are hashable
  • Loading branch information
mpkocher authored Aug 24, 2021
2 parents 2ca496f + 67ab47f commit abdbbcc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
18 changes: 14 additions & 4 deletions pydantic_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,15 @@ def __to_type_description(
)
# FIXME Pydantic has a very odd default of None, which makes often can make the
# the "default" is actually None, or is not None
allowed_defaults: T.Set[T.Any] = (
{NOT_PROVIDED} if allow_none else {NOT_PROVIDED, None}
# avoid using in with a Set to avoid assumptions that default_value is hashable
allowed_defaults: T.List[T.Any] = (
[NOT_PROVIDED] if allow_none else [NOT_PROVIDED, None]
)
v = (
""
if any((default_value is x) for x in allowed_defaults)
else f"default:{default_value}"
)
v = "" if default_value in allowed_defaults else f"default:{default_value}"
required = " required=True" if is_required else ""
sep = " " if v else ""
xs = sep.join([t, v]) + required
Expand Down Expand Up @@ -252,8 +257,13 @@ def _add_pydantic_field_to_parser(
# case 1 is a very common cases and the provided CLI custom flags have a different semantic meaning
# to negate the default value. E.g., debug:bool = False, will generate a CLI flag of
# --enable-debug to set the value to True. Very common to set this to (-d, --debug) to True
# avoid using in with a Set {True,False} to avoid assumptions that default_value is hashable
is_bool_with_non_null_default = all(
(not is_required, not field.allow_none, default_value in {True, False})
(
not is_required,
not field.allow_none,
default_value is True or default_value is False,
)
)

try:
Expand Down
2 changes: 1 addition & 1 deletion pydantic_cli/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "4.0.0"
__version__ = "4.0.1"
3 changes: 2 additions & 1 deletion pydantic_cli/examples/simple_with_custom.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
import logging
from typing import Union
from typing import Union, List

from pydantic import BaseModel, Field

Expand All @@ -19,6 +19,7 @@ class Config(ExampleConfigDefaults, DefaultConfig):
max_records: int = Field(10, extras={"cli": ("-m", "--max-records")})
min_filter_score: float = Field(..., extras={"cli": ("-f", "--filter-score")})
alpha: Union[int, str] = 1
values: List[str] = ["a", "b", "c"]


def example_runner(opts: Options) -> int:
Expand Down

0 comments on commit abdbbcc

Please sign in to comment.