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

Introduce mypy and enforce it on a few files #40

Merged
merged 2 commits into from
Jun 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ UNKNOWN.egg-info/

/.idea

/.mypy_cache
/.pytest_cache
/docs/html
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v1.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- id: flake8
exclude: ^docs/conf.py$
- repo: https://github.com/asottile/seed-isort-config
rev: v1.0.1
hooks:
- id: seed-isort-config
args: [--application-directories, '.:src']
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.4
hooks:
- id: isort
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.610-1
hooks:
- id: mypy
exclude: ^docs/conf.py$
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ matrix:
env: TOXENV=py37
- python: 3.6
env: TOXENV=doc_build
- python: 3.6
env: TOXENV=lint

install:
- pip install --upgrade pip
Expand All @@ -23,3 +25,8 @@ notifications:
irc:
channels:
- "chat.freenode.net#bandersnatch"

cache:
directories:
- $HOME/.cache/pip
- $HOME/.cache/pre-commit
4 changes: 2 additions & 2 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class DocStub(object):
if selected_theme not in available_themes:
selected_theme = available_themes[0]
print('SPHINX_THEME is not installed, using %r theme' % selected_theme)

html_theme = available_theme_settings[selected_theme]['theme']
html_theme_path = available_theme_settings[selected_theme]['path']
if available_theme_settings[selected_theme].get('options', None):
Expand Down Expand Up @@ -381,4 +381,4 @@ def setup(app):
'enable_eval_rst': True,
'enable_auto_doc_ref': True,
}, True)
app.add_transform(AutoStructify)
app.add_transform(AutoStructify)
16 changes: 16 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[mypy]

[mypy-bandersnatch.configuration]
disallow_untyped_defs = True

[mypy-bandersnatch.filter]
disallow_untyped_defs = True

[mypy-bandersnatch.log]
disallow_untyped_defs = True

[mypy-bandersnatch.release]
disallow_untyped_defs = True

[mypy-bandersnatch.util]
disallow_untyped_defs = True
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3

from setuptools import setup, find_packages
from setuptools import find_packages, setup

from src.bandersnatch import __version__

install_deps = [
Expand Down Expand Up @@ -45,7 +46,7 @@
update_stable_tag = bandersnatch.release:update_stable_tag
[zest.releaser.postreleaser.after]
update_requirements = bandersnatch.release:update_requirements
""",
""", # noqa
classifiers=[
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.6',
Expand Down
38 changes: 21 additions & 17 deletions src/bandersnatch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
#!/usr/bin/env python3
from typing import NamedTuple

from collections import namedtuple

# a namedtuple like that given by sys.version_info
__version_info__ = namedtuple(
'version_info',
'major minor micro releaselevel serial')(
major=3,
minor=0,
micro=0,
releaselevel='dev0',
serial=0 # Not currently in use below ...
)
class _VersionInfo(NamedTuple):
major: int
minor: int
micro: int
releaselevel: str
serial: int

if __version_info__.releaselevel:
__version__ = '{v.major}.{v.minor}.{v.micro}.{v.releaselevel}'.format(
v=__version_info__
)
else:
__version__ = '{v.major}.{v.minor}.{v.micro}'.format(v=__version_info__)
@property
def version_str(self) -> str:
extra = f'.{self.releaselevel}' if self.releaselevel else ''
return f'{self.major}.{self.minor}.{self.micro}.{extra}'


__version_info__ = _VersionInfo(
major=3,
minor=0,
micro=0,
releaselevel='dev0',
serial=0 # Not currently in use below ...
)
__version__ = __version_info__.version_str
3 changes: 2 additions & 1 deletion src/bandersnatch/buildout.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import os.path

import pkg_resources


class Requirements(object):
"""Generate a pip compatible requirements.txt from our working set based on
the 'bandersnatch' requirement."""

def __init__(self, buildout, name, options):
def __init__(self, buildout, name, options) -> None:
self.name, self.options = name, options
options['path'] = os.path.join(
buildout['buildout']['directory'], 'requirements.txt')
Expand Down
12 changes: 7 additions & 5 deletions src/bandersnatch/configuration.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""
Module containing classes to access the bandersnatch configuration file
"""
import pkg_resources
from configparser import ConfigParser
from typing import Any, Dict, Optional, Type

import pkg_resources


class Singleton(type): # pragma: no cover
_instances = {}
_instances: Dict['Singleton', Type] = {}

def __call__(cls, *args, **kwargs):
def __call__(cls, *args: Any, **kwargs: Any) -> Type:
if cls not in cls._instances:
cls._instances[cls] = super(
Singleton, cls
Expand All @@ -17,7 +19,7 @@ def __call__(cls, *args, **kwargs):


class BandersnatchConfig(metaclass=Singleton):
def __init__(self, config_file=None):
def __init__(self, config_file: Optional[str] = None) -> None:
"""
Bandersnatch configuration class singleton

Expand All @@ -35,7 +37,7 @@ def __init__(self, config_file=None):
self.config_file = config_file
self.load_configuration()

def load_configuration(self):
def load_configuration(self) -> None:
"""
Read the configuration from the configuration files
"""
Expand Down
22 changes: 12 additions & 10 deletions src/bandersnatch/filter.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""
Blacklist management
"""
import pkg_resources
from collections import defaultdict
from .configuration import BandersnatchConfig
from typing import Any, Dict, Iterable, List

import pkg_resources

from .configuration import BandersnatchConfig

loaded_filter_plugins = defaultdict(list)
loaded_filter_plugins: Dict[str, List['Filter']] = defaultdict(list)


class Filter(object):
Expand All @@ -15,17 +17,17 @@ class Filter(object):
"""
name = 'filter'

def __init__(self, *args, **kwargs):
def __init__(self, *args: Any, **kwargs: Any) -> None:
self.configuration = BandersnatchConfig().config
self.initialize_plugin()

def initialize_plugin(self):
def initialize_plugin(self) -> None:
"""
Code to initialize the plugin
"""
pass

def check_match(self, **kwargs):
def check_match(self, **kwargs: Any) -> bool:
"""
Check if the plugin matches based on the arguments provides.

Expand All @@ -51,7 +53,7 @@ class FilterReleasePlugin(Filter):
name = 'release_plugin'


def load_filter_plugins(entrypoint_group):
def load_filter_plugins(entrypoint_group: str) -> Iterable[Filter]:
"""
Load all blacklist plugins that are registered with pkg_resources

Expand All @@ -71,7 +73,7 @@ def load_filter_plugins(entrypoint_group):
try:
config_blacklist_plugins = config['blacklist']['plugins']
except KeyError:
config_blacklist_plugins = None
config_blacklist_plugins = ''
if config_blacklist_plugins:
config_plugins = []
for plugin in config_blacklist_plugins.split('\n'):
Expand All @@ -98,7 +100,7 @@ def load_filter_plugins(entrypoint_group):
return plugins


def filter_project_plugins():
def filter_project_plugins() -> Iterable[Filter]:
"""
Load and return the release filtering plugin objects

Expand All @@ -110,7 +112,7 @@ def filter_project_plugins():
return load_filter_plugins('bandersnatch_filter_plugins.project')


def filter_release_plugins():
def filter_release_plugins() -> Iterable[Filter]:
"""
Load and return the release filtering plugin objects

Expand Down
3 changes: 2 additions & 1 deletion src/bandersnatch/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# coverage analysis. Unfortunately this is really hard to test as the Python
# logging module won't allow reasonable teardown. :(
import logging
from typing import Any


def setup_logging(args):
def setup_logging(args: Any) -> logging.StreamHandler:
ch = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s')
Expand Down
13 changes: 7 additions & 6 deletions src/bandersnatch/main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import argparse
import bandersnatch.log
import bandersnatch.master
import bandersnatch.mirror
import bandersnatch.utils
import bandersnatch.verify
import configparser
import logging
import logging.config
import os.path
import shutil
from .configuration import BandersnatchConfig

import bandersnatch.log
import bandersnatch.master
import bandersnatch.mirror
import bandersnatch.utils
import bandersnatch.verify

from .configuration import BandersnatchConfig

logger = logging.getLogger(__name__)

Expand Down
4 changes: 3 additions & 1 deletion src/bandersnatch/master.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from .utils import USER_AGENT
import logging

import requests
import xmlrpc2

from .utils import USER_AGENT

logger = logging.getLogger(__name__)


Expand Down
11 changes: 6 additions & 5 deletions src/bandersnatch/mirror.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from .filter import filter_project_plugins
from .package import Package
from .utils import rewrite, USER_AGENT, update_safe
from packaging.utils import canonicalize_name
from threading import RLock
import asyncio
import concurrent.futures
import datetime
import fcntl
import logging
import os
from threading import RLock

from packaging.utils import canonicalize_name

from .filter import filter_project_plugins
from .package import Package
from .utils import USER_AGENT, rewrite, update_safe

logger = logging.getLogger(__name__)

Expand Down
15 changes: 8 additions & 7 deletions src/bandersnatch/package.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from . import utils
from .filter import filter_release_plugins
from .master import StalePage
from packaging.utils import canonicalize_name
from urllib.parse import urlparse, unquote
import hashlib
import json
import logging
import os.path
import pkg_resources
import requests
import sys
import time
from urllib.parse import unquote, urlparse

import pkg_resources
import requests
from packaging.utils import canonicalize_name

from . import utils
from .filter import filter_release_plugins
from .master import StalePage

logger = logging.getLogger(__name__)

Expand Down
8 changes: 5 additions & 3 deletions src/bandersnatch/release.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
from zest.releaser.utils import execute_command
import logging
import os
from typing import Dict

from zest.releaser.utils import execute_command

logger = logging.getLogger(__name__)


def update_requirements(data):
def update_requirements(data: Dict[str, str]) -> None:
os.chdir(data['workingdir'])
logger.info('Running buildout to update requirements.txt.')
execute_command('bin/buildout')
logger.info('Committing requirements.txt.')
execute_command('hg commit -v -m "Update requirements.txt"')


def update_stable_tag(data):
def update_stable_tag(data: Dict[str, str]) -> None:
os.chdir(data['workingdir'])
logger.info('Updating stable tag.')
execute_command('hg tag -f -r %s stable' % data['version'])
1 change: 1 addition & 0 deletions src/bandersnatch/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest.mock as mock

import pytest


Expand Down
Loading