Skip to content

Commit

Permalink
Add mypy configuration (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathangreen authored Feb 15, 2024
1 parent 5439583 commit bdbe0a7
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 20 deletions.
158 changes: 156 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,50 @@ branch = true
relative_files = true
source = ["virtual_library_card", "virtuallibrarycard"]

[tool.django-stubs]
django_settings_module = "virtual_library_card.settings.dev"

[tool.isort]
profile = "black"

[tool.mypy]
# TODO: Enable the the check_untyped_defs option
# This will get rid of the warnings that we get when running mypy
# > note: By default the bodies of untyped functions are not checked
# However this currently causes a number of errors to surface that will
# need to be cleaned up before we can enable the option.
# check_untyped_defs = true
# When we enable this option, we should remove this disable. Its just here
# to silence the noise in the mypy output for now, so its easier to see when
# there are errors in the output.
disable_error_code = "annotation-unchecked"
files = ["."]
plugins = ["mypy_django_plugin.main"]
warn_redundant_casts = true
warn_unreachable = true
warn_unused_configs = true
warn_unused_ignores = true

[[tool.mypy.overrides]]
# This override silences errors for modules we import that don't currently
# have type hints, or type stubs that cover them. We should go through this
# list periodically and remove modules that have since added type hints.
ignore_missing_imports = true
module = [
"better_profanity.*",
"crispy_forms.*",
"dal.*",
"datedelta.*",
"localflavor.*",
"parameterized.*",
"rest_framework.*",
"uwsgi.*",
# This is ignored because the file is created when building a container
# so it typically doesn't exist when running mypy, but since it only
# contains a couple version strings it can be safely ignored
"virtual_library_card._version",
]

[tool.poetry]
authors = ["The Palace Project <[email protected]>"]
description = "Virtual Library Card Creator"
Expand Down Expand Up @@ -77,10 +118,13 @@ tox-docker = "^4.0"
tox-gh-actions = "^3.0"

[tool.poetry.group.dev.dependencies]
django-stubs = {version = "^4.2.7", extras = ["compatible-mypy"]}
mypy = "^1.7.0"
parameterized = "^0.9"
pytest = "^8.0.0"
pytest-cov = "^4.0.0"
pytest-django = "^4.5.2"
types-requests = "^2.28.11"

[tool.pytest.ini_options]
addopts = [
Expand Down
2 changes: 1 addition & 1 deletion tests/admin/test_library_admin_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _get_library_change_data(self, library, **changes):
return data

def _add_library_states_data(
self, data: dict, places: list[str] = None, library=None
self, data: dict, places: list[str] | None = None, library=None
):
"""Helper function for change form data, for the inline library form"""
places = places or []
Expand Down
2 changes: 1 addition & 1 deletion tests/admin/test_library_card_admin_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class TestLibraryCardAdminViews(BaseAdminUnitTest):
MODEL = LibraryCard
MODEL_ADMIN = LibraryCardAdmin

def _get_card_data(self, card: LibraryCard = None, **data):
def _get_card_data(self, card: LibraryCard | None = None, **data):
initial = {
"number": "",
"created": "",
Expand Down
4 changes: 3 additions & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ def tearDown(self):
self._transaction.__exit__(None, None, None)
super().tearDown()

def do_library_card_signup_flow(self, client: Client, library: Library = None):
def do_library_card_signup_flow(
self, client: Client, library: Library | None = None
):
"""A common flow which is needed multiple times"""

if not library:
Expand Down
4 changes: 2 additions & 2 deletions virtual_library_card/api/dynamic_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def _request(
self,
method: Literal["get"] | Literal["post"],
path: str,
data: Any = None,
json: dict = None,
data: Any | None = None,
json: dict | None = None,
) -> Any:
"""Make a REST request to the dynamic links API
:param method: The HTTP method
Expand Down
2 changes: 1 addition & 1 deletion virtual_library_card/sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def text_whitespaces_to_html(string: str) -> str:
def send_user_welcome(
library: Library,
user: CustomUser,
card_number: str = None,
card_number: str | None = None,
):
"""Send out a welcome email which has two optional parts
- User welcome for a new card
Expand Down
16 changes: 8 additions & 8 deletions virtuallibrarycard/business_rules/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class LibraryRules:
def validate_user_address_fields(
cls,
library: Library,
city: str = None,
county: str = None,
state: str = None,
country: str = None,
city: str | None = None,
county: str | None = None,
state: str | None = None,
country: str | None = None,
) -> bool:
"""Validate whether the given address fields are valid for a user that would signup for a given library
- Country, State or City, at least one must be within the list of places of the library
Expand All @@ -31,10 +31,10 @@ def validate_user_address_fields(
def _place_hierarchy_match(
cls,
place: Place,
city: str = None,
county: str = None,
state: str = None,
country: str = None,
city: str | None = None,
county: str | None = None,
state: str | None = None,
country: str | None = None,
) -> bool:
"""Test from the current place all the way to the last parent available.
All levels of the place hierarchy MUST match even if the value isn't provided in the keyword args.
Expand Down
6 changes: 3 additions & 3 deletions virtuallibrarycard/business_rules/library_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class LibraryCardRules:
@classmethod
def new_card(
cls, user: CustomUser, library: Library, number: str = None
cls, user: CustomUser, library: Library, number: str | None = None
) -> tuple[LibraryCard, bool]:
"""Generate a new card only of a card does not exist for this user and library
Also send the welcome email"""
Expand Down Expand Up @@ -50,7 +50,7 @@ def bulk_upload_csv(
cls,
library: Library,
fileio: IO,
admin_user: CustomUser = None,
admin_user: CustomUser | None = None,
_async: bool = False,
) -> LibraryCardBulkUpload:
"""Bulk upload a CSV of library users with information enough to generate cards
Expand All @@ -63,7 +63,7 @@ def __init__(
self,
library: Library,
fileio: IO,
admin_user: CustomUser = None,
admin_user: CustomUser | None = None,
_async: bool = True,
) -> None:
self.library = library
Expand Down
2 changes: 1 addition & 1 deletion virtuallibrarycard/forms/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Meta:
exclude = []
labels = {"user": _("User Email")}

def __init__(self, data: dict = None, *args, **kwargs) -> None:
def __init__(self, data: dict | None = None, *args, **kwargs) -> None:
super().__init__(data, *args, **kwargs)

# If we have the field and also have a new instance (on create)
Expand Down

0 comments on commit bdbe0a7

Please sign in to comment.