Skip to content

Commit

Permalink
Don't shadow builtins (#1498)
Browse files Browse the repository at this point in the history
* Don't shadow builtins

* Replace open

* Add missing _

* Fix missed id

---------

Co-authored-by: Shatakshi Mishra <shatakshimishra01@gmail.com>
Co-authored-by: Sorin Sbarnea <sorin.sbarnea@gmail.com>
  • Loading branch information
3 people authored May 3, 2023
1 parent 6e98d5c commit 038b3e1
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 52 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ line-length = 100
builtins = ["__"]
select = ["ALL"]
ignore = [
'A003', # Class attribute `repr` is shadowing a python builtin
'ANN001', # Missing type annotation for function argument `output`
'ANN002', # Missing type annotation for `*args`
'ANN003', # Missing type annotation for `**kwargs`
Expand Down
4 changes: 2 additions & 2 deletions src/ansible_navigator/actions/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def _build_collection_content_menu(self):
:returns: The plugin menu definition
"""
self._collection_cache.open()
self._collection_cache.open_()
selected_collection = self._collections[self.steps.current.index]
collection_name = f"__{selected_collection['known_as']}"
collection_contents = []
Expand Down Expand Up @@ -655,7 +655,7 @@ def _parse_collection_info_stdout(self) -> dict:
roles_exclude_keys = ["readme"]
self._collection_cache = cast(KeyValueStore, self._collection_cache)

self._collection_cache.open()
self._collection_cache.open_()
for collection in self._collections:
plugins_details = self._get_collection_plugins_details(collection)

Expand Down
7 changes: 0 additions & 7 deletions src/ansible_navigator/content_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ class SerializationFormat(Enum):
YAML = "YAML"
JSON = "JSON"

def repr(self) -> str:
"""Return a string representation.
:return: A string
"""
return self.value


@dataclass
class ContentBase(Generic[T]):
Expand Down
18 changes: 9 additions & 9 deletions src/ansible_navigator/data/image_introspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Command(SimpleNamespace):
"""Abstraction for a details about a shell command."""

id: str
id_: str
command: str
parse: Callable
stdout: str = ""
Expand Down Expand Up @@ -209,7 +209,7 @@ def commands(self):
command = "ansible-galaxy collection list"
return [
Command(
id="ansible_collections",
id_="ansible_collections",
command=command,
parse=self.parse,
),
Expand Down Expand Up @@ -241,7 +241,7 @@ def commands(self) -> list[Command]:
:returns: The defined command
"""
return [Command(id="ansible_version", command="ansible --version", parse=self.parse)]
return [Command(id_="ansible_version", command="ansible --version", parse=self.parse)]

@staticmethod
def parse(command: Command) -> None:
Expand All @@ -262,7 +262,7 @@ def commands(self) -> list[Command]:
:returns: The defined command
"""
return [Command(id="os_release", command="cat /etc/os-release", parse=self.parse)]
return [Command(id_="os_release", command="cat /etc/os-release", parse=self.parse)]

def parse(self, command) -> None:
"""Parse the output of the cat command.
Expand All @@ -282,12 +282,12 @@ def commands(self) -> list[Command]:
:returns: The defined command
"""
pre = Command(id="pip_freeze", command="python3 -m pip freeze", parse=self.parse_freeze)
pre = Command(id_="pip_freeze", command="python3 -m pip freeze", parse=self.parse_freeze)
run_command(pre)
pre.parse(pre)
pkgs = " ".join(pkg for pkg in pre.details[0])
return [
Command(id="python_packages", command=f"python3 -m pip show {pkgs}", parse=self.parse),
Command(id_="python_packages", command=f"python3 -m pip show {pkgs}", parse=self.parse),
]

def parse(self, command):
Expand Down Expand Up @@ -323,7 +323,7 @@ def commands(self) -> list[Command]:
:returns: The defined command
"""
return [Command(id="redhat_release", command="cat /etc/redhat-release", parse=self.parse)]
return [Command(id_="redhat_release", command="cat /etc/redhat-release", parse=self.parse)]

@staticmethod
def parse(command):
Expand All @@ -344,7 +344,7 @@ def commands(self) -> list[Command]:
:returns: The defined command
"""
return [Command(id="system_packages", command="rpm -qai", parse=self.parse)]
return [Command(id_="system_packages", command="rpm -qai", parse=self.parse)]

def parse(self, command):
"""Parse the output of the rpm command.
Expand Down Expand Up @@ -412,7 +412,7 @@ def main():
if key not in ["details", "errors"]:
result_as_dict[f"__{key}"] = result_as_dict[key]
result_as_dict.pop(key)
response[result_as_dict["__id"]] = result_as_dict
response[result_as_dict["__id_"]] = result_as_dict
except Exception as exc:
response["errors"].append(str(exc))
print(json.dumps(response))
Expand Down
2 changes: 1 addition & 1 deletion src/ansible_navigator/image_manager/introspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class Command(SimpleNamespace):
"""Abstraction for a details about a shell command."""

id: str
id_: str
command: str
parse: Callable
stdout: str = ""
Expand Down
12 changes: 6 additions & 6 deletions src/ansible_navigator/ui_framework/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class Form:
"""Simple abstraction to hold the fields of the form and a convenience method to present it."""

type: FormType
type_: FormType
cancelled: bool = False
fields: list = field(default_factory=list)
submitted: bool = False
Expand All @@ -44,7 +44,7 @@ def present(self, screen, ui_config):
:param screen: A curses window
:param ui_config: The current user interface configuration
"""
if self.type is FormType.FORM:
if self.type_ is FormType.FORM:
self.fields.append(
FieldButton(
name="submit",
Expand All @@ -54,7 +54,7 @@ def present(self, screen, ui_config):
),
)
self.fields.append(FieldButton(name="cancel", text="Cancel", color=9))
elif self.type is FormType.NOTIFICATION:
elif self.type_ is FormType.NOTIFICATION:
self.fields.append(
FieldButton(
name="submit",
Expand All @@ -63,7 +63,7 @@ def present(self, screen, ui_config):
color=10,
),
)
elif self.type is FormType.WORKING:
elif self.type_ is FormType.WORKING:
pass

FormPresenter(form=self, screen=screen, ui_config=ui_config).present()
Expand Down Expand Up @@ -142,9 +142,9 @@ def _dimensions(self):
widths.append(max(len(msg) for msg in form_field.messages))
widths.append(len(form_field.validator(hint=True)) + self._input_start)

if self._form.type is FormType.FORM:
if self._form.type_ is FormType.FORM:
self._form_width = max(widths) + BUTTON_SPACE
elif self._form.type in (FormType.NOTIFICATION, FormType.WORKING):
elif self._form.type_ in (FormType.NOTIFICATION, FormType.WORKING):
self._form_width = max(widths)

height = 2 # title + horizontal line
Expand Down
6 changes: 3 additions & 3 deletions src/ansible_navigator/ui_framework/form_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def dict_to_form(form_data: dict) -> Form:
:returns: Object containing fields from form
"""
if form_data.get("type") == "notification":
form = Form(type=FormType.NOTIFICATION)
form = Form(type_=FormType.NOTIFICATION)
elif form_data.get("type") == "working":
form = Form(type=FormType.WORKING)
form = Form(type_=FormType.WORKING)
else:
form = Form(type=FormType.FORM)
form = Form(type_=FormType.FORM)

form._dict = form_data # pylint: disable=protected-access
form.title = form_data["title"]
Expand Down
2 changes: 1 addition & 1 deletion src/ansible_navigator/utils/key_value_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def close(self) -> None:
self.conn.commit()
self.conn.close()

def open(self) -> sqlite3.Connection:
def open_(self) -> sqlite3.Connection:
"""Establish the connection to the database.
:returns: A connection to the database
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Command(NamedTuple):
execution_environment: bool
cmdline: str | None = None
command: str = "ansible-navigator"
format: str | None = None
format_: str | None = None
log_level: str = "debug"
mode: str = "interactive"
pass_environment_variables: list = []
Expand All @@ -44,8 +44,8 @@ def join(self) -> str:
args.extend(["--ee", str(self.execution_environment)])
args.extend(["--ll", self.log_level])
args.extend(["--mode", self.mode])
if self.format:
args.extend(["--format", self.format])
if self.format_:
args.extend(["--format", self.format_])
if self.pass_environment_variables:
for env_var in self.pass_environment_variables:
args.extend(["--penv", env_var])
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/actions/images/test_stdout_tmux.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ShellCommand(UiTestStep):
user_input=StdoutCommand(
cmdline="",
mode="stdout",
format="json",
format_="json",
execution_environment=True,
raw_append=" | grep creator",
).join(),
Expand Down
36 changes: 18 additions & 18 deletions tests/unit/utils/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,24 @@ def test_flatten_list(value: list, anticipated_result: list) -> None:
class HumanTimeTestData(NamedTuple):
"""Data for human time test."""

id: str
id_: str
value: int | float
expected: str


human_time_test_data = [
HumanTimeTestData(id="s", value=1, expected="1s"),
HumanTimeTestData(id="m-s", value=60 + 1, expected="1m1s"),
HumanTimeTestData(id="h-m-s", value=3600 + 60 + 1, expected="1h1m1s"),
HumanTimeTestData(id_="s", value=1, expected="1s"),
HumanTimeTestData(id_="m-s", value=60 + 1, expected="1m1s"),
HumanTimeTestData(id_="h-m-s", value=3600 + 60 + 1, expected="1h1m1s"),
HumanTimeTestData(
id="d-h-m-s",
id_="d-h-m-s",
value=86400 + 3600 + 60 + 1,
expected="1d1h1m1s",
),
]


@pytest.mark.parametrize("data", human_time_test_data, ids=lambda data: data.id)
@pytest.mark.parametrize("data", human_time_test_data, ids=lambda data: data.id_)
def test_human_time_integer(data: HumanTimeTestData) -> None:
"""Test for the functions.human_time function (integer passed).
Expand All @@ -169,7 +169,7 @@ def test_human_time_negative_integer(data: HumanTimeTestData) -> None:
assert result == f"-{data.expected}"


@pytest.mark.parametrize("data", human_time_test_data, ids=lambda data: data.id)
@pytest.mark.parametrize("data", human_time_test_data, ids=lambda data: data.id_)
def test_human_time_float(data: HumanTimeTestData) -> None:
"""Test for the functions.human_time function (float passed).
Expand All @@ -181,7 +181,7 @@ def test_human_time_float(data: HumanTimeTestData) -> None:
assert result == data.expected


@pytest.mark.parametrize("data", human_time_test_data, ids=lambda data: data.id)
@pytest.mark.parametrize("data", human_time_test_data, ids=lambda data: data.id_)
def test_human_time_negative_float(data: HumanTimeTestData) -> None:
"""Test for the functions.human_time function (negative float passed).
Expand All @@ -196,24 +196,24 @@ def test_human_time_negative_float(data: HumanTimeTestData) -> None:
class RoundHalfUpTestData(NamedTuple):
"""Data for round half up tests."""

id: str
id_: str
value: int | float
expected: int


round_half_up_test_data = [
RoundHalfUpTestData(id="integer", value=1, expected=1),
RoundHalfUpTestData(id="negative-integer", value=-1, expected=-1),
RoundHalfUpTestData(id="down-float", value=1.49999999, expected=1),
RoundHalfUpTestData(id="up-float", value=1.50000001, expected=2),
RoundHalfUpTestData(id="negative-down-float", value=-1.49999999, expected=-1),
RoundHalfUpTestData(id="negative-up-float", value=-1.50000001, expected=-2),
RoundHalfUpTestData(id="half_even", value=2.5, expected=3),
RoundHalfUpTestData(id="half_even", value=3.5, expected=4),
RoundHalfUpTestData(id_="integer", value=1, expected=1),
RoundHalfUpTestData(id_="negative-integer", value=-1, expected=-1),
RoundHalfUpTestData(id_="down-float", value=1.49999999, expected=1),
RoundHalfUpTestData(id_="up-float", value=1.50000001, expected=2),
RoundHalfUpTestData(id_="negative-down-float", value=-1.49999999, expected=-1),
RoundHalfUpTestData(id_="negative-up-float", value=-1.50000001, expected=-2),
RoundHalfUpTestData(id_="half_even", value=2.5, expected=3),
RoundHalfUpTestData(id_="half_even", value=3.5, expected=4),
]


@pytest.mark.parametrize("data", round_half_up_test_data, ids=lambda data: data.id)
@pytest.mark.parametrize("data", round_half_up_test_data, ids=lambda data: data.id_)
def test_round_half_up(data: RoundHalfUpTestData) -> None:
"""Test for the functions.round_half_up function.
Expand Down

0 comments on commit 038b3e1

Please sign in to comment.