Skip to content

Commit

Permalink
fix: Fix parameter default checking logic and diff tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Apr 6, 2023
1 parent 88fada8 commit 1b940fd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 9 additions & 5 deletions src/griffe/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
POSITIONAL = frozenset((ParameterKind.positional_only, ParameterKind.positional_or_keyword))
KEYWORD = frozenset((ParameterKind.keyword_only, ParameterKind.positional_or_keyword))
POSITIONAL_KEYWORD_ONLY = frozenset((ParameterKind.positional_only, ParameterKind.keyword_only))
VARIADIC = frozenset((ParameterKind.var_positional, ParameterKind.var_keyword))

logger = get_logger(__name__)

Expand Down Expand Up @@ -360,12 +361,15 @@ def _function_incompatibilities(old_function: Function, new_function: Function)

# checking if parameter changed default
breakage = ParameterChangedDefaultBreakage(new_function, old_param, new_param)
try:
if old_param.default is not None and old_param.default != new_param.default:
non_required = not old_param.required and not new_param.required
non_variadic = old_param.kind not in VARIADIC and new_param.kind not in VARIADIC
if non_required and non_variadic:
try:
if old_param.default != new_param.default:
yield breakage
except Exception: # noqa: BLE001 (equality checks sometimes fail, e.g. numpy arrays)
# TODO: emitting breakage on a failed comparison could be a preference
yield breakage
except Exception: # noqa: BLE001 (equality checks sometimes fail, e.g. numpy arrays)
# TODO: emitting breakage on a failed comparison could be a preference
yield breakage

# checking if required parameters were added
for new_param in new_function.parameters:
Expand Down
7 changes: 3 additions & 4 deletions tests/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
(
"def a(x, y): ...",
"def a(y, x): ...",
[BreakageKind.PARAMETER_MOVED],
[BreakageKind.PARAMETER_MOVED, BreakageKind.PARAMETER_MOVED],
),
(
"def a(x, y): ...",
Expand All @@ -161,7 +161,7 @@
(
"def a() -> int: ...",
"def a() -> str: ...",
[BreakageKind.RETURN_CHANGED_TYPE],
[], # not supported yet: BreakageKind.RETURN_CHANGED_TYPE
),
],
)
Expand All @@ -175,7 +175,6 @@ def test_diff_griffe(old_code: str, new_code: str, expected_breakages: list[Brea
"""
with temporary_visited_module(old_code) as old_module, temporary_visited_module(new_code) as new_module:
breaking = list(find_breaking_changes(old_module, new_module))
if not expected_breakages:
assert not breaking
assert len(breaking) == len(expected_breakages)
for breakage, expected_kind in zip(breaking, expected_breakages):
assert breakage.kind is expected_kind

0 comments on commit 1b940fd

Please sign in to comment.