Skip to content

Commit

Permalink
refactor: Remove mypy workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Sep 1, 2022
1 parent 6da68a9 commit 128e3a4
Showing 1 changed file with 1 addition and 45 deletions.
46 changes: 1 addition & 45 deletions project/duties.py.jinja
Expand Up @@ -4,8 +4,6 @@ import importlib
import os
import re
import sys
import tempfile
from contextlib import suppress
from io import StringIO
from pathlib import Path
from typing import List, Optional, Pattern
Expand Down Expand Up @@ -196,49 +194,7 @@ def check_types(ctx): # noqa: WPS231
Arguments:
ctx: The context instance (passed automatically).
"""
# NOTE: the following code works around this issue:
# https://github.com/python/mypy/issues/10633

# compute packages directory path
py = f"{sys.version_info.major}.{sys.version_info.minor}"
pkgs_dir = Path("__pypackages__", py, "lib").resolve()

# build the list of available packages
packages = {}
for package in pkgs_dir.glob("*"):
if package.suffix not in {".dist-info", ".pth"} and package.name != "__pycache__":
packages[package.name] = package

# handle .pth files
for pth in pkgs_dir.glob("*.pth"):
with suppress(OSError):
for package in Path(pth.read_text().splitlines()[0]).glob("*"): # noqa: WPS440
if package.suffix != ".dist-info":
packages[package.name] = package

# create a temporary directory to assign to MYPYPATH
with tempfile.TemporaryDirectory() as tmpdir:

# symlink the stubs
ignore = set()
for stubs in (path for name, path in packages.items() if name.endswith("-stubs")): # noqa: WPS335
Path(tmpdir, stubs.name).symlink_to(stubs, target_is_directory=True)
# try to symlink the corresponding package
# see https://www.python.org/dev/peps/pep-0561/#stub-only-packages
pkg_name = stubs.name.replace("-stubs", "")
if pkg_name in packages:
ignore.add(pkg_name)
Path(tmpdir, pkg_name).symlink_to(packages[pkg_name], target_is_directory=True)

# create temporary mypy config to ignore stubbed packages
newconfig = Path("config", "mypy.ini").read_text()
newconfig += "\n" + "\n\n".join(f"[mypy-{pkg}.*]\nignore_errors=true" for pkg in ignore)
tmpconfig = Path(tmpdir, "mypy.ini")
tmpconfig.write_text(newconfig)

# set MYPYPATH and run mypy
os.environ["MYPYPATH"] = tmpdir
ctx.run(f"mypy --config-file {tmpconfig} {PY_SRC}", title="Type-checking", pty=PTY)
ctx.run(f"mypy --config-file config/mypy.ini {PY_SRC}", title="Type-checking", pty=PTY)


@duty(silent=True)
Expand Down

0 comments on commit 128e3a4

Please sign in to comment.