Skip to content

Commit

Permalink
Enable type checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed May 22, 2020
1 parent 755088a commit 2f933bd
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 20 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ script:

# Style
- make style
- make typing

- if [ $PURE = true ]; then SETUP_ARGS=--pure; fi
- python setup.py $SETUP_ARGS bdist_wheel
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ check-noextensions:: clean

check-all: check check-pypy check-noextensions

typing:
mypy dulwich

clean::
$(SETUP) clean --all
rm -f dulwich/*.so
Expand Down
6 changes: 5 additions & 1 deletion dulwich/diff_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ def changes_with_renames(self, tree1_id, tree2_id, want_unchanged=False,
_count_blocks_py = _count_blocks
try:
# Try to import C versions
from dulwich._diff_tree import _is_tree, _merge_entries, _count_blocks
from dulwich._diff_tree import ( # type: ignore
_is_tree,
_merge_entries,
_count_blocks,
)
except ImportError:
pass
17 changes: 13 additions & 4 deletions dulwich/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
import os
import posixpath
import stat
from typing import (
Optional,
Dict,
Union,
Type,
)
import warnings
import zlib
from hashlib import sha1
Expand Down Expand Up @@ -146,13 +152,13 @@ def filename_to_hex(filename):
return hex


def object_header(num_type, length):
def object_header(num_type: int, length: int) -> bytes:
"""Return an object header for the given numeric type and text length."""
return (object_class(num_type).type_name +
b' ' + str(length).encode('ascii') + b'\0')


def serializable_property(name, docstring=None):
def serializable_property(name: str, docstring:Optional[str]=None):
"""A property that helps tracking whether serialization is necessary.
"""
def set(obj, value):
Expand Down Expand Up @@ -253,6 +259,9 @@ class ShaFile(object):

__slots__ = ('_chunked_text', '_sha', '_needs_serialization')

type_name:bytes
type_num:int

@staticmethod
def _parse_legacy_object_header(magic, f):
"""Parse a legacy object, creating it but not reading the file."""
Expand Down Expand Up @@ -1417,7 +1426,7 @@ def _get_extra(self):
Tag,
)

_TYPE_MAP = {}
_TYPE_MAP:Dict[Union[bytes,int],Type[ShaFile]] = {}

for cls in OBJECT_CLASSES:
_TYPE_MAP[cls.type_name] = cls
Expand All @@ -1429,6 +1438,6 @@ def _get_extra(self):
_sorted_tree_items_py = sorted_tree_items
try:
# Try to import C versions
from dulwich._objects import parse_tree, sorted_tree_items
from dulwich._objects import parse_tree, sorted_tree_items # type: ignore
except ImportError:
pass
19 changes: 8 additions & 11 deletions dulwich/pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@
import struct

from itertools import chain
try:
from itertools import imap, izip
except ImportError:
# Python3
imap = map
izip = zip

import os
import sys
Expand Down Expand Up @@ -363,8 +357,8 @@ def __eq__(self, other):
if not isinstance(other, PackIndex):
return False

for (name1, _, _), (name2, _, _) in izip(self.iterentries(),
other.iterentries()):
for (name1, _, _), (name2, _, _) in zip(self.iterentries(),
other.iterentries()):
if name1 != name2:
return False
return True
Expand All @@ -378,7 +372,7 @@ def __len__(self):

def __iter__(self):
"""Iterate over the SHAs in this pack."""
return imap(sha_to_hex, self._itersha())
return map(sha_to_hex, self._itersha())

def iterentries(self):
"""Iterate over the entries in this pack index.
Expand Down Expand Up @@ -710,7 +704,7 @@ def chunks_length(chunks):
if isinstance(chunks, bytes):
return len(chunks)
else:
return sum(imap(len, chunks))
return sum(map(len, chunks))


def unpack_object(read_all, read_some=None, compute_crc32=False,
Expand Down Expand Up @@ -2094,6 +2088,9 @@ def keep(self, msg=None):


try:
from dulwich._pack import apply_delta, bisect_find_sha # noqa: F811
from dulwich._pack import ( # type: ignore # noqa: F811
apply_delta,
bisect_find_sha,
)
except ImportError:
pass
3 changes: 2 additions & 1 deletion dulwich/tests/compat/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import threading
from wsgiref import simple_server
import sys
from typing import Tuple

from dulwich.server import (
DictBackend,
Expand Down Expand Up @@ -87,7 +88,7 @@ class SmartWebTestCase(WebTests, CompatTestCase):
This server test case does not use side-band-64k in git-receive-pack.
"""

min_git_version = (1, 6, 6)
min_git_version:Tuple[int, ...] = (1, 6, 6)

def _handlers(self):
return {b'git-receive-pack': NoSideBand64kReceivePackHandler}
Expand Down
3 changes: 2 additions & 1 deletion dulwich/tests/compat/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import sys
import tempfile
import time
from typing import Tuple

from dulwich.repo import Repo
from dulwich.protocol import TCP_GIT_PORT
Expand Down Expand Up @@ -215,7 +216,7 @@ class CompatTestCase(TestCase):
min_git_version.
"""

min_git_version = (1, 5, 0)
min_git_version:Tuple[int, ...] = (1, 5, 0)

def setUp(self):
super(CompatTestCase, self).setUp()
Expand Down
2 changes: 1 addition & 1 deletion dulwich/tests/test_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
try:
from mock import patch
except ImportError:
patch = None
patch = None # type: ignore


class ArchiveTests(TestCase):
Expand Down
3 changes: 2 additions & 1 deletion dulwich/tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import gzip
import re
import os
from typing import Type

from dulwich.object_store import (
MemoryObjectStore,
Expand Down Expand Up @@ -107,7 +108,7 @@ def cache_forever(self):
class WebTestCase(TestCase):
"""Base TestCase with useful instance vars and utility functions."""

_req_class = TestHTTPGitRequest
_req_class:Type[HTTPGitRequest] = TestHTTPGitRequest

def setUp(self):
super(WebTestCase, self).setUp()
Expand Down
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
[flake8]
exclude = build,.git,build-pypy,.tox

[mypy]
ignore_missing_imports = True

0 comments on commit 2f933bd

Please sign in to comment.