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

Add typehints in openlibrary.accounts module #9821

Merged

Conversation

yivgen
Copy link
Contributor

@yivgen yivgen commented Aug 28, 2024

Related to #8028

Adds typehints to openlibrary.accounts and openlibrary.plugins.upstream.mybooks modules

Technical

Testing

Screenshot

Stakeholders

@scottbarnes

@yivgen
Copy link
Contributor Author

yivgen commented Aug 28, 2024

Hi! I've been very busy for the past few days but I can continue working on this now

@yivgen yivgen force-pushed the 8028/refactor/add-more-typehints branch from 01aecdc to 8856cbe Compare August 29, 2024 16:10
@scottbarnes scottbarnes self-assigned this Aug 29, 2024
@yivgen yivgen force-pushed the 8028/refactor/add-more-typehints branch from 8856cbe to 0a86938 Compare August 29, 2024 16:12
@yivgen
Copy link
Contributor Author

yivgen commented Aug 29, 2024

Also monkeytype suggests adding typehints in ReadingLog innit like so:

def __init__(self, user: User | None = ...) -> None:

but then self.user could hypothetically be None but that wouldn't work with a lot of the methods like lists, booknotes_counts, reading_log_counts, count_shelf and get_works
And I'm not sure what to do about that

Copy link
Collaborator

@scottbarnes scottbarnes left a comment

Choose a reason for hiding this comment

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

This looks great, @yivgen. Just one minor piece of substantive feedback. Great job handling the if TYPE_CHECKING stuff, and the 'mess' between list and List.

I think the way you handled the case you mentioned makes sense. MonkeyType is far from perfect, sadly. :)

@@ -83,7 +83,7 @@ def batch_import(raw_data: bytes) -> BatchResult:
The line numbers errors use 1-based counting because they are line numbers in a file.
"""
user = accounts.get_current_user()
username = user.get_username()
username = user.get_username() if user else None
Copy link
Collaborator

Choose a reason for hiding this comment

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

I see you had to make this change to get mypy to pass, and in terms of fixing the error, I would say this is an acceptable thing to do for now. It does however expose an oversight on the part of the knucklehead who wrote this function.

What probably needs to happen here is a dependency injection-like thing, where the username is passed into batch_import. This current implementation sets up a case where (1) it's harder to unit test batch_import than it should be, and (2) people using the function may end up with a submitter of None without expecting it.

Right now, however, batch_import is only called in one place, and there it is gated by a requirement that the account be in the /usergroup/admin group:

user_key = delegate.context.user and delegate.context.user.key
if user_key not in _get_members_of_group("/usergroup/admin"):
raise Forbidden('Permission Denied.')

So let's just leave this change for now, as user.get_username() won't return None the way the function is currently called`, and it will be a future task to fix this up and write some tests.

@@ -626,7 +626,7 @@ def get_recently_logged_books(
return cls.fetch(logged_books) if fetch else logged_books

@classmethod
def get_users_read_status_of_work(cls, username: str, work_id: str) -> str | None:
def get_users_read_status_of_work(cls, username: str, work_id: str) -> int | None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good catch here:

openlibrary=# \d bookshelves_books
                         Table "public.bookshelves_books"
    Column    |            Type             |              Modifiers
--------------+-----------------------------+--------------------------------------
 username     | text                        | not null
 work_id      | integer                     | not null
 bookshelf_id | integer                     | not null
 edition_id   | integer                     |
 private      | boolean                     |
 updated      | timestamp without time zone | default timezone('utc'::text, now())
 created      | timestamp without time zone | default timezone('utc'::text, now())

openlibrary/plugins/upstream/mybooks.py Outdated Show resolved Hide resolved
@scottbarnes scottbarnes added the Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] label Sep 2, 2024
@scottbarnes scottbarnes removed the Needs: Submitter Input Waiting on input from the creator of the issue/pr [managed] label Sep 10, 2024
@scottbarnes scottbarnes merged commit d67bcdc into internetarchive:master Sep 10, 2024
4 checks passed
@scottbarnes
Copy link
Collaborator

Thanks for this, @yivgen!

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.

2 participants