Repository navigation
Conversation
BREAKING CHANGE: External code calling api.facts.get_fact(...) could fail.
| positional_parameters = [ | ||
| name | ||
| for name, candidate in parameters.items() | ||
| if candidate.kind | ||
| in ( | ||
| inspect.Parameter.POSITIONAL_ONLY, | ||
| inspect.Parameter.POSITIONAL_OR_KEYWORD, | ||
| ) | ||
| ] | ||
|
|
||
| to_insert: list[tuple[int, str, object]] = [] | ||
| for name, obj in [ | ||
| ("state", state), | ||
| ("host", host), | ||
| ]: | ||
| if name in parameters: | ||
| if name in positional_parameters: | ||
| position = positional_parameters.index(name) | ||
| to_insert.append((position, name, obj)) | ||
| else: | ||
| call_kwargs[name] = obj |
There was a problem hiding this comment.
Starting a discussion here because I'd like feedback on this aspect.
This code, while already quite complex, does not handle properly Pyinfra fact global arguments (like _sudo). To avoid making this overly complex, one option would be to require all arguments to a "fact function" to be keyword arguments. Would this be too much of a limitation? Are there simpler solutions that I didn't think about?
Cleans up the tests that check and inventory Fact classes, such that they make use of the new FactCore base class and they do not need to rely on confusing naming conventions for facts. BREAKING CHANGE: Fact class `abstract` property now needs to be specified on each level of inheritance that is abstract. Even though the class property would be inherited, the test is such that the value defined on a parent is ignored. This is to make sure that `abstract = False` does NOT have to be specified in each and every concrete Fact class.
…tcutFact These represent better what those facts provide. API backwards compatibility is maintained by adding aliases that can be removed in a later release
58304fc to
4d7f947
Compare
wowi42
left a comment
There was a problem hiding this comment.
This is not safe to merge in v3 yet. It removes pyinfra.api.facts.get_fact and get_short_facts without compatibility wrappers or a deprecation path. Please keep those APIs delegating to the new implementation for v3. The other blockers are inline.
| bound_arguments = sig.bind(*call_args, **call_kwargs) | ||
| return fact_func(*bound_arguments.args, **bound_arguments.kwargs) | ||
|
|
||
| module_name = fact_func.__module__.replace("pyinfra.facts.", "") |
There was a problem hiding this comment.
The decorator copies only name and __doc__. The generated class keeps __module__ = "pyinfra.api.facts" and __name__ = "DecoratedFact", so docs generators filter it out and operation links point to a nonexistent page. Please preserve the wrapped function metadata and add docs-generation coverage with a real decorated fact.
There was a problem hiding this comment.
For this one, I'm not sure I fully understand what you mean by "docs generators filter it out". But I think that the fix expected is to copy __module__ and __name__ from the fact function into the wrapper class. I've done this in 760b57e
| def get_fact(self, name_or_cls: type[ShortFactBase[T]], *args, **kwargs) -> T: ... | ||
|
|
||
| def get_fact(self, name_or_cls, *args, **kwargs): | ||
| def get_fact(self, name_or_cls: type[FactCore[T]], *args, **kwargs): |
There was a problem hiding this comment.
Removing the -> T return annotation here makes Host.get_fact return Any to type checkers. Please restore the generic return type and add typing coverage for shell, shortcut, and decorated facts.
| from pyinfra.api.facts import FactCore, fact | ||
|
|
||
|
|
||
| class TestFactDecorator(unittest.TestCase): |
There was a problem hiding this comment.
Please add regression cases for a fact function returning None, raising an exception, and being called repeatedly without caching. These documented paths are not covered by the current tests.
Thanks for taking the time to review. |
This refactor is the beginning of work to simplify using Pyinfra to configure targets that are not easily configured with shell commands, or that are easier or safer to configure another way. Benefits include:
I have put in this PR a number of changes, which, if requested, could be split in separate PRs. I'm leaving them together for now as it would make sense if discussions relate to the whole direction. The high level changes included are:
get_factand the Facts API to extractFactCoreas the abstraction of any Fact@factdecorator to write facts with a simple functionAI usage disclosure