Skip to content

Refactor: Add type hints to utils.py and dbapi.py (#222)#256

Open
YatesMold wants to merge 4 commits intofedora-infra:developfrom
YatesMold:issue-222-type-hints
Open

Refactor: Add type hints to utils.py and dbapi.py (#222)#256
YatesMold wants to merge 4 commits intofedora-infra:developfrom
YatesMold:issue-222-type-hints

Conversation

@YatesMold
Copy link
Copy Markdown

This PR addresses the first part of issue #222 by adding type hints to the core database API and utility functions.

Changes made:

  • Added typing annotations to tahrir_api/dbapi.py
  • Added typing annotations to tahrir_api/utils.py

All tests pass successfully locally via tox -e py310.

Fixes #222 (partially, as agreed with @gridhead to start from these core modules).

@gridhead gridhead self-assigned this Feb 19, 2026
Copy link
Copy Markdown
Member

@gridhead gridhead left a comment

Choose a reason for hiding this comment

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

@YatesMold, please look into the breaking format checks.

@YatesMold
Copy link
Copy Markdown
Author

Thanks @gridhead! Good catch. I just ran black locally to format dbapi.py and utils.py, and pushed the update. The formatting checks should pass now. Let me know if everything looks good

Copy link
Copy Markdown
Member

@gridhead gridhead left a comment

Choose a reason for hiding this comment

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

So far so good.

Here are some suggestions.

return series_id

def get_all_series(self):
def get_all_series(self) -> Any:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please look into more specific types here instead of liberally using Any.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe try doing this?

Suggested change
def get_all_series(self) -> Any:
def get_all_series(self) -> Query[Series]:

return unique_badges

def get_all_badges(self):
def get_all_badges(self) -> Any:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please look into more specific types here instead of liberally using Any.

return self.get_milestone_from_badge_series(badge_id, series_id).count() != 0

def get_milestone_from_badge_series(self, badge_id, series_id):
def get_milestone_from_badge_series(self, badge_id: str, series_id: str) -> Any:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please look into more specific types here instead of liberally using Any.

return unique_badges

def get_all_badges(self):
def get_all_badges(self) -> Any:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe try doing this?

Suggested change
def get_all_badges(self) -> Any:
def get_all_badges(self) -> Query[Badge]:

return person.opt_out

def get_all_persons(self, include_opted_out=False):
def get_all_persons(self, include_opted_out: bool = False) -> Any:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please look into more specific types here instead of liberally using Any.

:param expires_on: When this invitation expires.

:type created_by: str
:type created_by_email: str
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good work here too.

) -> str:
"""
Add a new invitation to the database

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any reason why the remaining parameters are not included in the docstrings since you did make it a point to correct the ones that do exist?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also, since the returns have been type-hinted too, along with those for the parameters, please include those too in your changes.

Comment on lines +800 to +802
person = self.get_person(created_by_email)
if not person:
raise ValueError(f"Could not retrieve person with email {created_by_email!r}")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What's with these paranoia-stricken double-checks when we did confirm before that the person exists? These repeated lookups against the database do not serve any practical purpose other than slowing down the performance of the API layer. Please explore your changes to remove such unnecessary changes.

Comment on lines +1160 to +1161
if not person:
return False
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good work here too.

Comment on lines +1215 to +1216
if not person:
return
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here again, we are adding the person if they do not exist, so there is no reason why we would not want to trust the execution of the function from a couple of lines above. Our database-accessing API is synchronous, and we would rather let these errors occur, as rare as they are, than have alarmingly many safety conditions throughout the codebase.

Copy link
Copy Markdown
Member

@gridhead gridhead left a comment

Choose a reason for hiding this comment

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

See, we are open to AI-assisted contributions as long as the contributor follows the guidelines mentioned in the previously linked documentation. You have not been transparent about the usage of a large language model tooling into shaping your contributions, and your commit messages, 83af76c, 22c300c and 3cfe6a8, do not clearly describe that you have been assisted by a certain tooling.

Please look into this.

Assisted-by: Google Gemini
Assisted-by: Google Gemini
Assisted-by: Google Gemini
@YatesMold YatesMold force-pushed the issue-222-type-hints branch from 3cfe6a8 to f7e86b0 Compare February 24, 2026 08:44
@YatesMold
Copy link
Copy Markdown
Author

Hi @gridhead, thanks for the review and for pointing me to the docs! You are right. I used Google Gemini as a sounding board to double-check my type hint logic and to help draft the commit messages.

Since I'm still getting the hang of open source, I completely missed the AI contribution guidelines. My bad!
I'm doing an interactive rebase right now to add the Assisted-by: tags to my commit history. I'll force push the updates in a few minutes. Thanks for being patient with me

Copy link
Copy Markdown
Contributor

@sdglitched sdglitched left a comment

Choose a reason for hiding this comment

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

@YatesMold, it seems you have created a new commit for making the changes requested by @gridhead.

It's a good practice to keep the commits specific to the final changes rather than new commits for the changes requested by the reviewer.

Please squash the latest commit with the existing ones.

Also, the lint test seems to have failed. Please check and make necessary changes.

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.

Add type hints to the functions and methods in the project's codebase

3 participants