Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable mypy type checking #44

Merged
merged 13 commits into from
Jun 24, 2021
9 changes: 8 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ repos:
name: pylint
entry: poetry run pylint -j 0 autobisect
pass_filenames: false
language: system
language: system
- repo: local
hooks:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two lines are unneccessary

- id: system
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id should be mypy

name: mypy
entry: poetry run mypy autobisect
pass_filenames: false
language: system
7 changes: 3 additions & 4 deletions autobisect/bisect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import logging
import random
from datetime import datetime, timedelta
from enum import Enum
from pathlib import Path
from typing import Union, Generator
from typing import Generator, Optional

import requests
from fuzzfetch import (
Expand Down Expand Up @@ -145,7 +144,7 @@ def __init__(
flags: BuildFlags,
platform: Platform,
find_fix: bool = False,
config: Union[Path, None] = None,
config: Optional[Path] = None,
):
"""
Instantiate bisection object
Expand Down Expand Up @@ -250,7 +249,7 @@ def build_iterator(
"""
while build_range:
if random_choice:
build = random.choice(build_range)
build = build_range.random
else:
build = build_range.mid_point

Expand Down
24 changes: 6 additions & 18 deletions autobisect/build_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import time
from contextlib import contextmanager
from pathlib import Path
from typing import Union, List
from typing import List, Optional, Iterator

from fuzzfetch import Fetcher

Expand All @@ -23,16 +23,6 @@ class DatabaseManager(object):
"""

def __init__(self, db_path: Path) -> None:
self.con = None
self.cur = None
self.open(str(db_path))

def open(self, db_path: str) -> None:
"""
Opens and initializes the sqlite3 database
:param db_path: Path to the sqlite3 database
:type db_path: str
"""
self.con = sqlite3.connect(db_path)
self.cur = self.con.cursor()
self.cur.execute("CREATE TABLE IF NOT EXISTS in_use (build_path, pid INT)")
Expand All @@ -57,8 +47,8 @@ class BuildManager(object):
A class for managing downloaded builds
"""

def __init__(self, config: Union[Path, None] = None) -> None:
self.config = config or BisectionConfig()
def __init__(self, config: Optional[Path] = None) -> None:
self.config = BisectionConfig(config)
self.build_dir = self.config.store_path / "builds"
if not Path.is_dir(self.build_dir):
self.build_dir.mkdir(parents=True)
Expand All @@ -71,15 +61,13 @@ def current_build_size(self) -> int:
"""
Recursively enumerate the size of the supplied build
"""
return sum(
[os.path.getsize(f) for f in self.build_dir.rglob("*") if os.path.isfile(f)]
)
return sum([os.path.getsize(f) for f in self.build_dir.rglob("*")])

def enumerate_builds(self) -> List[Path]:
"""
Enumerate all available builds including their size and stats
"""
builds = [os.fspath(x) for x in self.build_dir.iterdir() if x.is_dir()]
builds = [x for x in self.build_dir.iterdir() if x.is_dir()]
return sorted(builds, key=lambda b: os.stat(b).st_atime)

def remove_old_builds(self) -> None:
Expand All @@ -105,7 +93,7 @@ def remove_old_builds(self) -> None:
time.sleep(0.1)

@contextmanager
def get_build(self, build: Fetcher, target: str) -> Path:
def get_build(self, build: Fetcher, target: str) -> Iterator[Path]:
"""
Retrieve the build matching the supplied revision
:param build: A fuzzFetch.Fetcher build object
Expand Down
20 changes: 11 additions & 9 deletions autobisect/builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
import logging
import random
from datetime import timedelta, datetime
from typing import List, Any, Union
from typing import List, Union, TypeVar, Generic

LOG = logging.getLogger("builds")

T = TypeVar("T")

class BuildRange(object):

class BuildRange(Generic[T]):
"""
A class for storing a range of builds or build representations
"""

def __init__(self, builds: List[Any]):
def __init__(self, builds: List[T]):
"""
Instantiate a new instance

Expand All @@ -31,7 +33,7 @@ def __init__(self, builds: List[Any]):
def __len__(self) -> int:
return len(self._builds)

def __getitem__(self, expr: int) -> "BuildRange":
def __getitem__(self, expr: int) -> T:
if isinstance(expr, slice):
new_range = copy.copy(self)
new_range._builds = self._builds[expr]
Expand All @@ -40,14 +42,14 @@ def __getitem__(self, expr: int) -> "BuildRange":
return self._builds[expr]

@property
def builds(self) -> List[Any]:
def builds(self) -> List[T]:
"""
Returns the "builds" list
"""
return self._builds

@property
def mid_point(self) -> Union[Any, None]:
def mid_point(self) -> Union[T, None]:
"""
Returns the midpoint of the "builds" list
"""
Expand All @@ -57,7 +59,7 @@ def mid_point(self) -> Union[Any, None]:
return None

@property
def random(self) -> Union[Any, None]:
def random(self) -> Union[T, None]:
"""
Select a random index
"""
Expand All @@ -66,7 +68,7 @@ def random(self) -> Union[Any, None]:

return None

def index(self, build: Any) -> Union[int, None]:
def index(self, build: T) -> Union[int, None]:
"""
Returns the index of the provided build
:param build: An object within the "builds" list
Expand All @@ -91,4 +93,4 @@ def new(cls, start: datetime, end: datetime) -> "BuildRange":
for offset in range(delta.days + 1):
dates.append((start + timedelta(days=offset)).strftime("%Y-%m-%d"))

return cls(dates)
return cls(dates) # type: ignore
Empty file added autobisect/py.typed
Empty file.
2 changes: 1 addition & 1 deletion autobisect/tests/test_bisect.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def test_build_iterator_random(mocker):
builds = BuildRange([])
for _ in range(1, 4):
builds._builds.append(mocker.Mock(spec=Fetcher))
spy = mocker.patch("autobisect.bisect.random.choice", side_effect=builds)
spy = mocker.patch("autobisect.builds.random.choice", side_effect=builds)

bisector = MockBisector(datetime.now(), datetime.now())
generator = bisector.build_iterator(builds, True)
Expand Down
Loading