Skip to content

Commit 41bfc1c

Browse files
committed
refactor: updated types to work with 3.8
1 parent 1c9f09d commit 41bfc1c

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

djc_core/djc_safe_eval/eval.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import builtins
2-
from typing import Any, Callable, Dict, Mapping, Optional
2+
from typing import Any, Callable, Dict, Mapping, Optional, Tuple
33

44
from djc_core.djc_core import safe_eval as safe_eval_rust
55
from djc_core.djc_safe_eval.error import error_context, _format_error_with_context
@@ -73,7 +73,7 @@ def safe_eval(
7373
def variable_fn(
7474
__context: Mapping[str, Any],
7575
__source: str,
76-
__token: tuple[int, int],
76+
__token: Tuple[int, int],
7777
var_name: str,
7878
) -> Any:
7979
if not validate_variable(var_name):
@@ -88,7 +88,7 @@ def variable_fn(
8888
def attribute_fn(
8989
__context: Mapping[str, Any],
9090
__source: str,
91-
__token: tuple[int, int],
91+
__token: Tuple[int, int],
9292
obj: Any,
9393
attr_name: str,
9494
) -> Any:
@@ -106,7 +106,7 @@ def attribute_fn(
106106
def subscript_fn(
107107
__context: Mapping[str, Any],
108108
__source: str,
109-
__token: tuple[int, int],
109+
__token: Tuple[int, int],
110110
obj: Any,
111111
key: Any,
112112
) -> Any:
@@ -122,7 +122,7 @@ def subscript_fn(
122122
def call_fn(
123123
__context: Mapping[str, Any],
124124
__source: str,
125-
__token: tuple[int, int],
125+
__token: Tuple[int, int],
126126
func: Callable,
127127
*args: Any,
128128
**kwargs: Any,
@@ -139,7 +139,7 @@ def call_fn(
139139
def assign_fn(
140140
__context: Mapping[str, Any],
141141
__source: str,
142-
__token: tuple[int, int],
142+
__token: Tuple[int, int],
143143
var_name: str,
144144
value: Any,
145145
) -> Any:
@@ -242,7 +242,7 @@ def evaluate(context: Dict[str, Any]) -> Any:
242242
# Each interceptor function receives the same 3 first positional arguments:
243243
# - __context: Mapping[str, Any] - The evaluation context
244244
# - __source: str - The source code
245-
# - __token: tuple[int, int] - The token tuple (start_index, end_index)
245+
# - __token: Tuple[int, int] - The token tuple (start_index, end_index)
246246
#
247247
# The __source and __token arguments are used by `@error_context` decorator to add the position
248248
# where the error occurred to the error message.
@@ -256,7 +256,7 @@ def evaluate(context: Dict[str, Any]) -> Any:
256256

257257
@error_context("variable")
258258
def variable(
259-
__context: Mapping[str, Any], __source: str, __token: tuple[int, int], var_name: str
259+
__context: Mapping[str, Any], __source: str, __token: Tuple[int, int], var_name: str
260260
) -> Any:
261261
"""Look up a variable in the evaluation context, e.g. `my_var`"""
262262
if not is_safe_variable(var_name):
@@ -268,7 +268,7 @@ def variable(
268268
def attribute(
269269
__context: Mapping[str, Any],
270270
__source: str,
271-
__token: tuple[int, int],
271+
__token: Tuple[int, int],
272272
obj: Any,
273273
attr_name: str,
274274
) -> Any:
@@ -284,7 +284,7 @@ def attribute(
284284
def subscript(
285285
__context: Mapping[str, Any],
286286
__source: str,
287-
__token: tuple[int, int],
287+
__token: Tuple[int, int],
288288
obj: Any,
289289
key: Any,
290290
) -> Any:
@@ -300,7 +300,7 @@ def subscript(
300300
def call(
301301
__context: Mapping[str, Any],
302302
__source: str,
303-
__token: tuple[int, int],
303+
__token: Tuple[int, int],
304304
func: Callable,
305305
*args: Any,
306306
**kwargs: Any,
@@ -319,7 +319,7 @@ def call(
319319
def assign(
320320
__context: Mapping[str, Any],
321321
__source: str,
322-
__token: tuple[int, int],
322+
__token: Tuple[int, int],
323323
var_name: str,
324324
value: Any,
325325
) -> Any:
@@ -338,7 +338,7 @@ def assign(
338338
def slice(
339339
__context: Mapping[str, Any],
340340
__source: str,
341-
__token: tuple[int, int],
341+
__token: Tuple[int, int],
342342
lower: Any = None,
343343
upper: Any = None,
344344
step: Any = None,
@@ -355,10 +355,10 @@ def slice(
355355
def interpolation(
356356
__context: Mapping[str, Any],
357357
__source: str,
358-
__token: tuple[int, int],
358+
__token: Tuple[int, int],
359359
value: Any,
360360
expression: str,
361-
conversion: str | None,
361+
conversion: Optional[str],
362362
format_spec: str,
363363
) -> Any:
364364
"""Process t-string interpolation."""
@@ -371,7 +371,7 @@ def interpolation(
371371

372372
@error_context("template")
373373
def template(
374-
__context: Mapping[str, Any], __source: str, __token: tuple[int, int], *parts: Any
374+
__context: Mapping[str, Any], __source: str, __token: Tuple[int, int], *parts: Any
375375
) -> Any:
376376
"""Construct a template from parts."""
377377
try:
@@ -385,7 +385,7 @@ def template(
385385
def format(
386386
__context: Mapping[str, Any],
387387
__source: str,
388-
__token: tuple[int, int],
388+
__token: Tuple[int, int],
389389
template_string: str,
390390
*args: Any,
391391
) -> str:

tests/test_safe_eval.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ruff: noqa: E731
22
import re
33
from dataclasses import dataclass, field
4-
from typing import Any, NamedTuple
4+
from typing import Any, Dict, List, NamedTuple
55

66
import pytest
77
from djc_core import SecurityError, safe_eval, unsafe
@@ -23,7 +23,7 @@ class Obj:
2323
start: int = 1
2424
end: int = 5
2525
nested: Nested = field(default_factory=Nested)
26-
items: dict[Any, Value] = field(
26+
items: Dict[Any, Value] = field(
2727
default_factory=lambda: {"test": Value(value=42), 0: Value(value=10)}
2828
)
2929

@@ -533,7 +533,7 @@ def test_allow_multiple_comprehensions(self):
533533
@dataclass
534534
class Item:
535535
name: str
536-
children: list[int]
536+
children: List[int]
537537

538538
items = [Item("a", [1, 2]), Item("b", [3, 4])]
539539
context = {"items": items}
@@ -619,7 +619,7 @@ def test_local_variable_tracking_in_multiple_comprehensions(self):
619619
@dataclass
620620
class Item:
621621
name: str
622-
children: list[int]
622+
children: List[int]
623623

624624
items = [Item("a", [1]), Item("b", [2])]
625625
context = {"items": items, "x": 100, "y": 200}
@@ -644,7 +644,7 @@ def test_local_variable_tracking_in_multiple_comprehension_conditions(self):
644644
@dataclass
645645
class Item:
646646
name: str
647-
children: list[int]
647+
children: List[int]
648648

649649
items = [Item("a", [-1, 1]), Item("b", [-2, 2])]
650650
context = {"items": items, "x": 100, "y": 200}

0 commit comments

Comments
 (0)