Skip to content

Commit

Permalink
Update to typeguard v4.10. Allows typechecking of | in annotations
Browse files Browse the repository at this point in the history
---------

Co-authored-by: David Liu <[email protected]>
  • Loading branch information
dependabot[bot] and david-yz-liu authored Aug 11, 2023
1 parent 81f04dc commit 96d4f36
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Make `graphviz` an optional dependency, and clarify the installation requirements for visualizing
control flow graphs.
- Fix `check_contrats` handling of forward references in class type annotations when using `check_contracts` decorator.
- Fix handling of `|` in type annotations (by updating to `typeguard` v4.1.0).

## [2.6.0] - 2023-08-06

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = [
"six",
"tabulate ~= 0.9.0",
"toml ~= 0.10.2",
"typeguard >= 2.13.3, < 3",
"typeguard >= 4.1.0, < 5",
"wrapt >= 1.15.0, < 2",
]
dynamic = ["version"]
Expand Down
24 changes: 16 additions & 8 deletions python_ta/contracts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from typing import Any, Callable, List, Optional, Set, Tuple

import wrapt
from typeguard import check_type
from typeguard import CollectionCheckStrategy, TypeCheckError, check_type

# Configuration options

Expand Down Expand Up @@ -158,8 +158,12 @@ def new_setattr(self: klass, name: str, value: Any) -> None:
if name in cls_annotations:
try:
_debug(f"Checking type of attribute {attr} for {klass.__qualname__} instance")
check_type(name, value, cls_annotations[name])
except TypeError:
check_type(
value,
cls_annotations[name],
collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS,
)
except TypeCheckError:
raise AssertionError(
f"Value {_display_value(value)} did not match type annotation for attribute "
f"{name}: {_display_annotation(cls_annotations[name])}"
Expand Down Expand Up @@ -209,7 +213,7 @@ def _check_function_contracts(wrapped, instance, args, kwargs):
try:
_debug(f"Checking type of parameter {param} in call to {wrapped.__qualname__}")
check_type_strict(param, arg, annotations[param])
except TypeError:
except (TypeError, TypeCheckError):
additional_suggestions = _get_argument_suggestions(arg, annotations[param])

raise PyTAContractError(
Expand Down Expand Up @@ -250,7 +254,7 @@ def _check_function_contracts(wrapped, instance, args, kwargs):
try:
_debug(f"Checking return type from call to {wrapped.__qualname__}")
check_type_strict("return", r, return_type)
except TypeError:
except (TypeError, TypeCheckError):
raise PyTAContractError(
f"{wrapped.__name__}'s return value {_display_value(r)} did not match "
f"return type annotation {_display_annotation(return_type)}"
Expand Down Expand Up @@ -297,7 +301,9 @@ def check_type_strict(argname: str, value: Any, expected_type: type) -> None:
type(value) is bool and expected_type is int
):
raise TypeError(f"type of {argname} must be {expected_type}; got {value} instead")
check_type(argname, value, expected_type)
check_type(
value, expected_type, collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS
)


def _get_argument_suggestions(arg: Any, annotation: type) -> str:
Expand Down Expand Up @@ -364,8 +370,10 @@ def _check_class_type_annotations(klass: type, instance: Any) -> None:
value = getattr(instance, attr)
try:
_debug(f"Checking type of attribute {attr} for {klass.__qualname__} instance")
check_type(attr, value, annotation)
except TypeError:
check_type(
value, annotation, collection_check_strategy=CollectionCheckStrategy.ALL_ITEMS
)
except TypeCheckError:
raise AssertionError(
f"{_display_value(value)} did not match type annotation for attribute {attr}: "
f"{_display_annotation(annotation)}"
Expand Down

0 comments on commit 96d4f36

Please sign in to comment.