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

Return None instead of -1 on failure (when success result is a bookmark ID) #648

Closed
LeXofLeviafan opened this issue Dec 14, 2022 · 10 comments · Fixed by #660
Closed

Return None instead of -1 on failure (when success result is a bookmark ID) #648

LeXofLeviafan opened this issue Dec 14, 2022 · 10 comments · Fixed by #660

Comments

@LeXofLeviafan
Copy link
Collaborator

LeXofLeviafan commented Dec 14, 2022

As of now, several BukuDb methods (get_rec_id(), get_max_id(), add_rec()) return a bookmark ID on success, but on failure they return -1. I suggest changing their failure value to None.

Reasoning:

  • Python is not C so None is always a valid return value
  • None is basically meant for this kind of purpose
  • the row numbers start with 1 so returning a falsey value on failure would make the success checks more natural (i.e. if buku.add_rec(…))
  • even the explicit value comparison becomes more transparent (is None instead of == -1)
  • (edit) -1 is an idiomatically valid index value in Python, which makes it even worse of a fit for "no index" value for this language.
@rachmadaniHaryono
Copy link
Collaborator

im undecided on this

the reasoning seem good but it may break other program that use buku

but after checking related buku project, not a lot of program use buku with python

also the next release should be major version, so maybe just go for it?

i havent check bukuserver yet

@LeXofLeviafan
Copy link
Collaborator Author

but after checking related buku project, not a lot of program use buku with python

Might be because of this 😅

@jarun
Copy link
Owner

jarun commented Dec 15, 2022

Let's do the change. We'll document this in the release notes.

@LeXofLeviafan
Copy link
Collaborator Author

…I found a few other things which can be improved in the source of the main buku file, so I'm opening respective issues to discuss them. I suggest fixing them along with this one.

@LeXofLeviafan
Copy link
Collaborator Author

…Tried my hand at implementing the suggested changes (for this and other issues on buku API). You can check it out committed here.

This includes: returning None as "no ID" value, returning [] as "empty search results" value, removing Optional hints where not necessary, changing the type of immutable parameter to bool (or Optional[bool]), making BookmarkVar a named tuple. (…Also simplified a few conditionals where it makes sense.)

And yes, I've made sure all the tests pass (aside from the network one blocked for me by a remote server), and both CLI and GUI appear to be working (can't say I did a lot of manual checks though).

@rachmadaniHaryono
Copy link
Collaborator

rachmadaniHaryono commented Dec 19, 2022

as note #651 this is another refactor issue rejected

i thought with new release some refactor will be accepted but only this issue based on #648 (comment)

e: and this also accepted #654

@LeXofLeviafan
Copy link
Collaborator Author

as note #651 this is another refactor issue rejected

So far it's the only one rejected actually. #653 was never commented on so far, and the other two appear to have been accepted.

(If this comment is regarding the contents of my sample refactor commit, I'm waiting for feedback on #653 ATM)

@rachmadaniHaryono
Copy link
Collaborator

rachmadaniHaryono commented Dec 19, 2022

i miss that 653

i agree on the issue because it dont touch code structure at all

do you want to make pr for this? if yes also edit the docstring so type is only on code type hint

e: not sure where to put this but if you import something from typing use import typing as T instead

@LeXofLeviafan
Copy link
Collaborator Author

LeXofLeviafan commented Dec 19, 2022

Well the word "optional" in the docstring can be interpreted as aimed at the parameter itself rather than the value type, so I figured there's no issue keeping it as it is.

As for typing, I've kept the existing import statement rather than making a single type hint differently from all others (or replacing all of them across the entire 6k LOC).

do you want to make pr for this?

I can make one, but only after #646 is closed – to avoid merge conflicts (it involves checking the same -1/None value)

@rachmadaniHaryono
Copy link
Collaborator

Well the word "optional" in the docstring can be interpreted as aimed at the parameter itself rather than the value type, so I figured there's no issue keeping it as it is.

i disagree with this.

  1. user should understand that parameter is optional when there is default value for that parameter. there maybe time when this is not the case but if that happen just explaint it on docstring instead of putting optional on docstring
  2. beginner python may confused between docstring optional and typing.optional

As for typing, I've kept the existing import statement rather than making a single type hint differently from all others (or replacing all of them across the entire 6k LOC).

dont worry about it, just make it with minimum effort as possible

@LeXofLeviafan LeXofLeviafan mentioned this issue Jan 7, 2023
97 tasks
LeXofLeviafan added a commit to LeXofLeviafan/buku that referenced this issue Jan 7, 2023
LeXofLeviafan added a commit to LeXofLeviafan/buku that referenced this issue Jan 7, 2023
LeXofLeviafan added a commit to LeXofLeviafan/buku that referenced this issue Jan 7, 2023
LeXofLeviafan added a commit to LeXofLeviafan/buku that referenced this issue Jan 22, 2023
LeXofLeviafan added a commit to LeXofLeviafan/buku that referenced this issue Jan 27, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Feb 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants