Skip to content
This repository has been archived by the owner on Oct 2, 2021. It is now read-only.

Commit

Permalink
Switch to mypy
Browse files Browse the repository at this point in the history
This is a work-in-progress attempt at switching over to mypy as part of
 #85. There's still some work to be done (e.g. mypy ignore comments need
 to be added where appropriate) and I'm currently working with some
 local changes pyre's stubs, but I wanted to track the work being done.
  • Loading branch information
dirn committed Oct 12, 2020
1 parent 263bc47 commit a8e31cc
Show file tree
Hide file tree
Showing 21 changed files with 16 additions and 90 deletions.
12 changes: 0 additions & 12 deletions src/.pyre_configuration

This file was deleted.

17 changes: 1 addition & 16 deletions src/applications/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class FinancialAidApplicationForm(ApplicationForm):
type = Application.Type.FINANCIAL_AID

travel_requested = forms.BooleanField(
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
label=_("Do you need assistance with travel?"),
required=False,
)
Expand All @@ -32,13 +31,9 @@ class Meta:
"lodging_requested",
)
labels = {
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
"background": _("Tell us a little bit more about yourself"),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
"lodging_requested": _("Do you need assistance with lodging?"),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
"reason_to_attend": _("Why are you interested in attending PyGotham?"),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
"travel_amount": _("What is the estimated cost (USD)?"),
}
widgets = {
Expand All @@ -51,20 +46,14 @@ def clean(self) -> Dict[str, Any]:
travel_amount = cleaned_data.get("travel_amount") or 0
if travel_amount < 0:
raise forms.ValidationError(
{
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
"travel_amount": _(
"Your estimated travel costs cannot be negative."
)
}
{"travel_amount": _("Your estimated travel costs cannot be negative.")}
)

travel_requested = cleaned_data.get("travel_requested")
if travel_requested:
if not travel_amount:
raise forms.ValidationError(
{
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
"travel_amount": _(
"Your estimated travel costs must be greater than $0.00."
)
Expand All @@ -73,7 +62,6 @@ def clean(self) -> Dict[str, Any]:
elif travel_amount:
raise forms.ValidationError(
{
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
"travel_requested": _(
"You must request travel assistance before providing an estimated cost."
)
Expand All @@ -83,7 +71,6 @@ def clean(self) -> Dict[str, Any]:
return cleaned_data

def clean_lodging_requested(self) -> bool:
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/261.
return bool(self.cleaned_data.get("lodging_requested"))


Expand All @@ -94,9 +81,7 @@ class Meta:
model = Application
fields = ("background", "reason_to_attend")
labels = {
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
"background": _("Tell us a little bit about yourself"),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
"reason_to_attend": _("Why are you interested in attending PyGotham?"),
}

Expand Down
1 change: 0 additions & 1 deletion src/applications/migrations/0003_auto_20200507_0125.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name="application",
name="travel_amount",
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/260.
field=models.DecimalField(
blank=True, decimal_places=2, max_digits=10, null=True
),
Expand Down
9 changes: 0 additions & 9 deletions src/applications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,35 @@
from django.db import models
from django.utils.translation import ugettext_lazy as _

# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
User = get_user_model()


class Application(models.Model):
# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
class Status(models.TextChoices):
PENDING = "pending"

# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
class Type(models.TextChoices):
FINANCIAL_AID = "finaid"
SCHOLARSHIP = "scholarship"

# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
applicant = models.ForeignKey(User, on_delete=models.DO_NOTHING)
background = models.TextField(_("applicant background"))
reason_to_attend = models.TextField(_("reason the applicant wishes to attend"))
status = models.CharField(
max_length=20,
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
choices=Status.choices,
default=Status.PENDING,
)
type = models.CharField(
max_length=11,
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
choices=Type.choices,
default=Type.SCHOLARSHIP,
)
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/260.
travel_amount = models.DecimalField(
max_digits=10, decimal_places=2, blank=True, null=True
)
lodging_requested = models.BooleanField(null=True)

def __str__(self) -> str:
# pyre-ignore[19]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
type_ = Application.Type(self.type)
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
return f"{type_.label} application for {self.applicant}"
1 change: 0 additions & 1 deletion src/applications/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ def test_financial_aid_lodging_requested_is_treated_as_boolean() -> None:

form = FinancialAidApplicationForm(other_fields)
assert form.is_valid()
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/261.
assert form.cleaned_data["lodging_requested"] is False


Expand Down
7 changes: 0 additions & 7 deletions src/applications/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from applications.views import apply


# pyre-ignore[13]: Investigate type stubs for factory-boy.
class ApplicationFactory(DjangoModelFactory):
class Meta:
model = Application
Expand All @@ -24,29 +23,25 @@ class Meta:

class UserFactory(DjangoModelFactory):
class Meta:
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
model = get_user_model()
django_get_or_create = ("email",)

email = "[email protected]"


@pytest.mark.django_db
# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
def test_that_one_of_form_type_and_pk_is_required_by_apply(client: Client) -> None:
user = UserFactory()
qs = get_query_string(user)
client.get(f"/login/magic{qs}")

request = HttpRequest()
request.user = user
# pyre-ignore[16]: pyre doesn't think ExceptionInfo as an __enter__.
with pytest.raises(Http404):
apply(request, form_type=None, pk=None)


@pytest.mark.django_db
# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
def test_user_can_edit_their_application(client: Client) -> None:
user = UserFactory()
qs = get_query_string(user)
Expand All @@ -67,7 +62,6 @@ def test_user_can_edit_their_application(client: Client) -> None:


@pytest.mark.django_db
# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
def test_user_can_view_their_application(client: Client) -> None:
user = UserFactory()
qs = get_query_string(user)
Expand All @@ -80,7 +74,6 @@ def test_user_can_view_their_application(client: Client) -> None:


@pytest.mark.django_db
# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
def test_user_cant_view_someone_elses_application(client: Client) -> None:
user = UserFactory()
other = UserFactory(email=f"other+{user.email}")
Expand Down
4 changes: 0 additions & 4 deletions src/applications/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,15 @@
app_name = "applications"

urlpatterns = [
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path("edit/<int:pk>", apply, name="edit"),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path(
"financial-aid",
apply,
{"form_type": FinancialAidApplicationForm},
name="financial_aid",
),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path(
"ticket", apply, {"form_type": ScholarshipApplicationForm}, name="scholarship"
),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path("view/<int:pk>", view, name="view"),
]
9 changes: 6 additions & 3 deletions src/awards/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

import os
from typing import List

from decouple import config
from decouple import config # type: ignore[import]

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -9,7 +12,7 @@
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

ALLOWED_HOSTS = []
ALLOWED_HOSTS: List[str] = []


# Application definition
Expand Down Expand Up @@ -77,7 +80,7 @@
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

# Password-based login isn't supported.
AUTH_PASSWORD_VALIDATORS = []
AUTH_PASSWORD_VALIDATORS: List[str] = []

AUTH_USER_MODEL = "users.User"

Expand Down
2 changes: 1 addition & 1 deletion src/awards/settings/prod.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from decouple import config
from decouple import config # type: ignore[import]

from awards.settings.base import * # NOQA

Expand Down
11 changes: 1 addition & 10 deletions src/awards/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,27 @@
from users.views import login, magic_login

urlpatterns = [
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path(
"", TemplateView.as_view(template_name="homepage/index.html"), name="homepage"
),
# pyre doesn't include stubs for the Django admin.
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path(
"admin/login/",
RedirectView.as_view(
pattern_name=settings.LOGIN_URL, permanent=True, query_string=True
),
),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path("admin/", admin.site.urls), # type: ignore
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path("apply/", include("applications.urls", namespace="applications")),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path("login", login, name="login"),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path("login/magic", magic_login, name="magic-login"),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path("logout", LogoutView.as_view(), name="logout"),
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path("users/", include("users.urls", namespace="users")),
]

if settings.DEBUG:
import debug_toolbar
import debug_toolbar # type: ignore[import]

urlpatterns = [
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
path("__debug__/", include(debug_toolbar.urls)),
] + urlpatterns
2 changes: 0 additions & 2 deletions src/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from django.test import Client


# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
def client() -> Client:
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
return Client()
3 changes: 0 additions & 3 deletions src/homepage/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@

class UserFactory(DjangoModelFactory):
class Meta:
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
model = get_user_model()
django_get_or_create = ("email",)

email = "[email protected]"


@pytest.mark.django_db
# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
def test_login_link_is_not_shown_to_logged_in_users(client: Client) -> None:
user = UserFactory()
qs = get_query_string(user)
Expand All @@ -27,7 +25,6 @@ def test_login_link_is_not_shown_to_logged_in_users(client: Client) -> None:
assert b"log in" not in response.content.lower()


# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
def test_login_link_is_shown_to_guests(client: Client) -> None:
response = client.get("/")
assert b"log in" in response.content.lower()
3 changes: 3 additions & 0 deletions src/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[mypy]
files = awards
mypy_path = ../.tox/types/lib/pyre_check/stubs
1 change: 0 additions & 1 deletion src/users/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@

class LoginForm(forms.Form):
email = forms.EmailField(
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/262.
widget=forms.EmailInput(attrs={"placeholder": _("Email address")})
)
2 changes: 0 additions & 2 deletions src/users/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ class Migration(migrations.Migration):
),
(
"email",
# pyre-ignore[28]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
django.contrib.postgres.fields.citext.CIEmailField(
max_length=254, unique=True, verbose_name="email address"
),
),
(
"date_joined",
models.DateTimeField(
# pyre-ignore[6]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
default=django.utils.timezone.now,
verbose_name="date joined",
),
Expand Down
9 changes: 0 additions & 9 deletions src/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,23 @@
from django.utils.translation import ugettext_lazy as _


# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/272.
class UserManager(BaseUserManager):
def create_user(self, email: str, password: str) -> User:
# pyre-ignore[28]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
user = User(email=email)
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
user.save()
return user

def create_superuser(self, email: str, password: str) -> User:
# pyre-ignore[28]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
user = User(email=email, is_staff=True)
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
user.save()
return user


# pyre-ignore[11]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
class User(AbstractBaseUser):
# pyre-ignore[28]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
email = CIEmailField(_("email address"), unique=True)
# Blank passwords are allowed since we only allow applicants to use
# magic links to log in.
password = models.CharField(_("password"), max_length=128, blank=True)
# pyre-ignore[6]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
date_joined = models.DateTimeField(_("date joined"), default=timezone.now)

is_staff = models.BooleanField(_("user can access the admin"), default=False)
Expand All @@ -44,7 +36,6 @@ class User(AbstractBaseUser):
is_active = True

def __str__(self) -> str:
# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
username, domain = self.email.split("@", 1)

# Hide the characters in the username other than the first one.
Expand Down
1 change: 0 additions & 1 deletion src/users/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

pytestmark = pytest.mark.django_db

# pyre-ignore[16]: This is fixed by https://github.com/facebook/pyre-check/pull/256.
User = get_user_model()


Expand Down
Loading

0 comments on commit a8e31cc

Please sign in to comment.