Static type checking with mypy

This does introduce some not-so-nice things, like having to annotate each
`__init__` function with `-> None`. However, the benefits of having static
type checking in a complex bit of software like BAT outweigh the downsides.
This commit is contained in:
Sybren A. Stüvel
2018-03-09 11:09:47 +01:00
parent 632d01334c
commit fdbbc3a20d
18 changed files with 117 additions and 82 deletions
+2 -2
View File
@@ -13,8 +13,8 @@ from . import result, modifier_walkers
log = logging.getLogger(__name__)
_warned_about_types = set()
_funcs_for_code = {}
_warned_about_types = set() # type: typing.Set[bytes]
_funcs_for_code = {} # type: typing.Dict[bytes, typing.Callable]
def iter_assets(block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
+1 -1
View File
@@ -11,7 +11,7 @@ from blender_asset_tracer.blendfile import iterators
# Don't warn about these types at all.
_warned_about_types = {b'LI', b'DATA'}
_funcs_for_code = {}
_funcs_for_code = {} # type: typing.Dict[bytes, typing.Callable]
log = logging.getLogger(__name__)
+6 -5
View File
@@ -7,12 +7,13 @@ blend files.
"""
import collections
import logging
import pathlib
import typing
from blender_asset_tracer import blendfile, bpathlib
from . import expanders
_funcs_for_code = {}
_funcs_for_code = {} # type: typing.Dict[bytes, typing.Callable]
log = logging.getLogger(__name__)
@@ -23,16 +24,16 @@ class _BlockIterator:
without having to pass those variables to each recursive call.
"""
def __init__(self):
def __init__(self) -> None:
# Set of (blend file Path, block address) of already-reported blocks.
self.blocks_yielded = set()
self.blocks_yielded = set() # type: typing.Set[typing.Tuple[pathlib.Path, int]]
# Queue of blocks to visit
self.to_visit = collections.deque()
self.to_visit = collections.deque() # type: typing.Deque[blendfile.BlendFileBlock]
def iter_blocks(self,
bfile: blendfile.BlendFile,
limit_to: typing.Set[blendfile.BlendFileBlock] = frozenset(),
limit_to: typing.Set[blendfile.BlendFileBlock] = set(),
) -> typing.Iterator[blendfile.BlendFileBlock]:
"""Expand blocks with dependencies from other libraries."""
+1 -1
View File
@@ -8,7 +8,7 @@ log = logging.getLogger(__name__)
class DoesNotExist(OSError):
"""Indicates a path does not exist on the filesystem."""
def __init__(self, path: pathlib.Path):
def __init__(self, path: pathlib.Path) -> None:
super().__init__(path)
self.path = path
@@ -53,7 +53,8 @@ def _modifier_particle_system(modifier: blendfile.BlendFileBlock, block_name: by
if flag & cdefs.PTCACHE_EXTERNAL:
path, field = pointcache.get(b'path', return_field=True)
yield result.BlockUsage(pointcache, path, path_full_field=field,
bpath = bpathlib.BlendPath(path)
yield result.BlockUsage(pointcache, bpath, path_full_field=field,
is_sequence=True, block_name=block_name)
+6 -4
View File
@@ -40,8 +40,8 @@ class BlockUsage:
path_full_field: dna.Field = None,
path_dir_field: dna.Field = None,
path_base_field: dna.Field = None,
block_name: bytes = '',
):
block_name: bytes = b'',
) -> None:
if block_name:
self.block_name = block_name
else:
@@ -69,7 +69,9 @@ class BlockUsage:
self.path_full_field = path_full_field
self.path_dir_field = path_dir_field
self.path_base_field = path_base_field
self._abspath = None # cached by __fspath__()
# cached by __fspath__()
self._abspath = None # type: pathlib.Path
@staticmethod
def guess_block_name(block: blendfile.BlendFileBlock) -> bytes:
@@ -134,7 +136,7 @@ class BlockUsage:
raise NotImplemented()
return self.block_name < other.block_name and self.block < other.block
def __eq__(self, other: 'BlockUsage'):
def __eq__(self, other: object):
if not isinstance(other, BlockUsage):
return False
return self.block_name == other.block_name and self.block == other.block