Skip to content

Conversation

@shsms
Copy link
Contributor

@shsms shsms commented Jan 12, 2024

Often during the early stages of development, people might just try to run pytest, and not pylint every time.

This makes it hard to identify when they forget to call super().__init__() when deriving from BackgroundService.

This PR improves error handling in BackgroundService to provide a more meaningful error and a solution to an otherwise hard to debug issue.

shsms added 2 commits January 12, 2024 18:42
Often during the early stages of development, people might just try to
run pytest, and not pylint every time.

This makes it hard to identify when they forget to call
`super().__init__()` when deriving from `BackgroundService`.

This commit improves error handling in `BackgroundService` to provide
a more meaningful error and a solution to an otherwise hard to debug
issue.

Signed-off-by: Sahas Subramanian <[email protected]>
Signed-off-by: Sahas Subramanian <[email protected]>
@github-actions github-actions bot added the part:actor Affects an actor ot the actors utilities (decorator, etc.) label Jan 12, 2024
@shsms shsms self-assigned this Jan 12, 2024
@github-actions github-actions bot added the part:docs Affects the documentation label Jan 12, 2024
@shsms
Copy link
Contributor Author

shsms commented Jan 12, 2024

Before
Traceback (most recent call last):
  File "/home/sahas/.local/lib/python3.11/site-packages/frequenz/sdk/actor/_background_service.py", line 237, in __del__
  File "/home/sahas/.local/lib/python3.11/site-packages/frequenz/sdk/actor/_background_service.py", line 129, in cancel
AttributeError: 'SocActor' object has no attribute '_tasks'
After
Traceback (most recent call last):
  File "/home/sahas/code/sdk/ev-power-distribution/src/frequenz/sdk/actor/_background_service.py", line 244, in __del__
  File "/home/sahas/code/sdk/ev-power-distribution/src/frequenz/sdk/actor/_background_service.py", line 132, in cancel
  File "/home/sahas/code/sdk/ev-power-distribution/src/frequenz/sdk/actor/_background_service.py", line 264, in _assert_initialized
AssertionError: SocActor instance is not initialized. Make sure to call `super().__init__()` in your __init__() method.

@shsms shsms marked this pull request as ready for review January 12, 2024 17:53
@shsms shsms requested a review from a team as a code owner January 12, 2024 17:53
@shsms shsms requested a review from llucax January 12, 2024 17:53
@shsms shsms added this to the v1.0.0-rc4 milestone Jan 15, 2024
@llucax
Copy link
Contributor

llucax commented Jan 16, 2024

Isn't the missing call to super() detected by flake8 or pylint?

@shsms
Copy link
Contributor Author

shsms commented Jan 16, 2024

Isn't the missing call to super() detected by flake8 or pylint?

did you read the description?

@llucax
Copy link
Contributor

llucax commented Jan 16, 2024

did you read the description?

Haha, yes, but I guess I missed the first line, sorry.

I'm not sure it is worth adding the runtime cost of checking if it was initialized everywhere just to get a better error message in early stages of development.

I would like more opinions on this one, @matthias-wende-frequenz @Marenz @daniel-zullo-frequenz ?

@shsms
Copy link
Contributor Author

shsms commented Jan 16, 2024

I'm not sure it is worth adding the runtime cost of checking if it was initialized everywhere just to get a better error message in early stages of development.

it is an assert that doesn't allocate and runs only when a background service is starting up or stopping, so it is not really a runtime cost.

but it makes development easy especially for external developers who might not always want to start with repo config, etc, but might just want to start coding, and setup the tooling later, because that's what we've been showing in the demos as well.

In such a scenario, it is just annoying, and people would be more inclined to think the SDK is broken because the error message is totally unhelpful.

@Marenz
Copy link
Contributor

Marenz commented Jan 16, 2024

only when a background service is starting up or stopping,

I mean that's not exactly correct, looking at the diff it's also running on wait and is_running and a few more.
My feeling is that it's still okay as those methods are unlikely to be in a critical path and more used to start/stop things as well.

@llucax
Copy link
Contributor

llucax commented Jan 16, 2024

Yeah, it is true that runtime cost shouldn't be an issue, and now that I think about it, that's not the real/main reason I'm concerned about this PR.

I can understand it might have been annoying to bump to that weird error once, but for me it feels a bit weird to add asserts to check for the same we are checking with linters, just because maybe one forgets to run the linter one time, it is also about polluting the code, specially if we start doing this more and more.

I mean, I'd be fine to merge this one if it was really hard to figure out what was going on and would have save you some time, but I don't think it makes sense as a general practice.

@llucax
Copy link
Contributor

llucax commented Jan 16, 2024

In any case, this will probably go away when we fix #826, so I'm fine with merging it. I will still give it a bit more time before the approval to see if @matthias-wende-frequenz or @daniel-zullo-frequenz also have any opinion on the topic.

@shsms
Copy link
Contributor Author

shsms commented Jan 16, 2024

just because maybe one forgets to run the linter one time

I think you're only considering frequenz users who would all use repo-config, etc. It is not impossible to imagine external users of the sdk that don't have pylint as part of their workflow.

@daniel-zullo-frequenz
Copy link
Contributor

I agree the current patch is fine for this particular scenario but it shouldn't be considered as a good/general practice, otherwise all the attributes of the parent class would need to be checked as well.
And we should encourage non-frequenz users to use linters, the ones that can be configured in Editors/IDEs are already quite good.

@shsms
Copy link
Contributor Author

shsms commented Jan 19, 2024

And we should encourage non-frequenz users to use linters, the ones that can be configured in Editors/IDEs are already quite good.

a lot of people just use flake8, and that doesn't detect this issue. And pyright (the LSP server I think most people use with editors) doesn't detect them either.

@daniel-zullo-frequenz
Copy link
Contributor

daniel-zullo-frequenz commented Jan 19, 2024

hmm Pylint detected it in VS code if not calling super().__init__(name=name)

image

@shsms
Copy link
Contributor Author

shsms commented Jan 19, 2024

Oh wow, if vscode does, then I'm sure other popular things like pycharm do as well. Ok, maybe this was just a side effect of not using the popular tool. Sorry about the noise. :-)

@shsms shsms closed this Jan 19, 2024
@shsms shsms deleted the background-service-improvements branch January 19, 2024 12:49
@llucax
Copy link
Contributor

llucax commented Feb 1, 2024

Just for the records, pyright also detects it:

image

@shsms
Copy link
Contributor Author

shsms commented Feb 1, 2024

interesting, I guess my pyright is just badly configured then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:actor Affects an actor ot the actors utilities (decorator, etc.) part:docs Affects the documentation

Projects

Development

Successfully merging this pull request may close these issues.

4 participants