Skip to content

Commit 679c6c0

Browse files
authored
Merge pull request #130 from larsewi/fix-pyright-errors
Fixed pyright errors across modules
2 parents 9e7b76d + d6b6342 commit 679c6c0

8 files changed

Lines changed: 27 additions & 18 deletions

File tree

ci/linting.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pyright .
1010

1111
shopt -s globstar
1212
echo "Running black"
13-
black --check .
13+
black --check --diff .
1414

1515
echo "Running pyflakes"
1616
pyflakes .

examples/gpg/gpg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def evaluate_promise(self, promiser, attributes, metadata):
141141
f"Importing ascii key for user id '{user_id}' into gpg homedir '{promiser}'"
142142
)
143143
if self.gpg_import_ascii(promiser, key["ascii"]):
144-
if result != Result.NOTKEPT:
144+
if result != Result.NOT_KEPT:
145145
result = Result.REPAIRED
146146
else:
147147
self.log_error(f"Unable to import key for user id '{user_id}'")

libraries/python/cfengine_module_library.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import traceback
2222
from copy import copy
2323
from collections import OrderedDict
24+
from typing import Any
2425

2526
_LOG_LEVELS = {
2627
level: idx
@@ -91,6 +92,17 @@ def __init__(self, d):
9192
for key, value in d.items():
9293
setattr(self, key, value)
9394

95+
# Python only calls __getattr__ as a fallback when normal attribute
96+
# lookup (__getattribute__) has already failed, so attributes set via
97+
# setattr() in __init__ are never affected. The -> Any return type
98+
# tells pyright that dynamic attribute access is valid.
99+
def __getattr__(self, name) -> Any:
100+
raise AttributeError(
101+
"'{}' object has no attribute '{}'".format(
102+
self.__class__.__qualname__, name
103+
)
104+
)
105+
94106
def __repr__(self):
95107
return "{}({})".format(
96108
self.__class__.__qualname__,

promise-types/ansible/ansible_promise.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ def validate_promise(self, promiser: str, attributes: Dict, metadata: Dict):
9696
return
9797

9898
def evaluate_promise(
99-
self, safe_promiser: str, attributes: Dict, metadata: Dict
99+
self, promiser: str, attributes: Dict, metadata: Dict
100100
) -> Tuple[str, List[str]]:
101-
model = self.create_attribute_object(safe_promiser, attributes)
101+
model = self.create_attribute_object(promiser, attributes)
102102

103103
classes = []
104104
result = Result.KEPT
@@ -149,9 +149,7 @@ def evaluate_promise(
149149

150150
exit_code = pbex.run()
151151
if exit_code != 0:
152-
classes.append(
153-
"{safe_promiser}_failed".format(safe_promiser=safe_promiser)
154-
)
152+
classes.append("{safe_promiser}_failed".format(safe_promiser=promiser))
155153
result = Result.NOT_KEPT
156154
elif callback.changed:
157155
result = Result.REPAIRED

promise-types/iptables/iptables.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def evaluate_promise(self, promiser: str, attributes: Dict, metadata: Dict):
231231

232232
return result, classes
233233

234-
def evaluate_command_policy(self, executable, table, chain, target) -> Result:
234+
def evaluate_command_policy(self, executable, table, chain, target) -> str:
235235
policy_rules = self._iptables_policy_rules_of(executable, table, chain)
236236
assert len(policy_rules) == 1 and len(policy_rules[0].split()) >= 1
237237

promise-types/json/json_promise_type.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(self, **kwargs):
3939
self.types
4040
) # for now, the only valid attributes are the types.
4141

42-
def create_attribute_object(self, attributes):
42+
def create_attribute_object(self, promiser, attributes):
4343
data = {t: None for t in self.valid_attributes}
4444
for attr, val in attributes.items():
4545
data[attr] = val
@@ -73,7 +73,7 @@ def validate_promise(self, promiser, attributes, metadata):
7373
if colon and not field:
7474
raise ValidationError("Invalid syntax: field specified but empty")
7575

76-
model = self.create_attribute_object(attributes)
76+
model = self.create_attribute_object(promiser, attributes)
7777
if (
7878
model.object
7979
and isinstance(model.object, str)
@@ -113,7 +113,7 @@ def validate_promise(self, promiser, attributes, metadata):
113113
)
114114

115115
def evaluate_promise(self, promiser, attributes, metadata):
116-
model = self.create_attribute_object(attributes)
116+
model = self.create_attribute_object(promiser, attributes)
117117
filename, _, field = promiser.partition(":")
118118

119119
if os.path.exists(filename) and not os.path.isfile(filename):

promise-types/sshd/sshd_promise_type.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import subprocess
44
import tempfile
55

6-
from cfengine_module_library import PromiseModule, ValidationError, Result
7-
6+
from cfengine_module_library import PromiseModule, Result, ValidationError
87

98
BASE_CONFIG = "/etc/ssh/sshd_config"
109
DROP_IN_DIR = "/etc/ssh/sshd_config.d/"

promise-types/systemd/systemd.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ def prepare_promiser_and_attributes(self, promiser, attributes):
7979
return (safe_promiser, attributes)
8080

8181
def evaluate_promise(
82-
self, safe_promiser: str, attributes: Dict, metadata: Dict
82+
self, promiser: str, attributes: Dict, metadata: Dict
8383
) -> Tuple[str, List[str]]:
84-
model = self.create_attribute_object(safe_promiser, attributes)
84+
model = self.create_attribute_object(promiser, attributes)
8585
# get the status of the service
8686
try:
8787
output = self._exec_command(
@@ -106,13 +106,13 @@ def evaluate_promise(
106106
self.log_error(e.stderr.strip())
107107
return (
108108
Result.NOT_KEPT,
109-
["{safe_promiser}_show_failed".format(safe_promiser=safe_promiser)],
109+
["{safe_promiser}_show_failed".format(safe_promiser=promiser)],
110110
)
111111
# apply the changes
112112
if model.state == SystemdPromiseTypeStates.ABSENT.value:
113-
return self._service_absent(model, safe_promiser, service_status)
113+
return self._service_absent(model, promiser, service_status)
114114
else:
115-
return self._service_present(model, safe_promiser, service_status)
115+
return self._service_present(model, promiser, service_status)
116116

117117
def _service_absent(
118118
self, model: AttributeObject, safe_promiser: str, service_status: dict

0 commit comments

Comments
 (0)