Skip to content

Commit

Permalink
Replace mypy with pyright
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyfedoseev committed Aug 22, 2023
1 parent 9dfee84 commit f346055
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ jobs:
- name: build
run: make

- name: mypy
run: make mypy
- name: check-types
run: make check-types

- name: tests
run: env TOX_PARALLEL_NO_SPINNER=1 make test
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM andreyfedoseev/django-static-precompiler:20.04-1
FROM andreyfedoseev/django-static-precompiler:22.04-1
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Etc/UTC
RUN apt update && \
Expand All @@ -7,6 +7,7 @@ RUN apt update && \
apt install -y \
python3-venv \
python3.8-dev \
python3.8-distutils \
python3.9-dev \
python3.9-distutils \
python3.10-dev \
Expand All @@ -25,6 +26,7 @@ RUN python3 -m venv $POETRY_HOME && \
RUN mkdir /app
WORKDIR /app
ADD poetry.lock pyproject.toml /app/
RUN poetry install --all-extras --no-root --no-interaction
RUN poetry install --all-extras --no-root --no-interaction && \
pyright --version # this is to force pyright to install its dependencies
ADD . /app/
RUN poetry install --all-extras --no-interaction
2 changes: 1 addition & 1 deletion Dockerfile-base
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:20.04
FROM ubuntu:22.04
MAINTAINER Andrey Fedoseev <andrey@andreyfedoseev.com>
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update && apt install -y autoconf libtool npm wget
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ shell:
test:
docker compose run --rm app tox -p auto

mypy:
docker compose run --rm app mypy --strict ./src
check-types:
docker compose run --rm app pyright

package:
rm -rf ./dist
Expand Down
52 changes: 50 additions & 2 deletions poetry.lock

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

28 changes: 11 additions & 17 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ packages = [
{include = "static_precompiler", from = "src"}
]
include = [
"AUTHORS",
"README.md",
"CHANGES.md",
"LICENSE",
"py.typed",
{path = "AUTHORS"},
{path = "README.md"},
{path = "CHANGES.md"},
{path = "LICENSE"},
{path = "py.typed"},
{ path = "tests", format = "sdist" }
]

Expand All @@ -50,8 +50,8 @@ pytest-cov = "~4.0.0"
watchdog = "~3.0.0"
coverage = "~7.3.0"
tox = "~4.9.0"
mypy = "~1.4.0"
django-stubs = {extras = ["compatible-mypy"], version = "~4.2.3"}
pyright = "^1.1.323"
django-stubs = "~4.2.3"

[tool.poetry.extras]
libsass = ["libsass"]
Expand All @@ -73,13 +73,7 @@ target-version = "py38"
[tool.ruff.isort]
known-first-party = ["static_precompiler"]

[tool.mypy]
python_version = "3.8"
plugins = ["mypy_django_plugin.main"]

[[tool.mypy.overrides]]
module = "pretend"
ignore_missing_imports = true

[tool.django-stubs]
django_settings_module = "static_precompiler.mypy_django_settings"
[tool.pyright]
include = ["src"]
exclude = ["__pycache__"]
pythonVersion = "3.8"
8 changes: 3 additions & 5 deletions src/static_precompiler/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@
from typing import Optional

import django.core.cache
import django.utils.encoding
from django.core.cache import BaseCache

from . import settings


def get_cache() -> BaseCache:
def get_cache() -> django.core.cache.BaseCache:
if settings.CACHE_NAME:
return django.core.cache.caches.get(settings.CACHE_NAME) # type: ignore
return django.core.cache.cache
return django.core.cache.cache # type: ignore


def get_cache_key(key: str) -> str:
return f"static_precompiler.{socket.gethostname()}.{key}"


def get_hexdigest(plaintext: str, length: Optional[int] = None) -> str:
digest = hashlib.md5(django.utils.encoding.smart_bytes(plaintext)).hexdigest()
digest = hashlib.md5(plaintext.encode()).hexdigest()
if length:
return digest[:length]
return digest
2 changes: 1 addition & 1 deletion src/static_precompiler/compilers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_full_source_path(self, source_path: str) -> str:
return full_path

with contextlib.suppress(django.core.exceptions.SuspiciousOperation):
full_path = finders.find(norm_source_path)
full_path = finders.find(norm_source_path, all=False)

if full_path is None:
raise ValueError(f"Can't find staticfile named: {source_path}")
Expand Down
7 changes: 5 additions & 2 deletions src/static_precompiler/compilers/libsass.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ def compile_file(self, source_path: str) -> str:

try:
if self.is_sourcemap_enabled:
compiled, sourcemap = sass.compile(
result = sass.compile(
filename=full_source_path,
source_map_filename=sourcemap_path,
output_filename_hint=full_output_path,
include_paths=self.load_paths,
)
if result is None:
raise exceptions.StaticCompilationError(f"Could not compile {source_path}")
compiled, sourcemap = result
else:
compile_kwargs: Dict[str, Any] = {}
if self.load_paths:
Expand Down Expand Up @@ -78,7 +81,7 @@ def compile_file(self, source_path: str) -> str:

def compile_source(self, source: str) -> str:
try:
compiled: str = sass.compile(string=source, indented=self.indented, include_paths=self.load_paths)
compiled = sass.compile(string=source, indented=self.indented, include_paths=self.load_paths)
except sass.CompileError as e:
raise exceptions.StaticCompilationError("Could not compile source") from e

Expand Down
12 changes: 4 additions & 8 deletions src/static_precompiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import posixpath
import subprocess
from typing import Any, Dict, List, Optional, Tuple, Union
from typing import Any, Dict, List, Optional, Tuple

from django.utils import encoding

Expand Down Expand Up @@ -38,12 +38,11 @@ def normalize_whitespace(text: str) -> str:


# noinspection PyShadowingBuiltins
def run_command(
args: List[str], input: Optional[Union[bytes, str]] = None, cwd: Optional[str] = None
) -> Tuple[int, str, str]:
def run_command(args: List[str], input: Optional[str] = None, cwd: Optional[str] = None) -> Tuple[int, str, str]:
popen_kwargs: Dict[str, Any] = {
"stdout": subprocess.PIPE,
"stderr": subprocess.PIPE,
"universal_newlines": True,
}

if cwd is not None:
Expand All @@ -57,14 +56,11 @@ def run_command(

p = subprocess.Popen(args, **popen_kwargs)

if input:
input = encoding.smart_bytes(input)

output, error = p.communicate(input)
return_code = p.poll()
assert return_code is not None

return return_code, encoding.smart_str(output), encoding.smart_str(error)
return return_code, output, error


def compile_static(path: str) -> str:
Expand Down
3 changes: 0 additions & 3 deletions tests/test_base_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,6 @@ def test_compile_lazy(monkeypatch):
# noinspection PyUnresolvedReferences
assert compiler.compile.calls == [pretend.call("dummy.coffee")]

assert compiler.compile(encoding.force_str("foo")).startswith(encoding.force_str("")) is True
assert compiler.compile(encoding.force_bytes("foo")).startswith(encoding.force_bytes("")) is True


def test_find_dependencies():
compiler = compilers.BaseCompiler()
Expand Down
4 changes: 3 additions & 1 deletion tests/test_scss.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,9 @@ def test_precision(precision, monkeypatch, tmpdir):

with open(full_output_path) as compiled:
compiled_css = compiled.read()
line_height = re.search(r"line-height: (.+?);", compiled_css).groups()[0]
match = re.search(r"line-height: (.+?);", compiled_css)
assert match
line_height = match.groups()[0]
assert len(line_height.split(".")[-1]) == expected_precision


Expand Down

0 comments on commit f346055

Please sign in to comment.