Skip to content

Fixed pyright errors across modules#130

Merged
olehermanse merged 8 commits intocfengine:masterfrom
larsewi:fix-pyright-errors
Apr 1, 2026
Merged

Fixed pyright errors across modules#130
olehermanse merged 8 commits intocfengine:masterfrom
larsewi:fix-pyright-errors

Conversation

@larsewi
Copy link
Copy Markdown
Contributor

@larsewi larsewi commented Apr 1, 2026

Summary

  • Added __getattr__ with -> Any return type to AttributeObject so pyright accepts dynamic attribute access
  • Fixed Result.NOTKEPT typo in gpg module (NOT_KEPT)
  • Fixed evaluate_promise parameter name mismatches in ansible and systemd modules to match base class signature
  • Fixed create_attribute_object signature in json module to match base class
  • Fixed evaluate_command_policy return type annotation in iptables module (Result -> str)

🤖 Generated with Claude Code

@larsewi larsewi force-pushed the fix-pyright-errors branch from ab5edfb to d66e22f Compare April 1, 2026 11:07
@larsewi larsewi requested a review from olehermanse April 1, 2026 11:11
Copy link
Copy Markdown
Member

@olehermanse olehermanse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, please improve the first commit a bit.

larsewi added 8 commits April 1, 2026 13:22
AttributeObject uses dynamic setattr() in __init__, which pyright
cannot analyze statically. Adding __getattr__ with an Any return
type tells pyright that dynamic attribute access is valid, while
preserving correct runtime behavior (AttributeError for truly
missing attributes).

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Renamed safe_promiser to promiser to match the base class
PromiseModule.evaluate_promise() signature. The parameter already
receives the safe promiser via prepare_promiser_and_attributes().

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Renamed safe_promiser to promiser to match the base class
PromiseModule.evaluate_promise() signature. The parameter already
receives the safe promiser via prepare_promiser_and_attributes().

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Added missing promiser parameter to match the base class
PromiseModule.create_attribute_object() signature.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Changed evaluate_command_policy return type from Result to str,
since Result.KEPT and Result.REPAIRED are string class attributes,
not Result instances.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
This prints the unified diff of formatting changes on failure,
making it easier to manually fix issues when black versions
disagree.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
Due to conflicting versions of black.

Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>
@larsewi
Copy link
Copy Markdown
Contributor Author

larsewi commented Apr 1, 2026

"getattr is only invoked if the attribute wasn't found the usual ways"

from cfengine_module_library import AttributeObject
a = AttributeObject({"x": 1, "y": 2})
print("Accessing x:", a.x)
print("Accessing y:", a.y)
print("Accessing z:")
a.z
$ PYTHONPATH=libraries/python python3 test_attribute_object.py
Accessing x: 1
Accessing y: 2
Accessing z:
Traceback (most recent call last):
  File "/home/larsewi/ntech/modules/test_attribute_object.py", line 8, in <module>
    a.z
  File "/home/larsewi/ntech/modules/libraries/python/cfengine_module_library.py", line 100, in __getattr__
    raise AttributeError(
AttributeError: 'AttributeObject' object has no attribute 'z'

@olehermanse
Copy link
Copy Markdown
Member

@larsewi please put it in a comment - the code looks weird as is.

@larsewi larsewi force-pushed the fix-pyright-errors branch from fb19d18 to d6b6342 Compare April 1, 2026 11:30
@larsewi larsewi requested a review from olehermanse April 1, 2026 11:31
@olehermanse olehermanse merged commit 679c6c0 into cfengine:master Apr 1, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants