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

frontend: unify naming convention for safe methods #2922

Merged
merged 1 commit into from
Nov 20, 2023

Conversation

nikromen
Copy link
Member

@nikromen nikromen commented Sep 19, 2023

Originally, safe methods were considered as methods raising ObjectNotFound for API (mainly with session.one() method). Now methods which returns None or ignores the error completely are also considered as "safe".

For the sake of consistency:

  • *_or_none -> returns None if object was not found in the database
  • *_safe -> methods that ignores error completely (they basically logs and/or are safe from exceptions)

and renaming some of the current safe methods to normal ones that aren't safe by the new rules

@nikromen nikromen force-pushed the love-for-safe-methods branch from 1d8b232 to f2fb5f3 Compare September 19, 2023 15:48
@nikromen
Copy link
Member Author

the fail in CI (copr-common rawhide) seems unrelated to this PR (some problem with package python-six)

also these linter issues were there before 💀 bad linter! :D

@praiskup
Copy link
Member

also these linter issues were there before 💀 bad linter! :D

Detecting that this is a function rename is a rather complicated task not implemented in csdiff. You need to be a bit more benevolent if you rename calls. :-)

@praiskup
Copy link
Member

Can you document the style in CONTRIBUTE.md?

@praiskup
Copy link
Member

Even though, ... I don't know. I don't feel anyhow "safe" when I'm calling "_safe" methods. And creating new doesn't make much sense to me. My personal feeling is that we should much better document the method behavior in docstrings....

@praiskup
Copy link
Member

I also somewhat dislike that this "safe" concept leaks outside the frontend/ boundaries :-) :-)

@nikromen
Copy link
Member Author

I also somewhat dislike that this "safe" concept leaks outside the frontend/ boundaries :-) :-)

hmm that's true... but what to do with the safe methods in them? keep them as they are or delete safe suffix?

Even though, ... I don't know. I don't feel anyhow "safe" when I'm calling "_safe" methods. And creating new doesn't make much sense to me.

As I understood it, they are safe because they catch not-found tracebacks and instead of reraising them (and causing traceback mess in the API calls -> would result to 500 I guess) it translates it to 404 with reasonable message so this is somewhat safe.

My personal feeling is that we should much better document the method behavior in docstrings....

I am not against getting rid of the suffixes completely and documenting it... I care only about clarifying the inconsistency :D we can shortly discuss it at standup

@nikromen nikromen requested a review from FrostyX September 25, 2023 10:44
@FrostyX
Copy link
Member

FrostyX commented Sep 27, 2023

I am all for changing the status quo because nobody really knows what "safe" means (probably because on every place it means something different)

*_noop_safe -> methods that ignores error completely (they basically logs)
*_none_safe -> returns None if object was not found in the database
*_safe -> raises an exception (ObjectNotFound) if no object found

But this is IMHO too many cases

My favorite is _none_safe, I think it is very useful to have methods like these. Considering that @praiskup doesn't like the word "safe" (I get it) we could easily change the name to something else. For example SQLAlchemy has .one() and .one_or_none() so we could use this convention and have e.g. get_chroot_or_none(...).

*_safe -> raises an exception (ObjectNotFound) if no object found

I think we shouldn't have to care if we raised our custom exception or the default SQLAlchemy exception. If we do, we should IMHO fix our error handler instead. So, I would remove the suffix entirely.

*_noop_safe -> methods that ignores error completely (they basically logs)

I cannot make up my mind here but I guess having methods about which we know that they don't raise any exceptions, is a good idea. If we change the naming as I suggested above, we could say that "safe" means "safe from exceptions" and simply use _safe for this case.

However, in many cases _or_none is basically the same thing as "safe from exceptions", so I don't know if we want to distinguish them or not.

What do you think?

@praiskup
Copy link
Member

What do you think?

I agree, and to your question - I think we should have _safe and _or_none.

Repeating my concern; we should stay on Frontend with this concept, and we should document it.

@praiskup
Copy link
Member

_safe doesn't imply _or_none and vice versa, right? So once could think of a thing_or_none_safe?

@FrostyX
Copy link
Member

FrostyX commented Sep 27, 2023

I agree, and to your question - I think we should have _safe and _or_none.

+1

_safe doesn't imply _or_none and vice versa, right? So once could think of a thing_or_none_safe?

From what I've seen the _or_none is used in our code when we query and return things from the database and the _safe in sense of "safe from exceptions" is mainly used for doing things and not expecting results. So theoretically there could be thing_or_none_safe but I think we won't have any.

Repeating my concern; we should stay on Frontend with this concept

Looking at the PR changes, even the original code with _safe already leaked out frontend

and we should document it.

Absolutely.
I guess here https://github.com/fedora-copr/copr/blob/main/CONTRIBUTE.md ?

@praiskup
Copy link
Member

praiskup commented Oct 2, 2023

I guess here https://github.com/fedora-copr/copr/blob/main/CONTRIBUTE.md ?

I'd vote for that one, yes.

@nikromen
Copy link
Member Author

no progress

@nikromen nikromen force-pushed the love-for-safe-methods branch 3 times, most recently from d5f8bc4 to 867d0d5 Compare November 13, 2023 11:53
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

vcs-diff-lint found more than 10 potential problems in the proposed changes. Check the Files changed tab for more details.

@nikromen nikromen force-pushed the love-for-safe-methods branch 2 times, most recently from dfa790a to 107e173 Compare November 13, 2023 14:07
Originally, safe methods were considered as methods raising ObjectNotFound
for API (mainly with session.one() method). Now methods which returns None
or ignores the error completely are also considered as "safe".

For the sake of consistency:
- *_or_none -> returns None if object was not found in the database
- *_safe -> methods that ignores error completely (they basically
   log and/or are safe from exceptions)

and renaming some of the current safe methods to normal ones that aren't
safe by the new rules
@nikromen nikromen force-pushed the love-for-safe-methods branch from 107e173 to 504549c Compare November 13, 2023 14:11
@nikromen nikromen requested a review from praiskup November 13, 2023 14:11
@nikromen
Copy link
Member Author

I fixed also a buch of pylint errors with this since the linter was screaming at me with a lot of old errors that were already there so this PR is code-cleanup in general
@praiskup @FrostyX PTAL

@FrostyX
Copy link
Member

FrostyX commented Nov 20, 2023

LGTM but I would suggest running beaker tests against this just to be sure.

@praiskup
Copy link
Member

LGTM but I would suggest running beaker tests against this just to be sure.

It's going to be done before the release this week, so merging.

@praiskup praiskup merged commit 44aa094 into fedora-copr:main Nov 20, 2023
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants