|
36 | 36 | from matplotlib._enums import JoinStyle, CapStyle |
37 | 37 |
|
38 | 38 | # Don't let the original cycler collide with our validating cycler |
39 | | -from cycler import Cycler, cycler as ccycler |
| 39 | +from cycler import Cycler, concat as cconcat, cycler as ccycler |
40 | 40 |
|
41 | 41 |
|
42 | 42 | class ValidateInStrings: |
@@ -845,13 +845,32 @@ def _eval_cycler_expr(node): |
845 | 845 | return left * right |
846 | 846 | raise ValueError(f"Unsupported operator: {type(node.op).__name__}") |
847 | 847 | if isinstance(node, ast.Call): |
848 | | - if not (isinstance(node.func, ast.Name) and node.func.id == 'cycler'): |
849 | | - raise ValueError("only the 'cycler()' function is allowed") |
850 | | - args = [ast.literal_eval(a) for a in node.args] |
| 848 | + if not (isinstance(node.func, ast.Name) |
| 849 | + and node.func.id in ('cycler', 'concat')): |
| 850 | + raise ValueError( |
| 851 | + "only the 'cycler()' and 'concat()' functions are allowed") |
| 852 | + func = cycler if node.func.id == 'cycler' else cconcat |
| 853 | + args = [_eval_cycler_expr(a) for a in node.args] |
851 | 854 | kwargs = {kw.arg: ast.literal_eval(kw.value) for kw in node.keywords} |
852 | | - return cycler(*args, **kwargs) |
853 | | - raise ValueError( |
854 | | - f"Unsupported expression in cycler string: {ast.dump(node)}") |
| 855 | + return func(*args, **kwargs) |
| 856 | + if isinstance(node, ast.Subscript): |
| 857 | + value = _eval_cycler_expr(node.value) |
| 858 | + sl = node.slice |
| 859 | + if isinstance(sl, ast.Slice): |
| 860 | + s = slice( |
| 861 | + ast.literal_eval(sl.lower) if sl.lower else None, |
| 862 | + ast.literal_eval(sl.upper) if sl.upper else None, |
| 863 | + ast.literal_eval(sl.step) if sl.step else None, |
| 864 | + ) |
| 865 | + return value[s] |
| 866 | + raise ValueError("only slicing is supported, not indexing") |
| 867 | + # Allow literal values (int, strings, lists, tuples) as arguments |
| 868 | + # to cycler() and concat(). |
| 869 | + try: |
| 870 | + return ast.literal_eval(node) |
| 871 | + except (ValueError, TypeError): |
| 872 | + raise ValueError( |
| 873 | + f"Unsupported expression in cycler string: {ast.dump(node)}") |
855 | 874 |
|
856 | 875 |
|
857 | 876 | # A validator dedicated to the named legend loc |
|
0 commit comments