Skip to content

Commit

Permalink
Merge pull request #215 from jodal/mypy
Browse files Browse the repository at this point in the history
Initial mypy setup
  • Loading branch information
jodal authored Mar 28, 2021
2 parents 5b16f2b + 379f7e9 commit 63c4d0a
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 23 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ jobs:
- name: "Lint: flake8"
python: "3.7"
tox: flake8
- name: "Lint: mypy"
python: "3.7"
tox: mypy
- name: "Docs"
python: "3.7"
tox: docs
Expand Down
27 changes: 15 additions & 12 deletions comics/aggregator/crawler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import re
import time
import xml
from dataclasses import dataclass, field
from typing import Dict, Optional, Tuple

import httpx
import pytz
Expand Down Expand Up @@ -59,33 +61,34 @@ def validate(self, identifier):
raise ImageURLNotFound(identifier)


@dataclass
class CrawlerBase:
# ### Crawler settings
# Date of oldest release available for crawling
history_capable_date = None
history_capable_date: Optional[str] = None
# Number of days a release is available for crawling
history_capable_days = None
history_capable_days: Optional[int] = None
# On what weekdays the comic is published (example: "Mo,We,Fr")
schedule = None
schedule: Optional[str] = None
# In approximately what time zone the comic is published
# (example: "Europe/Oslo")
time_zone = "UTC"
time_zone: str = "UTC"
# Whether to allow multiple releases per day
multiple_releases_per_day = False
multiple_releases_per_day: bool = False

# ### Downloader settings
# Whether the comic reruns old images as new releases
has_rerun_releases = False
has_rerun_releases: bool = False

# ### Settings used for both crawling and downloading
# Dictionary of HTTP headers to send when retrieving items from the site
headers = {}
headers: Dict[str, str] = field(default_factory=dict)

# Feed object which is reused when crawling multiple dates
feed = None
feed: Optional[FeedParser] = None

# Page objects mapped against URL for use when crawling multiple dates
pages = {}
pages: Dict[str, LxmlParser] = field(default_factory=dict)

def __init__(self, comic):
self.comic = comic
Expand Down Expand Up @@ -291,14 +294,14 @@ class NettserierCrawlerBase(CrawlerBase):
# In order to get older releases we need to
# loop through the pages and check the published date
time_zone = "Europe/Oslo"
page_cache = {}
page_cache: Dict[str, Tuple[LxmlParser, datetime.date]] = {}

def get_page(self, url):
def get_page(self, url) -> Tuple[LxmlParser, datetime.date]:
if url not in self.page_cache:
page = self.parse_page(url)
page_date = page.text('p[class="comic-pubtime"]')
date = self.string_to_date(page_date, "Published %Y-%m-%d %H:%M:%S")
self.page_cache[url] = [page, date]
self.page_cache[url] = (page, date)
return self.page_cache[url]

def crawl_helper(self, short_name, pub_date):
Expand Down
3 changes: 2 additions & 1 deletion comics/browser/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import json
from typing import Optional

from django.conf import settings
from django.contrib.auth.decorators import login_required
Expand Down Expand Up @@ -142,7 +143,7 @@ def get_view_type(self):
class ReleaseDateMixin(ReleaseMixin):
"""Things common for all *date based* views"""

date_field = "pub_date"
date_field: Optional[str] = "pub_date"
month_format = "%m"


Expand Down
17 changes: 10 additions & 7 deletions comics/core/comic_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime
import logging
from dataclasses import dataclass
from typing import Optional

from comics.comics import get_comic_module, get_comic_module_names
from comics.core.exceptions import ComicDataError
Expand All @@ -8,17 +10,18 @@
logger = logging.getLogger("comics.core.comic_data")


@dataclass
class ComicDataBase:
# Required values
name = None
language = None
url = None
name: str
language: str
url: str

# Default values
active = True
start_date = None
end_date = None
rights = ""
active: bool = True
start_date: Optional[str] = None
end_date: Optional[str] = None
rights: str = ""

@property
def slug(self):
Expand Down
2 changes: 1 addition & 1 deletion comics/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
]

if DEBUG:
TEMPLATES[0]["OPTIONS"]["loaders"] = [
TEMPLATES[0]["OPTIONS"]["loaders"] = [ # type: ignore
"django.template.loaders.app_directories.Loader",
]

Expand Down
59 changes: 58 additions & 1 deletion poetry.lock

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

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ sentry-sdk = "^1.0.0"
black = "^20.8b1"
django-debug-toolbar = "^3.2"
django-extensions = "^3.1.1"
django-stubs = "^1.7.0"
flake8 = "^3.9.0"
flake8-black = "^0.2.1"
flake8-bugbear = "^21.3.2"
flake8-isort = "^4.0.0"
isort = "^5.8.0"
mypy = "^0.812"
pep8-naming = "^0.11.1"
pytest = "^6.2.2"
pytest-cov = "^2.11.1"
Expand Down
36 changes: 36 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,39 @@ ignore =
E501
# W503: line break before binary operator (not PEP8 compliant)
W503


[mypy]
strict_optional = True
plugins =
mypy_django_plugin.main

[mypy.plugins.django-stubs]
django_settings_module = "comics.settings"

[mypy-comics.*.migrations.*]
ignore_errors = True

[mypy-debug_toolbar.*]
ignore_missing_imports = True

[mypy-django_extensions.*]
ignore_missing_imports = True

[mypy-environ.*]
ignore_missing_imports = True

[mypy-feedparser.*]
ignore_missing_imports = True

[mypy-invitations.*]
ignore_missing_imports = True

[mypy-lxml.*]
ignore_missing_imports = True

[mypy-PIL.*]
ignore_missing_imports = True

[mypy-tastypie.*]
ignore_missing_imports = True
7 changes: 6 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tox]
envlist = py37, docs, flake8
envlist = py37, docs, flake8, mypy
skipsdist = true

[testenv]
Expand All @@ -23,3 +23,8 @@ commands =
commands =
poetry install
poetry run python -m flake8

[testenv:mypy]
commands =
poetry install
poetry run python -m mypy comics

0 comments on commit 63c4d0a

Please sign in to comment.