Skip to content

Commit

Permalink
CI: Freeze DRF commit used in typecheck_tests and types-requests/type…
Browse files Browse the repository at this point in the history
…s-urllib3 (#345)

* Freeze DRF git commit hash used in CI.
* Freeze versions of types-requests and types-urllib3 since they are already incompatible with mypy 0.991.

typecheck_tests.py runs in CI. It makes a git checkout of the django-rest-framework (DRF) project and then runs mypy over its test suite together with our stubs. The idea is that DRF test suite provides some example code that we can verify our type stubs against.

Since upstream's test suite doesn't have proper type hints, there are bound to be lots of false positives where mypy's inference doesn't guess the intent of the developer, or hacks were made for testing purposes. So we have a massive IGNORED_ERRORS dict containing the false positives.

Since we took the latest master state of DRF repo, as upstream adds new tests, new false positives would appear at a random moment in some djangorestframework-stubs pull request and we'd have to look into them and usually add new ignores.
  • Loading branch information
intgr committed Feb 9, 2023
1 parent 4fb7adf commit 3a339b0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Expand Up @@ -5,6 +5,8 @@ pytest==7.2.1
pytest-mypy-plugins==1.10.1
djangorestframework==3.14.0
types-pytz==2022.7.1.0
types-requests==2.28.11.8
types-urllib3==1.26.25.4
django-stubs==1.13.0
django-stubs-ext==0.7.0
-e .[compatible-mypy,coreapi,markdown]
5 changes: 2 additions & 3 deletions scripts/git_helpers.py
@@ -1,4 +1,3 @@
from pathlib import Path
from typing import Optional

from git import RemoteProgress, Repo
Expand All @@ -14,7 +13,7 @@ def update(self, op_code, cur_count, max_count=None, message=""):
print(self._cur_line)


def checkout_target_tag(drf_version: Optional[str]) -> Path:
def git_checkout_drf(commit_ref: Optional[str] = None) -> None:
if not DRF_SOURCE_DIRECTORY.exists():
DRF_SOURCE_DIRECTORY.mkdir(exist_ok=True, parents=False)
repository = Repo.clone_from(
Expand All @@ -27,4 +26,4 @@ def checkout_target_tag(drf_version: Optional[str]) -> Path:
else:
repository = Repo(DRF_SOURCE_DIRECTORY)
repository.remote("origin").pull("master", progress=ProgressPrinter(), depth=100)
repository.git.checkout(drf_version or "master")
repository.git.checkout(commit_ref or "master")
4 changes: 2 additions & 2 deletions scripts/stubgen-drf.py
Expand Up @@ -3,7 +3,7 @@

from mypy.stubgen import generate_stubs, parse_options

from scripts.git_helpers import checkout_target_tag
from scripts.git_helpers import git_checkout_drf
from scripts.paths import DRF_SOURCE_DIRECTORY

if __name__ == "__main__":
Expand All @@ -12,6 +12,6 @@
args = parser.parse_args()
if DRF_SOURCE_DIRECTORY.exists():
shutil.rmtree(DRF_SOURCE_DIRECTORY)
checkout_target_tag(args.drf_version)
git_checkout_drf(args.drf_version)
stubgen_options = parse_options([f"{DRF_SOURCE_DIRECTORY}", "-o=stubgen"])
generate_stubs(stubgen_options)
8 changes: 6 additions & 2 deletions scripts/typecheck_tests.py
Expand Up @@ -7,9 +7,13 @@
from distutils import dir_util, spawn
from typing import Dict, List, Pattern, Union

from scripts.git_helpers import checkout_target_tag
from scripts.git_helpers import git_checkout_drf
from scripts.paths import DRF_SOURCE_DIRECTORY, PROJECT_DIRECTORY, STUBS_DIRECTORY

# django-rest-framework commit hash to check against (ignored with --drf_version)
# This should be updated periodically or after every DRF release.
DRF_GIT_REF = "22d206c1e0dbc03840c4d190f7eda537c0f2010a"

IGNORED_MODULES = []
MOCK_OBJECTS = [
"MockQueryset",
Expand Down Expand Up @@ -284,7 +288,7 @@ def update(self, op_code, cur_count, max_count=None, message=""):
parser = ArgumentParser()
parser.add_argument("--drf_version", required=False)
args = parser.parse_args()
checkout_target_tag(args.drf_version)
git_checkout_drf(args.drf_version or DRF_GIT_REF)
if sys.version_info[1] > 7:
shutil.copytree(STUBS_DIRECTORY, DRF_SOURCE_DIRECTORY / "rest_framework", dirs_exist_ok=True)
else:
Expand Down

0 comments on commit 3a339b0

Please sign in to comment.