Skip to content

Commit e594aa6

Browse files
fix the pylint used-before-assignment error (#367)
* fix the pylint used-before-assignment error * fix integration tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f678d3f commit e594aa6

File tree

4 files changed

+107
-120
lines changed

4 files changed

+107
-120
lines changed

plugins/filter/next_nth_usable.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,23 @@ def next_nth_usable(value, offset):
118118
Returns the next nth usable ip within a network described by value.
119119
"""
120120
try:
121+
v = None
121122
vtype = ipaddr(value, "type")
122123
if vtype == "address":
123124
v = ipaddr(value, "cidr")
124125
elif vtype == "network":
125126
v = ipaddr(value, "subnet")
126127

127-
v = netaddr.IPNetwork(v)
128+
if v is not None:
129+
v = netaddr.IPNetwork(v)
130+
else:
131+
return False
128132
except Exception:
129133
return False
134+
130135
if not isinstance(offset, int):
131136
raise AnsibleFilterError("Must pass in an integer")
137+
132138
if v.size > 1:
133139
first_usable, last_usable = _first_last(v)
134140
nth_ip = int(netaddr.IPAddress(int(v.ip) + offset))

plugins/filter/previous_nth_usable.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,18 @@ def previous_nth_usable(value, offset):
117117
Returns the previous nth usable ip within a network described by value.
118118
"""
119119
try:
120+
v = None
120121
vtype = ipaddr(value, "type")
121122
if vtype == "address":
122123
v = ipaddr(value, "cidr")
123124
elif vtype == "network":
124125
v = ipaddr(value, "subnet")
125126

126-
v = netaddr.IPNetwork(v)
127+
if v is not None:
128+
v = netaddr.IPNetwork(v)
129+
else:
130+
return False
131+
127132
except Exception:
128133
return False
129134

tests/integration/targets/utils_keep_keys/tasks/complex1.yaml

Lines changed: 59 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -4,126 +4,85 @@
44
file: complex.yaml
55

66
- name: Test keep_keys
7-
vars:
8-
result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2']) }}"
9-
target:
10-
- { p1: a, p2: a }
11-
- { p1: b, p2: b }
12-
- { p1: c, p2: c }
137
tags: keep_keys
148
block:
9+
- name: Setup data as facts for complex keep_keys integration test l1
10+
ansible.builtin.set_fact:
11+
l1:
12+
- { p1: a, p2: a, p3: a }
13+
- { p1: b, p2: b, p3: b }
14+
- { p1: c, p2: c, p3: c }
15+
1516
- name: Test keep_keys debug
1617
ansible.builtin.debug:
17-
var: result|to_yaml
18-
when: debug_test|d(false)|bool
18+
msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2']) }}"
19+
register: result
20+
1921
- name: Test keep_keys assert
2022
ansible.builtin.assert:
21-
that: result == target
23+
that: result['msg'] == keep['l1']
2224

23-
- name: Test map keep_keys
24-
vars:
25-
result: "{{ l2 | map('ansible.utils.keep_keys', target=['p1', 'p2']) | list }}"
26-
target:
27-
- - { p1: a, p2: a }
28-
- { p1: b, p2: b }
29-
- { p1: c, p2: c }
30-
- - { p1: a, p2: a }
31-
- { p1: b, p2: b }
32-
- { p1: c, p2: c }
33-
- - { p1: a, p2: a }
34-
- { p1: b, p2: b }
35-
- { p1: c, p2: c }
36-
tags: map_keep_keys
37-
block:
38-
- name: Test map keep_keys debug
25+
- name: Debug l1 for starts_with 'p'
3926
ansible.builtin.debug:
40-
var: result|to_yaml
41-
when: debug_test|d(false)|bool
42-
- name: Test map keep_keys assert
43-
ansible.builtin.assert:
44-
that: result == target
27+
msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='starts_with') }}"
28+
register: result_l1_starts_with_p
4529

46-
- name: Test keep_keys starts_with
47-
vars:
48-
result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='starts_with') }}"
49-
target:
50-
- { p1: a, p2: a }
51-
- { p1: b, p2: b }
52-
- { p1: c, p2: c }
53-
tags: keep_keys_starts_with_2
54-
block:
55-
- name: Test keep_keys starts_with debug
56-
ansible.builtin.debug:
57-
var: result|to_yaml
58-
when: debug_test|d(false)|bool
59-
- name: Test keep_keys starts_with assert
30+
- name: Assert result dicts l1 for starts_with 'p'
6031
ansible.builtin.assert:
61-
that: result == target
32+
that:
33+
- result_l1_starts_with_p['msg'] == keep['l1_starts_with_p']
6234

63-
- name: Test keep_keys starts_with 'p'
64-
vars:
65-
result: "{{ l1 | ansible.utils.keep_keys(target=['p'], matching_parameter='starts_with') }}"
66-
target:
67-
- { p1: a, p2: a, p3: a }
68-
- { p1: b, p2: b, p3: b }
69-
- { p1: c, p2: c, p3: c }
70-
tags: keep_keys_starts_with_1
71-
block:
72-
- name: Test keep_keys starts_with 'p' debug
35+
- name: Debug l1 for ends_with
7336
ansible.builtin.debug:
74-
var: result|to_yaml
75-
when: debug_test|d(false)|bool
76-
- name: Test keep_keys starts_with 'p' assert
37+
msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='ends_with') }}"
38+
register: result_l1_ends_with
39+
40+
- name: Assert result dicts l1 for ends_with
7741
ansible.builtin.assert:
78-
that: result == target
42+
that:
43+
- result_l1_ends_with['msg'] == keep['l1_ends_with']
7944

80-
- name: Test keep_keys ends_with
81-
vars:
82-
result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='ends_with') }}"
83-
target:
84-
- { p1: a, p2: a }
85-
- { p1: b, p2: b }
86-
- { p1: c, p2: c }
87-
tags: keep_keys_ends_with_2
88-
block:
89-
- name: Test keep_keys end_with debug
45+
- name: Debug l1 for ends_with '2'
9046
ansible.builtin.debug:
91-
var: result|to_yaml
92-
when: debug_test|d(false)|bool
93-
- name: Test keep_keys end_with assert
47+
msg: "{{ l1 | ansible.utils.keep_keys(target=['2'], matching_parameter='ends_with') }}"
48+
register: result_l1_ends_with_2
49+
50+
- name: Assert result dicts l1 for ends_with '2'
9451
ansible.builtin.assert:
95-
that: result == target
52+
that:
53+
- result_l1_ends_with_2['msg'] == keep['l1_ends_with_2']
9654

97-
- name: Test keep_keys ends_with '2'
98-
vars:
99-
result: "{{ l1 | ansible.utils.keep_keys(target=['2'], matching_parameter='ends_with') }}"
100-
target:
101-
- { p2: a }
102-
- { p2: b }
103-
- { p2: c }
104-
tags: keep_keys_ends_with_1
105-
block:
106-
- name: Test keep_keys end_with '2' debug
55+
- name: Test keep_keys regex
10756
ansible.builtin.debug:
108-
var: result|to_yaml
109-
when: debug_test|d(false)|bool
110-
- name: Test keep_keys end_with '2' assert
57+
msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='regex') }}"
58+
register: result_l1_regex
59+
60+
- name: Assert result dicts l1 for regex
11161
ansible.builtin.assert:
112-
that: result == target
62+
that:
63+
- result_l1_regex['msg'] == keep['l1_regex']
11364

114-
- name: Test keep_keys regex
115-
vars:
116-
result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='regex') }}"
117-
target:
118-
- { p1: a, p2: a }
119-
- { p1: b, p2: b }
120-
- { p1: c, p2: c }
121-
tags: keep_keys_regex
65+
- name: Test map keep_keys
66+
tags: map_keep_keys
12267
block:
123-
- name: Test keep_keys regex debug
68+
- name: Setup data as facts for complex keep integration test l2
69+
ansible.builtin.set_fact:
70+
l2:
71+
- - { p1: a, p2: a, p3: a }
72+
- { p1: b, p2: b, p3: b }
73+
- { p1: c, p2: c, p3: c }
74+
- - { p1: a, p2: a, p3: a }
75+
- { p1: b, p2: b, p3: b }
76+
- { p1: c, p2: c, p3: c }
77+
- - { p1: a, p2: a, p3: a }
78+
- { p1: b, p2: b, p3: b }
79+
- { p1: c, p2: c, p3: c }
80+
81+
- name: Test map keep_keys debug
12482
ansible.builtin.debug:
125-
var: result|to_yaml
126-
when: debug_test|d(false)|bool
127-
- name: Test keep_keys regex assert
83+
msg: "{{ l2 | map('ansible.utils.keep_keys', target=['p1', 'p2']) | list }}"
84+
register: result
85+
86+
- name: Test map keep_keys assert
12887
ansible.builtin.assert:
129-
that: result == target
88+
that: result['msg'] == keep['l2']
Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
---
2-
l1:
3-
- { p1: a, p2: a, p3: a }
4-
- { p1: b, p2: b, p3: b }
5-
- { p1: c, p2: c, p3: c }
6-
l2:
7-
- - { p1: a, p2: a, p3: a }
8-
- { p1: b, p2: b, p3: b }
9-
- { p1: c, p2: c, p3: c }
10-
- - { p1: a, p2: a, p3: a }
11-
- { p1: b, p2: b, p3: b }
12-
- { p1: c, p2: c, p3: c }
13-
- - { p1: a, p2: a, p3: a }
14-
- { p1: b, p2: b, p3: b }
15-
- { p1: c, p2: c, p3: c }
16-
l3:
17-
- { p1_key_o1: a, p2_key_o2: a, p3_key_o3: a }
18-
- { p1_key_o4: b, p2_key_o5: b, p3_key_o6: b }
19-
- { p1_key_o7: c, p2_key_o8: c, p3_key_o9: c }
2+
keep:
3+
l1:
4+
- { p1: a, p2: a }
5+
- { p1: b, p2: b }
6+
- { p1: c, p2: c }
7+
l2:
8+
- - { p1: a, p2: a }
9+
- { p1: b, p2: b }
10+
- { p1: c, p2: c }
11+
- - { p1: a, p2: a }
12+
- { p1: b, p2: b }
13+
- { p1: c, p2: c }
14+
- - { p1: a, p2: a }
15+
- { p1: b, p2: b }
16+
- { p1: c, p2: c }
17+
l3:
18+
- { p1_key_o1: a, p2_key_o2: a, p3_key_o3: a }
19+
- { p1_key_o4: b, p2_key_o5: b, p3_key_o6: b }
20+
- { p1_key_o7: c, p2_key_o8: c, p3_key_o9: c }
21+
l1_starts_with_p:
22+
- { p1: a, p2: a }
23+
- { p1: b, p2: b }
24+
- { p1: c, p2: c }
25+
l1_ends_with:
26+
- { p1: a, p2: a }
27+
- { p1: b, p2: b }
28+
- { p1: c, p2: c }
29+
l1_ends_with_2:
30+
- { p2: a }
31+
- { p2: b }
32+
- { p2: c }
33+
l1_regex:
34+
- { p1: a, p2: a }
35+
- { p1: b, p2: b }
36+
- { p1: c, p2: c }

0 commit comments

Comments
 (0)