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 type safety and type hinting #317

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Conversation

ashleyzhang01
Copy link
Contributor

@ashleyzhang01 ashleyzhang01 commented Nov 5, 2024

Type safety is important for catching bugs early, facilitating refactoring, and improving overall maintainability.
We should use type hints makes the code more understandable to both developers and static type checkers, so our code can be more reliable and predictable

We should:

  • Use type hints
  • Write code in a more functional style
  • Add comments for things like magic values, unintuitive behaviors, gotchas, external context, etc.
  • Types should have capitalized names
  • Keep things pythonic (e.g. use list comprehensions, avoid explicit for-loops, use built-in functions and libraries)
  • Use type aliases to give semantic meaning to primitive types (e.g., ModelPath = str)
  • Functions should typically return a singular type rather than unions
  • Functions not used outside their module should be prefixed with _
  • Use context managers (with statements) for resource management
  • Consider using Protocol classes for structural typing
  • Use assert to help type checking when function logic limits return types
  • Use typing.cast as a no-op to inform the type checker of a type when needed
  • Use schemas instead of args and kwargs 😭

TODO: (in the works) add mypy for type checking

Copy link

codecov bot commented Nov 5, 2024

Codecov Report

Attention: Patch coverage is 93.44262% with 28 lines in your changes missing coverage. Please review.

Project coverage is 90.68%. Comparing base (96a605c) to head (848c72b).

Files with missing lines Patch % Lines
backend/sublet/views.py 83.87% 10 Missing ⚠️
backend/sublet/serializers.py 91.22% 5 Missing ⚠️
backend/gsr_booking/views.py 94.00% 3 Missing ⚠️
backend/gsr_booking/api_wrapper.py 93.75% 2 Missing ⚠️
backend/gsr_booking/models.py 84.61% 2 Missing ⚠️
backend/gsr_booking/serializers.py 81.81% 2 Missing ⚠️
backend/portal/logic.py 81.81% 2 Missing ⚠️
backend/user/views.py 85.71% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #317      +/-   ##
==========================================
- Coverage   90.86%   90.68%   -0.19%     
==========================================
  Files          63       63              
  Lines        2661     2779     +118     
==========================================
+ Hits         2418     2520     +102     
- Misses        243      259      +16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ashleyzhang01 ashleyzhang01 marked this pull request as ready for review November 7, 2024 20:12
@Clue88
Copy link

Clue88 commented Nov 7, 2024

Screenshot 2024-11-07 at 3 33 19 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants