Skip to content

Commit 1d1d655

Browse files
committed
Fix ruff notes
1 parent 49e5ffb commit 1d1d655

16 files changed

Lines changed: 94 additions & 105 deletions

File tree

fhirpathpy/engine/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import json
22
import numbers
3-
import fhirpathpy.engine.util as util
4-
from fhirpathpy.engine.nodes import TypeInfo
3+
4+
from fhirpathpy.engine import util
55
from fhirpathpy.engine.evaluators import evaluators
66
from fhirpathpy.engine.invocations import (
77
invocation_registry as base_invocation_registry,
88
)
9+
from fhirpathpy.engine.nodes import TypeInfo
910

1011

1112
def check_integer_param(val):

fhirpathpy/engine/evaluators/__init__.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1+
import json
2+
import re
13
from collections import abc
24
from decimal import Decimal
35
from functools import reduce
46

5-
import re
6-
import json
7-
import fhirpathpy.engine as engine
8-
import fhirpathpy.engine.util as util
9-
import fhirpathpy.engine.nodes as nodes
7+
from fhirpathpy import engine
8+
from fhirpathpy.engine import nodes, util
109

1110

1211
def boolean_literal(ctx, parentData, node):
@@ -71,7 +70,7 @@ def alias_op_expression(mapFn):
7170
def func(ctx, parentData, node):
7271
op = node["terminalNodeText"][0]
7372

74-
if not op in mapFn:
73+
if op not in mapFn:
7574
raise Exception("Do not know how to alias " + op + " by " + json.dumps(mapFn))
7675

7776
alias = mapFn[op]
@@ -204,9 +203,8 @@ def func(acc, res):
204203
toAdd_ = res.data.get(f"_{key}")
205204
if key == "extension":
206205
childPath = "Extension"
207-
else:
208-
if key == "length":
209-
toAdd = len(res.data)
206+
elif key == "length":
207+
toAdd = len(res.data)
210208

211209
childPath = (
212210
model["path2Type"].get(childPath, childPath)

fhirpathpy/engine/invocations/__init__.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
from decimal import Decimal
22

3-
import fhirpathpy.engine.invocations.collections as collections
4-
import fhirpathpy.engine.invocations.existence as existence
5-
import fhirpathpy.engine.invocations.filtering as filtering
6-
import fhirpathpy.engine.invocations.subsetting as subsetting
7-
import fhirpathpy.engine.invocations.strings as strings
8-
import fhirpathpy.engine.invocations.navigation as navigation
9-
import fhirpathpy.engine.invocations.combining as combining
10-
import fhirpathpy.engine.invocations.math as math
11-
import fhirpathpy.engine.invocations.misc as misc
12-
import fhirpathpy.engine.invocations.equality as equality
13-
import fhirpathpy.engine.invocations.logic as logic
14-
import fhirpathpy.engine.invocations.datetime as datetime
15-
import fhirpathpy.engine.invocations.types as types
16-
import fhirpathpy.engine.invocations.aggregate as aggregate
3+
from fhirpathpy.engine.invocations import (
4+
aggregate,
5+
collections,
6+
combining,
7+
datetime,
8+
equality,
9+
existence,
10+
filtering,
11+
logic,
12+
math,
13+
misc,
14+
navigation,
15+
strings,
16+
subsetting,
17+
types,
18+
)
1719
from fhirpathpy.engine.nodes import FP_DateTime, FP_Quantity, FP_Time
1820

1921
invocation_registry = {

fhirpathpy/engine/invocations/combining.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fhirpathpy.engine.invocations.existence as existence
1+
from fhirpathpy.engine.invocations import existence
22

33
"""
44
This file holds code to hande the FHIRPath Combining functions

fhirpathpy/engine/invocations/equality.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import json
12
from collections import abc
23
from decimal import Decimal
3-
import json
4-
import fhirpathpy.engine.util as util
5-
import fhirpathpy.engine.nodes as nodes
4+
5+
from fhirpathpy.engine import nodes, util
66

77
"""
88
This file holds code to hande the FHIRPath Math functions.
@@ -81,7 +81,7 @@ def equivalence(ctx, x, y):
8181
if isinstance(x_val, nodes.FP_Quantity) and isinstance(y_val, nodes.FP_Quantity):
8282
return x_val.deep_equal(y_val)
8383

84-
if isinstance(a, (abc.Mapping, list)) and isinstance(b, (abc.Mapping, list)):
84+
if isinstance(a, abc.Mapping | list) and isinstance(b, abc.Mapping | list):
8585

8686
def deep_equal(a, b):
8787
if isinstance(a, abc.Mapping) and isinstance(b, abc.Mapping):
@@ -94,7 +94,7 @@ def deep_equal(a, b):
9494
)
9595
elif isinstance(a, str) and isinstance(b, str):
9696
return normalize_string(a) == normalize_string(b)
97-
elif isinstance(a, (int, float)) and isinstance(b, (int, float)):
97+
elif isinstance(a, int | float) and isinstance(b, int | float):
9898
return abs(a - b) < 0.5
9999
else:
100100
return a == b
@@ -190,12 +190,11 @@ def typecheck(a, b):
190190
if lClass != rClass and not areNumbers:
191191
d = None
192192

193-
# TODO refactor
194-
if lClass == str and (rClass == nodes.FP_DateTime or rClass == nodes.FP_Time):
193+
if lClass is str and (rClass in (nodes.FP_DateTime, nodes.FP_Time)):
195194
d = nodes.FP_DateTime(a) or nodes.FP_Time(a)
196195
if d is not None:
197196
rtn = [d, b]
198-
elif rClass == str and (lClass == nodes.FP_DateTime or lClass == nodes.FP_Time):
197+
elif rClass is str and (lClass in (nodes.FP_DateTime, nodes.FP_Time)):
199198
d = nodes.FP_DateTime(b) or nodes.FP_Time(b)
200199
if d is not None:
201200
rtn = [a, d]

fhirpathpy/engine/invocations/existence.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
from decimal import Decimal
2-
from fhirpathpy.engine.invocations import misc
3-
from fhirpathpy.engine.invocations.misc import to_boolean
4-
import fhirpathpy.engine.util as util
5-
import fhirpathpy.engine.nodes as nodes
6-
import fhirpathpy.engine.invocations.filtering as filtering
72

3+
from fhirpathpy.engine import nodes, util
4+
from fhirpathpy.engine.invocations import filtering, misc
85

96
"""
107
This file holds code to hande the FHIRPath Existence functions
@@ -54,7 +51,7 @@ def all_macro(ctx, colls, expr):
5451

5552
def extract_boolean_value(data):
5653
value = util.get_data(data)
57-
if type(value) != bool:
54+
if type(value) is not bool:
5855
raise Exception("Found type '" + type(data) + "' but was expecting bool")
5956
return value
6057

fhirpathpy/engine/invocations/filtering.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from collections import abc
2-
from decimal import Decimal
3-
import numbers
4-
import fhirpathpy.engine.util as util
5-
import fhirpathpy.engine.nodes as nodes
2+
3+
from fhirpathpy.engine import nodes, util
64

75
# Contains the FHIRPath Filtering and Projection functions.
86
# (Section 5.2 of the FHIRPath 1.0.0 specification).
@@ -54,18 +52,18 @@ def repeat_macro(ctx, data, expr):
5452
res = []
5553
items = data
5654

57-
next = None
55+
next_item = None
5856
lres = None
5957

6058
uniq = set()
6159

6260
while len(items) != 0:
63-
next = items[0]
61+
next_item = items[0]
6462
items = items[1:]
65-
lres = [l for l in expr(next) if l not in uniq]
63+
lres = [elem for elem in expr(next_item) if elem not in uniq]
6664
if len(lres) > 0:
67-
for l in lres:
68-
uniq.add(l)
65+
for elem in lres:
66+
uniq.add(elem)
6967
res = res + lres
7068
items = items + lres
7169

fhirpathpy/engine/invocations/logic.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
def or_op(ctx, a, b):
22
if isinstance(b, list):
3-
if a == True:
3+
if a is True:
44
return True
5-
if a == False:
5+
if a is False:
66
return []
77
if isinstance(a, list):
88
return []
99
if isinstance(a, list):
10-
if b == True:
10+
if b is True:
1111
return True
1212
return []
1313

@@ -16,15 +16,15 @@ def or_op(ctx, a, b):
1616

1717
def and_op(ctx, a, b):
1818
if isinstance(b, list):
19-
if a == True:
19+
if a is True:
2020
return []
21-
if a == False:
21+
if a is False:
2222
return False
2323
if isinstance(a, list):
2424
return []
2525

2626
if isinstance(a, list):
27-
if b == True:
27+
if b is True:
2828
return []
2929
return False
3030

@@ -42,19 +42,19 @@ def xor_op(ctx, a, b):
4242

4343
def implies_op(ctx, a, b):
4444
if isinstance(b, list):
45-
if a == True:
45+
if a is True:
4646
return []
47-
if a == False:
47+
if a is False:
4848
return True
4949
if isinstance(a, list):
5050
return []
5151

5252
if isinstance(a, list):
53-
if b == True:
53+
if b is True:
5454
return True
5555
return []
5656

57-
if a == False:
57+
if a is False:
5858
return True
5959

6060
return a and b

fhirpathpy/engine/invocations/math.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from decimal import Decimal
2+
3+
from fhirpathpy.engine import nodes, util
24
from fhirpathpy.engine.invocations.equality import remove_duplicate_extension
3-
import fhirpathpy.engine.util as util
4-
import fhirpathpy.engine.nodes as nodes
55

66
"""
77
Adds the math functions to the given FHIRPath engine.

fhirpathpy/engine/invocations/misc.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import re
22
from decimal import Decimal
33

4-
import fhirpathpy.engine.util as util
5-
import fhirpathpy.engine.nodes as nodes
4+
from fhirpathpy.engine import nodes, util
65

76
# This file holds code to hande the FHIRPath Existence functions (5.1 in the
87
# specification).
@@ -36,10 +35,10 @@ def to_integer(ctx, coll):
3635

3736
value = util.get_data(coll[0])
3837

39-
if value == False:
38+
if value is False:
4039
return 0
4140

42-
if value == True:
41+
if value is True:
4342
return 1
4443

4544
if util.is_number(value):
@@ -72,7 +71,7 @@ def to_quantity(ctx, coll, to_unit=None):
7271
v = util.val_data_converted(coll[0])
7372
quantity_regex_res = None
7473

75-
if isinstance(v, (int, Decimal)):
74+
if isinstance(v, int | Decimal):
7675
result = nodes.FP_Quantity(v, "'1'")
7776
elif isinstance(v, nodes.FP_Quantity):
7877
result = v
@@ -211,10 +210,10 @@ def to_boolean(ctx, coll):
211210

212211
if var_type == "bool":
213212
return val
214-
elif var_type == "int" or var_type == "float":
215-
if val == 1 or val == 1.0:
213+
elif var_type in ("int", "float"):
214+
if val in (1, 1.0):
216215
return True
217-
elif val == 0 or val == 0.0:
216+
elif val in (0, 0.0):
218217
return False
219218
elif var_type == "str":
220219
lower_case_var = val.lower()
@@ -248,17 +247,13 @@ def string_singleton(coll):
248247

249248
def singleton(coll, type):
250249
if len(coll) > 1:
251-
raise Exception(
252-
"Unexpected collection {coll}; expected singleton of type {type}".format(
253-
coll=coll, type=type
254-
)
255-
)
250+
raise Exception(f"Unexpected collection {coll}; expected singleton of type {type}")
256251
elif len(coll) == 0:
257252
return []
258253
to_singleton = singleton_eval_by_type[type]
259254
if to_singleton:
260255
val = to_singleton(coll)
261256
if val is not None:
262257
return val
263-
raise Exception("Expected {type}, but got: {coll}".format(type=type.lower(), coll=coll))
264-
raise Exception("Not supported type {}".format(type))
258+
raise Exception(f"Expected {type.lower()}, but got: {coll}")
259+
raise Exception(f"Not supported type {type}")

0 commit comments

Comments
 (0)