Skip to content

Commit

Permalink
Ensure that annotations are strings
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmojaki committed Sep 28, 2019
1 parent 01dc13a commit 74465e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions friendly_states/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,13 @@ def set_state(self, previous_state, new_state):


def extract_state_names(annotation):
if not isinstance(annotation, str):
raise ValueError(
"Found non-string annotation. Remember to add:\n\n"
"from __future__ import annotations\n\n"
"at the top of your file."
)

try:
tree = ast.parse(annotation)
except SyntaxError:
Expand Down
12 changes: 11 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from friendly_states.core import AttributeState, IncorrectInitialState, BaseState, MappingKeyState
from friendly_states.core import AttributeState, IncorrectInitialState, BaseState, MappingKeyState, extract_state_names
from friendly_states.exceptions import StateChangedElsewhere, IncorrectSummary, MultipleMachineAncestors, \
InheritedFromState, CannotInferOutputState, DuplicateStateNames, DuplicateOutputStates, UnknownOutputState, \
ReturnedInvalidState, GetStateDidNotReturnState
Expand Down Expand Up @@ -587,3 +587,13 @@ class Summary:
CommonState2: []

machine2.Machine.check_summary(Summary)


def test_extract_state_names():
assert extract_state_names("x x") is None
assert extract_state_names("") is None
assert extract_state_names("x;x") is None
assert extract_state_names("[x[y]]") is None
assert extract_state_names("[x[y]]") is None
with raises(ValueError):
extract_state_names(None)

0 comments on commit 74465e2

Please sign in to comment.