Tag Expressions
Tag Expressions is a Python library that provides an implementation for parsing and evaluating Cucumber Tag Expressions. It allows you to check if a set of tags matches a given boolean expression string. The current version is 2.0.1, with releases typically occurring for maintenance or minor feature additions, following a stable core API.
Common errors
-
TypeError: evaluate() missing 1 required positional argument: 'tags'
cause The `evaluate` function was called without the required `tags` argument.fixProvide a list or other iterable of tag strings as the second argument to `evaluate`. Example: `evaluate('my_expression', ['tag1', 'tag2'])`. -
TypeError: argument of type 'str' is not iterable
cause A single string was passed as the `tags` argument to `evaluate` instead of an iterable of strings.fixWrap your single tag string in a list. Example: `evaluate('my_expression', ['my_tag'])` instead of `evaluate('my_expression', 'my_tag')`. -
NameError: name 'evaluate' is not defined
cause The `evaluate` function was used without being correctly imported.fixAdd `from tag_expressions import evaluate` at the top of your Python file.
Warnings
- breaking Version 2.0.0 dropped support for Python 2.x. The library is now Python 3.8+ only.
- gotcha The `evaluate` function expects the `tags` argument to be an iterable (e.g., a list or set) of strings, not a single string.
- gotcha This library implements the *Cucumber* Tag Expressions grammar, which has specific syntax and operator precedence. It is not a general-purpose boolean expression parser.
Install
-
pip install tag-expressions
Imports
- evaluate
from tag_expressions import evaluate
Quickstart
from tag_expressions import evaluate
# Basic tag evaluation
assert evaluate('tag1', ['tag1']) is True
assert evaluate('tag1', ['tag2']) is False
# AND operator
assert evaluate('tag1 and tag2', ['tag1', 'tag2']) is True
assert evaluate('tag1 and tag2', ['tag1']) is False
# OR operator
assert evaluate('tag1 or tag2', ['tag1']) is True
assert evaluate('tag1 or tag2', ['tag2']) is True
# NOT operator
assert evaluate('not tag1', ['tag2']) is True
assert evaluate('not tag1', ['tag1']) is False
# Combined expression
assert evaluate('(tag1 or tag2) and not tag3', ['tag1', 'tag4']) is True
assert evaluate('(tag1 or tag2) and not tag3', ['tag2', 'tag4']) is True
assert evaluate('(tag1 or tag2) and not tag3', ['tag1', 'tag3']) is False
print("All tag expressions evaluated successfully!")