Refactor: Add type hints to utils.py and dbapi.py (#222)#256
Refactor: Add type hints to utils.py and dbapi.py (#222)#256YatesMold wants to merge 4 commits intofedora-infra:developfrom
Conversation
gridhead
left a comment
There was a problem hiding this comment.
@YatesMold, please look into the breaking format checks.
|
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 |
gridhead
left a comment
There was a problem hiding this comment.
So far so good.
Here are some suggestions.
tahrir_api/dbapi.py
Outdated
| return series_id | ||
|
|
||
| def get_all_series(self): | ||
| def get_all_series(self) -> Any: |
There was a problem hiding this comment.
Please look into more specific types here instead of liberally using Any.
There was a problem hiding this comment.
Maybe try doing this?
| def get_all_series(self) -> Any: | |
| def get_all_series(self) -> Query[Series]: |
tahrir_api/dbapi.py
Outdated
| return unique_badges | ||
|
|
||
| def get_all_badges(self): | ||
| def get_all_badges(self) -> Any: |
There was a problem hiding this comment.
Please look into more specific types here instead of liberally using Any.
tahrir_api/dbapi.py
Outdated
| 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: |
There was a problem hiding this comment.
Please look into more specific types here instead of liberally using Any.
tahrir_api/dbapi.py
Outdated
| return unique_badges | ||
|
|
||
| def get_all_badges(self): | ||
| def get_all_badges(self) -> Any: |
There was a problem hiding this comment.
Maybe try doing this?
| def get_all_badges(self) -> Any: | |
| def get_all_badges(self) -> Query[Badge]: |
tahrir_api/dbapi.py
Outdated
| return person.opt_out | ||
|
|
||
| def get_all_persons(self, include_opted_out=False): | ||
| def get_all_persons(self, include_opted_out: bool = False) -> Any: |
There was a problem hiding this comment.
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 |
| ) -> str: | ||
| """ | ||
| Add a new invitation to the database | ||
|
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Also, since the returns have been type-hinted too, along with those for the parameters, please include those too in your changes.
tahrir_api/dbapi.py
Outdated
| person = self.get_person(created_by_email) | ||
| if not person: | ||
| raise ValueError(f"Could not retrieve person with email {created_by_email!r}") |
There was a problem hiding this comment.
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.
| if not person: | ||
| return False |
tahrir_api/dbapi.py
Outdated
| if not person: | ||
| return |
There was a problem hiding this comment.
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.
gridhead
left a comment
There was a problem hiding this comment.
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
3cfe6a8 to
f7e86b0
Compare
|
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! |
Assisted-by: Google Gemini
sdglitched
left a comment
There was a problem hiding this comment.
@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.
This PR addresses the first part of issue #222 by adding type hints to the core database API and utility functions.
Changes made:
typingannotations totahrir_api/dbapi.pytypingannotations totahrir_api/utils.pyAll tests pass successfully locally via
tox -e py310.Fixes #222 (partially, as agreed with @gridhead to start from these core modules).