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>
pull/369/head
Ruchi Pakhle 2024-08-20 20:19:57 +05:30 committed by GitHub
parent f678d3f4f3
commit e594aa6090
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 116 additions and 129 deletions

View File

@ -118,17 +118,23 @@ def next_nth_usable(value, offset):
Returns the next nth usable ip within a network described by value.
"""
try:
v = None
vtype = ipaddr(value, "type")
if vtype == "address":
v = ipaddr(value, "cidr")
elif vtype == "network":
v = ipaddr(value, "subnet")
v = netaddr.IPNetwork(v)
if v is not None:
v = netaddr.IPNetwork(v)
else:
return False
except Exception:
return False
if not isinstance(offset, int):
raise AnsibleFilterError("Must pass in an integer")
if v.size > 1:
first_usable, last_usable = _first_last(v)
nth_ip = int(netaddr.IPAddress(int(v.ip) + offset))

View File

@ -117,13 +117,18 @@ def previous_nth_usable(value, offset):
Returns the previous nth usable ip within a network described by value.
"""
try:
v = None
vtype = ipaddr(value, "type")
if vtype == "address":
v = ipaddr(value, "cidr")
elif vtype == "network":
v = ipaddr(value, "subnet")
v = netaddr.IPNetwork(v)
if v is not None:
v = netaddr.IPNetwork(v)
else:
return False
except Exception:
return False

View File

@ -4,126 +4,85 @@
file: complex.yaml
- name: Test keep_keys
vars:
result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2']) }}"
target:
- { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
tags: keep_keys
block:
- name: Setup data as facts for complex keep_keys integration test l1
ansible.builtin.set_fact:
l1:
- { p1: a, p2: a, p3: a }
- { p1: b, p2: b, p3: b }
- { p1: c, p2: c, p3: c }
- name: Test keep_keys debug
ansible.builtin.debug:
var: result|to_yaml
when: debug_test|d(false)|bool
msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2']) }}"
register: result
- name: Test keep_keys assert
ansible.builtin.assert:
that: result == target
that: result['msg'] == keep['l1']
- name: Debug l1 for starts_with 'p'
ansible.builtin.debug:
msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='starts_with') }}"
register: result_l1_starts_with_p
- name: Assert result dicts l1 for starts_with 'p'
ansible.builtin.assert:
that:
- result_l1_starts_with_p['msg'] == keep['l1_starts_with_p']
- name: Debug l1 for ends_with
ansible.builtin.debug:
msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='ends_with') }}"
register: result_l1_ends_with
- name: Assert result dicts l1 for ends_with
ansible.builtin.assert:
that:
- result_l1_ends_with['msg'] == keep['l1_ends_with']
- name: Debug l1 for ends_with '2'
ansible.builtin.debug:
msg: "{{ l1 | ansible.utils.keep_keys(target=['2'], matching_parameter='ends_with') }}"
register: result_l1_ends_with_2
- name: Assert result dicts l1 for ends_with '2'
ansible.builtin.assert:
that:
- result_l1_ends_with_2['msg'] == keep['l1_ends_with_2']
- name: Test keep_keys regex
ansible.builtin.debug:
msg: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='regex') }}"
register: result_l1_regex
- name: Assert result dicts l1 for regex
ansible.builtin.assert:
that:
- result_l1_regex['msg'] == keep['l1_regex']
- name: Test map keep_keys
vars:
result: "{{ l2 | map('ansible.utils.keep_keys', target=['p1', 'p2']) | list }}"
target:
- - { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
- - { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
- - { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
tags: map_keep_keys
block:
- name: Setup data as facts for complex keep integration test l2
ansible.builtin.set_fact:
l2:
- - { p1: a, p2: a, p3: a }
- { p1: b, p2: b, p3: b }
- { p1: c, p2: c, p3: c }
- - { p1: a, p2: a, p3: a }
- { p1: b, p2: b, p3: b }
- { p1: c, p2: c, p3: c }
- - { p1: a, p2: a, p3: a }
- { p1: b, p2: b, p3: b }
- { p1: c, p2: c, p3: c }
- name: Test map keep_keys debug
ansible.builtin.debug:
var: result|to_yaml
when: debug_test|d(false)|bool
msg: "{{ l2 | map('ansible.utils.keep_keys', target=['p1', 'p2']) | list }}"
register: result
- name: Test map keep_keys assert
ansible.builtin.assert:
that: result == target
- name: Test keep_keys starts_with
vars:
result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='starts_with') }}"
target:
- { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
tags: keep_keys_starts_with_2
block:
- name: Test keep_keys starts_with debug
ansible.builtin.debug:
var: result|to_yaml
when: debug_test|d(false)|bool
- name: Test keep_keys starts_with assert
ansible.builtin.assert:
that: result == target
- name: Test keep_keys starts_with 'p'
vars:
result: "{{ l1 | ansible.utils.keep_keys(target=['p'], matching_parameter='starts_with') }}"
target:
- { p1: a, p2: a, p3: a }
- { p1: b, p2: b, p3: b }
- { p1: c, p2: c, p3: c }
tags: keep_keys_starts_with_1
block:
- name: Test keep_keys starts_with 'p' debug
ansible.builtin.debug:
var: result|to_yaml
when: debug_test|d(false)|bool
- name: Test keep_keys starts_with 'p' assert
ansible.builtin.assert:
that: result == target
- name: Test keep_keys ends_with
vars:
result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='ends_with') }}"
target:
- { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
tags: keep_keys_ends_with_2
block:
- name: Test keep_keys end_with debug
ansible.builtin.debug:
var: result|to_yaml
when: debug_test|d(false)|bool
- name: Test keep_keys end_with assert
ansible.builtin.assert:
that: result == target
- name: Test keep_keys ends_with '2'
vars:
result: "{{ l1 | ansible.utils.keep_keys(target=['2'], matching_parameter='ends_with') }}"
target:
- { p2: a }
- { p2: b }
- { p2: c }
tags: keep_keys_ends_with_1
block:
- name: Test keep_keys end_with '2' debug
ansible.builtin.debug:
var: result|to_yaml
when: debug_test|d(false)|bool
- name: Test keep_keys end_with '2' assert
ansible.builtin.assert:
that: result == target
- name: Test keep_keys regex
vars:
result: "{{ l1 | ansible.utils.keep_keys(target=['p1', 'p2'], matching_parameter='regex') }}"
target:
- { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
tags: keep_keys_regex
block:
- name: Test keep_keys regex debug
ansible.builtin.debug:
var: result|to_yaml
when: debug_test|d(false)|bool
- name: Test keep_keys regex assert
ansible.builtin.assert:
that: result == target
that: result['msg'] == keep['l2']

View File

@ -1,19 +1,36 @@
---
l1:
- { p1: a, p2: a, p3: a }
- { p1: b, p2: b, p3: b }
- { p1: c, p2: c, p3: c }
l2:
- - { p1: a, p2: a, p3: a }
- { p1: b, p2: b, p3: b }
- { p1: c, p2: c, p3: c }
- - { p1: a, p2: a, p3: a }
- { p1: b, p2: b, p3: b }
- { p1: c, p2: c, p3: c }
- - { p1: a, p2: a, p3: a }
- { p1: b, p2: b, p3: b }
- { p1: c, p2: c, p3: c }
l3:
- { p1_key_o1: a, p2_key_o2: a, p3_key_o3: a }
- { p1_key_o4: b, p2_key_o5: b, p3_key_o6: b }
- { p1_key_o7: c, p2_key_o8: c, p3_key_o9: c }
keep:
l1:
- { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
l2:
- - { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
- - { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
- - { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
l3:
- { p1_key_o1: a, p2_key_o2: a, p3_key_o3: a }
- { p1_key_o4: b, p2_key_o5: b, p3_key_o6: b }
- { p1_key_o7: c, p2_key_o8: c, p3_key_o9: c }
l1_starts_with_p:
- { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
l1_ends_with:
- { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }
l1_ends_with_2:
- { p2: a }
- { p2: b }
- { p2: c }
l1_regex:
- { p1: a, p2: a }
- { p1: b, p2: b }
- { p1: c, p2: c }