-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix check_contracts handling of forward references in type annotations (
#941)
- Loading branch information
1 parent
df754d2
commit 81f04dc
Showing
8 changed files
with
63 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
"""Tests for forward references in instance attribute type annotations.""" | ||
from __future__ import annotations | ||
|
||
from typing import Any, Optional | ||
|
||
import pytest | ||
|
||
from python_ta.contracts import check_contracts | ||
|
||
|
||
@check_contracts | ||
class Node: | ||
"""A node in a linked list.""" | ||
|
||
item: Any | ||
next: Optional[Node] | ||
|
||
def __init__(self, item: Any) -> None: | ||
self.item = item | ||
self.next = None | ||
|
||
def bad_method(self) -> None: | ||
"""Set self.next to an invalid value, violating the type annotation.""" | ||
self.next = 1 | ||
|
||
|
||
def test_node_no_error() -> None: | ||
"""Test that a Node can be initialized without error.""" | ||
Node(1) | ||
|
||
|
||
def test_node_setattr_no_error() -> None: | ||
"""Test that assigning a valid attribute to a Node does not raise an error.""" | ||
node = Node(1) | ||
node.next = Node(2) | ||
|
||
assert node.next.item == 2 | ||
|
||
|
||
def test_node_setattr_error() -> None: | ||
"""Test that assigning an invalid attribute to a Node raises an AssertionError.""" | ||
node = Node(1) | ||
|
||
with pytest.raises(AssertionError): | ||
node.next = 2 | ||
|
||
|
||
def test_node_method_error() -> None: | ||
"""Test that violating a Node type annotation with a method call raises an AssertionError.""" | ||
node = Node(1) | ||
|
||
with pytest.raises(AssertionError): | ||
node.bad_method() |
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion
4
...est_contracts_type_alias_abstract_ring.py → ...est_contracts_type_alias_abstract_ring.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters