Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/6025 implement top level handler abc #6246

Closed
wants to merge 58 commits into from

Conversation

Hugo-Inmanta
Copy link
Contributor

Description

Part of #6025

Self Check:

Strike through any lines that are not applicable (~~line~~) then check the box

  • Attached issue to pull request
  • Changelog entry
  • Type annotations are present
  • Code is clear and sufficiently documented
  • No (preventable) type errors (check using make mypy or make mypy-diff)
  • Sufficient test cases (reproduces the bug/tests the requested feature)
  • Correct, in line with design
  • End user documentation is included or an issue is created for end-user documentation (add ref to issue here: )
  • If this PR fixes a race condition in the test suite, also push the fix to the relevant stable branche(s) (see test-fixes for more info)

@@ -793,7 +823,7 @@ def call() -> typing.Awaitable[Result]:


@stable_api
class CRUDHandler(ResourceHandler):
class CRUDHandler(ResourceHandler, HandlerABC):
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't ResourceHandler just inherit from HandlerABC instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess you mean CRUDHandler ? This would mean we need to lift some functionality from ResourceHandler to HandlerABC ?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I mean ResourceHandler. If it inherits from HandlerABC, CRUDHandler doesn't need to. Now we have

class HandlerABC: pass
class ResourceHandler: pass
class CRUDHandler(ResourceHandler, HandlerABC): pass

and I'm proposing

class HandlerABC: pass
class ResourceHandler(HandlerABC): pass
class CRUDHandler(ResourceHandler): pass

Copy link
Contributor

@sanderr sanderr Jul 11, 2023

Choose a reason for hiding this comment

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

My first snippet is not right. Not sure if you changed it or I saw it wrong. Anyway, if ResourceHandler(HandlerABC), why would we explicitly inherit from it in CRUDHandler?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, 100% agree

@@ -55,7 +55,7 @@ class provider(object): # noqa: N801
"""
A decorator that registers a new handler.

:param resource_type: The type of the resource this handler provides an implementation for.
:param resource_type: The type of the resource this handler provides an implementation for. # TODO rephrase 'provides an implementation for'
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't forget about this TODO

@sanderr sanderr requested a review from bartv July 11, 2023 11:33
Copy link
Contributor

@bartv bartv left a comment

Choose a reason for hiding this comment

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

Not sure I can contribute here. I a missing missing the point on what the goal of this refactor is.

:param resource: The resource to query facts for.
"""

@abstractmethod
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this one or pre should be marked as abstractmethod because that requires concrete classes to override them. We want their default behavior to be a NOOP, don't we?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, that's the route I took on my PR implementing the DiscoveryHandler, I'll change it here as well

src/inmanta/agent/handler.py Outdated Show resolved Hide resolved
"""

def __init__(self, agent: "inmanta.agent.agent.AgentInstance", io: Optional["IOBase"] = None) -> None:
self._agent = agent
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we call super here instead of setting self._agent, self._ioloop and self._client directly?

requires: Dict[ResourceIdStr, ResourceState],
) -> None:
"""
Main entrypoint of the handler that will be called by the agent.
Copy link
Contributor

Choose a reason for hiding this comment

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

This docstring doesn't really describe the behavior of the method.

@stable_api
class ResourceHandler(HandlerABC):
"""
A baseclass for classes that handle resources. New handler are registered with the
Copy link
Contributor

Choose a reason for hiding this comment

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

The part about handler registration seems relevant for the top level ABC as well.

self,
ctx: HandlerContext,
resource: R,
requires: Dict[ResourceIdStr, ResourceState],
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be a good time to clean up this type. Either dict or abc.Mapping. Afaik we don't ever update it so Mapping would be best. @arnaudsjs do you know?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we ever update it.

:return: A client that is associated with the session of the agent that executes this handler.
"""
if self._client is None:
self._client = protocol.SessionClient("agent", self._agent.sessionid)
return self._client


@stable_api
class ResourceHandler(HandlerABC):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
class ResourceHandler(HandlerABC):
class ResourceHandler(HandlerABC[R]):

I think. Then update all type annotations of resources.Resource to R instead. Same for the CRUDHandler.

Hugo-Inmanta and others added 5 commits July 21, 2023 12:05
@arnaudsjs
Copy link
Contributor

Regarding the typing issue on chain_future: This seems to be a typing issue in Tornado. The chain_future is annotated to accept a asyncio.Future instance, but according to the implementation and the documentation of the method it accepts all types of Futures (asyncio.Future and concurrent.futures.Future). I created a ticket for this.

@arnaudsjs
Copy link
Contributor

@sanderr I still have the following type error left:

src/inmanta/resources.py:435: error: Incompatible return value type (got "Resource", expected "T")  [return-value]

I don't really see how that can be fixed. Resource.deserialize() is a class method. Its return type depends on what resource.get_class() returns and that can be any subclass of Resource.

@arnaudsjs arnaudsjs requested review from sanderr and removed request for sanderr August 16, 2023 12:36
@arnaudsjs arnaudsjs added the merge-tool-ready This ticket is ready to be merged in label Aug 17, 2023
@inmantaci
Copy link
Contributor

Processing this pull request

@inmantaci inmantaci removed the merge-tool-ready This ticket is ready to be merged in label Aug 17, 2023
@inmantaci
Copy link
Contributor

Pull request rejected by merge tool. This pull request has not been approved yet.

@arnaudsjs arnaudsjs requested review from sanderr and removed request for sanderr August 17, 2023 11:01
@arnaudsjs arnaudsjs dismissed sanderr’s stale review August 17, 2023 11:03

Sander is out of office for several says

@arnaudsjs arnaudsjs added the merge-tool-ready This ticket is ready to be merged in label Aug 17, 2023
@inmantaci
Copy link
Contributor

Processing this pull request

@inmantaci
Copy link
Contributor

Merged into branches master in 4ffeafa

@inmantaci inmantaci closed this Aug 17, 2023
inmantaci pushed a commit that referenced this pull request Aug 17, 2023
…rce handlers generic in their resource type. (Issue #6025, PR #6246)

# Description

Part of #6025

# Self Check:

Strike through any lines that are not applicable (`~~line~~`) then check the box

- [ ] Attached issue to pull request
- [ ] Changelog entry
- [ ] Type annotations are present
- [ ] Code is clear and sufficiently documented
- [ ] No (preventable) type errors (check using make mypy or make mypy-diff)
- [ ] Sufficient test cases (reproduces the bug/tests the requested feature)
- [ ] Correct, in line with design
- [ ] End user documentation is included or an issue is created for end-user documentation (add ref to issue here: )
- [ ] If this PR fixes a race condition in the test suite, also push the fix to the relevant stable branche(s) (see [test-fixes](https://internal.inmanta.com/development/core/tasks/build-master.html#test-fixes) for more info)
@inmantaci inmantaci deleted the issue/6025-implement-top-level-handler-ABC branch August 17, 2023 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge-tool-ready This ticket is ready to be merged in
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants