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

Add mypy checks #3229

Merged
merged 2 commits into from
Nov 7, 2017
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
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ sudo: required

language: python
python:
- "3.6"
- "3.5"

service:
- docker
Expand Down Expand Up @@ -77,6 +77,9 @@ jobs:
- docker-compose --version
- python3 -m pip install --requirement=travis/requirements.txt
script:
- python3 -m compileall commons/c2cgeoportal_commons
- python3 -m compileall geoportal/c2cgeoportal_geoportal
- python3 -m compileall admin/c2cgeoportal_admin
- docker build --tag camptocamp/geomapfish-build-dev:${MAJOR_VERSION} docker/build
- docker tag camptocamp/geomapfish-build-dev:${MAJOR_VERSION} camptocamp/geomapfish-build-dev-travis:${TRAVIS_BUILD_NUMBER}
- ./docker-run travis/empty-make.sh help
Expand Down
1 change: 1 addition & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dockerBuild {
sh './docker-run make admin/c2cgeoportal_admin/locale/c2cgeoportal_admin.pot'
// lint
sh './docker-run make flake8'
sh './docker-run make mypy'
sh './docker-run make git-attributes'
sh './docker-run make quote'
sh './docker-run make spell'
Expand Down
13 changes: 12 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ build: $(MAKO_FILES:.mako=) \
doc: $(BUILD_DIR)/sphinx.timestamp

.PHONY: checks
checks: flake8 git-attributes quote spell yamllint
checks: flake8 mypy git-attributes quote spell yamllint

.PHONY: clean
clean:
Expand Down Expand Up @@ -166,6 +166,17 @@ flake8:
--copyright-min-file-size=1 \
--copyright-regexp="Copyright \(c\) ([0-9][0-9][0-9][0-9]-)?$(shell date +%Y), Camptocamp SA"

.PHONY: mypy
mypy:
MYPYPATH=/usr/local/lib/python3.6/site-packages:/c2cwsgiutils \
Copy link
Member

@gberaudo gberaudo Nov 7, 2017

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Why?

Copy link
Member Author

@sbrunner sbrunner Nov 7, 2017

Choose a reason for hiding this comment

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

It's use on Docker who are based on Python 3.6 ...

Copy link
Member

Choose a reason for hiding this comment

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

Ah OK, I missed you are only calling the mypy target inside docker.

mypy --disallow-untyped-defs --strict-optional --follow-imports skip \
commons/c2cgeoportal_commons
# TODO: add --disallow-untyped-defs
MYPYPATH=/usr/local/lib/python3.6/site-packages/ \
mypy --ignore-missing-imports --strict-optional --follow-imports skip \
geoportal/c2cgeoportal_geoportal \
admin/c2cgeoportal_admin \

.PHONY: git-attributes
git-attributes:
git --no-pager diff --check `git log --oneline | tail -1 | cut --fields=1 --delimiter=' '`
Expand Down
42 changes: 37 additions & 5 deletions admin/acceptance_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
import pytest
import transaction
from pyramid import testing

from pyramid.paster import bootstrap
from pyramid.view import view_config
from webtest import TestApp
from wsgiref.simple_server import make_server
import threading

from c2cgeoportal_commons.tests import ( # noqa: F401
dbsession,
transact,
raise_db_error_on_query,
)
from c2cgeoportal_commons.scripts.initializedb import init_db
from c2cgeoportal_commons.models import get_engine, get_session_factory, get_tm_session, generate_mappers
from sqlalchemy.exc import DBAPIError


@pytest.fixture(scope='session')
@pytest.mark.usefixtures("settings")
def dbsession(settings):
generate_mappers(settings)
engine = get_engine(settings)
init_db(engine, force=True)
session_factory = get_session_factory(engine)
session = get_tm_session(session_factory, transaction.manager)
return session


@pytest.fixture(scope='function')
@pytest.mark.usefixtures("dbsession")
def transact(dbsession):
t = dbsession.begin_nested()
yield
t.rollback()


def raise_db_error(Table):
raise DBAPIError('this is a test !', None, None)


@pytest.fixture(scope='function')
@pytest.mark.usefixtures("dbsession")
def raise_db_error_on_query(dbsession):
query = dbsession.query
dbsession.query = raise_db_error
yield
dbsession.query = query


@pytest.fixture(scope="session")
Expand Down
24 changes: 22 additions & 2 deletions commons/acceptance_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import pytest

from c2cgeoportal_commons.tests import dbsession # noqa: F401
from c2cgeoportal_commons.tests import transact # noqa: F401
import transaction
from c2cgeoportal_commons.scripts.initializedb import init_db
from c2cgeoportal_commons.models import get_engine, get_session_factory, get_tm_session, generate_mappers


@pytest.fixture(scope='session')
@pytest.mark.usefixtures("settings")
def dbsession(settings):
generate_mappers(settings)
engine = get_engine(settings)
init_db(engine, force=True)
session_factory = get_session_factory(engine)
session = get_tm_session(session_factory, transaction.manager)
return session


@pytest.fixture(scope='function')
@pytest.mark.usefixtures("dbsession")
def transact(dbsession):
t = dbsession.begin_nested()
yield
t.rollback()


@pytest.fixture(scope='session')
Expand Down
9 changes: 6 additions & 3 deletions commons/c2cgeoportal_commons/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""c2cgeoportal_commons package."""
from typing import Optional # noqa, pylint: disable=unused-import

from pyramid.config import Configurator

from c2cgeoportal_commons.models import ( # noqa
get_session_factory,
Expand All @@ -8,11 +11,11 @@
init_dbsessions,
)

schema = None
srid = None
schema = None # type: Optional[str]
srid = None # type: Optional[str]


def includeme(config):
def includeme(config: Configurator) -> None:
"""
Initialize the model for a Pyramid app.
Activate this setup using ``config.include('c2cgeoportal_admin.commons')``.
Expand Down
32 changes: 20 additions & 12 deletions commons/c2cgeoportal_commons/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,41 @@

import re
import logging
from typing import Optional, Dict, Any

from c2cwsgiutils.health_check import HealthCheck
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
import sqlalchemy.ext.declarative
from sqlalchemy.orm import sessionmaker, configure_mappers
from sqlalchemy.engine import Engine
from sqlalchemy.orm import sessionmaker, configure_mappers, Session
import sqlalchemy.ext.declarative.api
import zope.sqlalchemy
import c2cwsgiutils
from transaction import TransactionManager
import c2cgeoportal_commons.models # "7/11/17, fix ci build, avoid use of globals"

LOG = logging.getLogger(__name__)

DBSession = None # Initialized by init_dbsessions
Base = sqlalchemy.ext.declarative.declarative_base()
DBSessions = {}
Base = sqlalchemy.ext.declarative.declarative_base() # type: sqlalchemy.ext.declarative.api.Base
DBSessions = {} # type: Dict[str, Session]

srid = None
schema = None
srid = None # type: Optional[str]
schema = None # type: Optional[str]


def get_engine(settings, prefix='sqlalchemy.'):
def get_engine(settings: dict, prefix: str='sqlalchemy.') -> Engine:
return engine_from_config(settings, prefix)


def get_session_factory(engine):
def get_session_factory(engine: Engine) -> sessionmaker:
factory = sessionmaker()
factory.configure(bind=engine)
return factory


def get_tm_session(session_factory, transaction_manager):
def get_tm_session(session_factory: sessionmaker, transaction_manager: TransactionManager) -> Session:
"""
Get a ``sqlalchemy.orm.Session`` instance backed by a transaction.

Expand All @@ -84,14 +91,14 @@ def get_tm_session(session_factory, transaction_manager):
return dbsession


def generate_mappers(settings):
def generate_mappers(settings: dict) -> None:
"""
Initialize the model for a Pyramid app.
"""
global schema
schema = settings.get('schema')
schema = settings['schema']
global srid
srid = settings.get('srid')
srid = settings['srid']

# import or define all models here to ensure they are attached to the
# Base.metadata prior to any initialization routines
Expand All @@ -101,7 +108,7 @@ def generate_mappers(settings):
# all relationships can be setup
configure_mappers()

def init_dbsessions(settings, config=None, health_check=None):
def init_dbsessions(settings: dict, config: Configurator=None, health_check: HealthCheck=None) -> None:
# define the srid, schema as global variables to be usable in the model
global schema
global srid
Expand All @@ -123,6 +130,7 @@ def init_dbsessions(settings, config=None, health_check=None):
c2cgeoportal_commons.models.DBSessions[dbsession_name] = \
c2cwsgiutils.db.create_session(config, dbsession_name, **dbsession_config)

Base.metadata.clear()
from c2cgeoportal_commons.models import main

if health_check is not None:
Expand Down
Loading