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

Mypy linting #174

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
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
25 changes: 25 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# See https://pre-commit.com for more information
repos:
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
# Required to make flake8 read from pyproject.toml for now :(
additional_dependencies: ["flake8-pyproject"]
- repo: https://github.com/psf/black
rev: 23.9.1
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.6.0
hooks:
- id: mypy
language_version: "3.10"
args: [--explicit-package-bases,--config-file,pyproject.toml]
additional_dependencies:
[
"types-requests",
"sqlalchemy-stubs",
"types-pyYAML",
"types-pkg-resources",
]
DrPyser marked this conversation as resolved.
Show resolved Hide resolved
47 changes: 35 additions & 12 deletions bin/wazo-dird-init-db
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,41 @@ from xivo.user_rights import change_user

def _parse_cli_args(args):
parser = argparse.ArgumentParser()
parser.add_argument('--user', action='store',
help="The system user to use to connect to postgresql and create the user and database")
parser.add_argument('--pg_db_uri', action='store', default='postgresql:///postgres',
help="The DSN to connect to the postgres DB as an superuser")
parser.add_argument('--dird_db_uri', action='store', default='postgresql:///asterisk',
help="The DSN to connect to the dird DB as an superuser")
parser.add_argument('--db', action='store', default='asterisk',
help="The database name that will be created")
parser.add_argument('--owner', action='store', default='asterisk',
help="The database user that will be created and that will own the database")
parser.add_argument('--password', action='store', default='proformatique',
help="The password that will be assigned to the created user")
parser.add_argument(
'--user',
action='store',
help="The system user to use to connect to postgresql and create the user and database",
)
parser.add_argument(
'--pg_db_uri',
action='store',
default='postgresql:///postgres',
help="The DSN to connect to the postgres DB as an superuser",
)
parser.add_argument(
'--dird_db_uri',
action='store',
default='postgresql:///asterisk',
help="The DSN to connect to the dird DB as an superuser",
)
parser.add_argument(
'--db',
action='store',
default='asterisk',
help="The database name that will be created",
)
parser.add_argument(
'--owner',
action='store',
default='asterisk',
help="The database user that will be created and that will own the database",
)
parser.add_argument(
'--password',
action='store',
default='proformatique',
help="The password that will be assigned to the created user",
)
return parser.parse_args(args)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,39 @@ app = Flask(__name__)
@app.route("/1.1/infos")
def infos():
time.sleep(timeout)
return jsonify({
"uuid": "6fa459ea-ee8a-3ca4-894e-db77e1europe"
})
return jsonify({"uuid": "6fa459ea-ee8a-3ca4-894e-db77e1europe"})


@app.route("/1.1/users")
def users():
time.sleep(timeout)
return jsonify({
"total": 2,
"items": [
{
"id": 42,
"line_id": 3,
"agent_id": 2,
"firstname": "Bob",
"lastname": "Dylan",
"exten": "1632",
"email": "[email protected]",
"mobile_phone_number": "0634321243"
},
{
"id": 100,
"line_id": 42,
"agent_id": None,
"firstname": "Charles",
"lastname": "European",
"exten": "9012",
"email": "",
"mobile_phone_number": ""
}
]
})
return jsonify(
{
"total": 2,
"items": [
{
"id": 42,
"line_id": 3,
"agent_id": 2,
"firstname": "Bob",
"lastname": "Dylan",
"exten": "1632",
"email": "[email protected]",
"mobile_phone_number": "0634321243",
},
{
"id": 100,
"line_id": 42,
"agent_id": None,
"firstname": "Charles",
"lastname": "European",
"exten": "9012",
"email": "",
"mobile_phone_number": "",
},
],
}
)


if __name__ == "__main__":
Expand Down
38 changes: 38 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[tool.black]
skip-string-normalization = true

[tool.flake8]
show-source = true
max-line-length = 99
application-import-names = "wazo_dird"
exclude = [
"build",
".tox",
".eggs",
"integration_tests/assets/scripts"
]

ignore = [
"E203", # whitespace before ':' warnings
"E501", # line too long
"W503", # line break before binary operator
]

[tool.mypy]
python_version = "3.10"

show_error_codes = true
explicit_package_bases = true
check_untyped_defs = true
warn_unused_configs = true
follow_imports = "skip"
ignore_missing_imports = true

plugins = [
"sqlmypy"
]
exclude = [
"build",
"integration_tests/docker/broken-plugins/setup.py"
]
DrPyser marked this conversation as resolved.
Show resolved Hide resolved
packages = ["wazo_dird"]
44 changes: 4 additions & 40 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,15 @@ deps =

[testenv:black]
skip_install = true
deps = black
commands = black --skip-string-normalization .
exclude =
integration_tests/assets/scripts

[testenv:pycodestyle]
# E501: line too long (80 chars)
commands =
-sh -c 'pycodestyle --ignore=E501 wazo_dird > pycodestyle.txt'
deps =
pycodestyle
allowlist_externals =
sh

[testenv:pylint]
commands =
-sh -c 'pylint --rcfile=/usr/share/xivo-ci/pylintrc wazo_dird > pylint.txt'
deps =
-rrequirements.txt
-rtest-requirements.txt
pylint
allowlist_externals =
sh
pre-commit
commands = pre-commit run black --all-files

[testenv:linters]
skip_install = true
basepython = python3.10
deps = flake8
flake8-colors
black
commands =
black --skip-string-normalization --check .
flake8
deps = pre-commit
commands = pre-commit run --all-files

[testenv:integration]
use_develop = true
Expand All @@ -65,15 +41,3 @@ commands =
pytest {posargs}
allowlist_externals =
make

[flake8]
exclude =
.tox
.eggs
integration_tests/assets/scripts
show-source = true
max-line-length = 99
application-import-names = wazo_dird
# W503: line break before binary operator
# E203: whitespace before ':' warnings
ignore = E203, E501, W503
6 changes: 4 additions & 2 deletions wazo_dird/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ def __init__(self, location_path, msg):


class InvalidSourceConfigError(InvalidConfigError):
def __init__(self, source_info: dict, details: dict = None, details_fmt: str = ''):
def __init__(
self, source_info: dict, details: dict | None = None, details_fmt: str = ''
):
assert 'backend' in source_info, repr(source_info)
super().__init__(
location_path=f'/backends/{source_info["backend"]}/sources',
Expand All @@ -172,7 +174,7 @@ def __init__(self, source_info: dict, details: dict = None, details_fmt: str = '


class InvalidSourceConfigAPIError(APIException):
def __init__(self, source_info: dict, details: dict = None) -> None:
def __init__(self, source_info: dict, details: dict | None = None) -> None:
details = details or {}
details.update(source_info=source_info)
super().__init__(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ def test_set_tenant_with_key_file(self):

def test_set_tenant_without_key_file(self):
config = deepcopy(SOURCE_CONFIG)
del config['auth']['key_file']
del config['auth']['key_file'] # type: ignore[attr-defined]
self.registry.get(config)
assert self.confd_client.tenant_uuid != SOURCE_CONFIG['tenant_uuid']
4 changes: 2 additions & 2 deletions wazo_dird/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def load_services(
controller: Controller,
):
global services_extension_manager
dependencies: ServiceDependencies = {
dependencies = {
'config': config,
'source_manager': source_manager,
'bus': bus,
Expand Down Expand Up @@ -90,7 +90,7 @@ def load_views(
rest_api: CoreRestApi,
):
global views_extension_manager
dependencies: ViewDependencies = {
dependencies = {
'config': config,
'services': services,
'auth_client': auth_client,
Expand Down
5 changes: 3 additions & 2 deletions wazo_dird/plugins/config/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

from wazo_dird.auth import required_acl, required_master_tenant
from wazo_dird.http import AuthResource
from wazo_dird.plugins.config_service.plugin import Service as ConfigService


class Config(AuthResource):
_config_service = None
_config_service: ConfigService

@classmethod
def configure(cls, config_service):
def configure(cls, config_service: ConfigService):
cls._config_service = config_service

@required_master_tenant()
Expand Down
17 changes: 14 additions & 3 deletions wazo_dird/plugins/office365_backend/services.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright 2019-2023 The Wazo Authors (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later
from __future__ import annotations

import itertools
import logging
from typing import TypedDict
import uuid

import requests
Expand Down Expand Up @@ -132,10 +134,19 @@ def get_microsoft_access_token(user_uuid, wazo_token, **auth_config):
logger.error('Error occured while connecting to wazo-auth, error :%s', e)


def get_first_email(contact_information):
return next(iter(contact_information.get('emailAddresses') or []), {}).get(
'address'
class EmailAddress(TypedDict, total=False):
address: str


class ContactInformation(TypedDict, total=False):
emailAddresses: list[EmailAddress]


def get_first_email(contact_information: ContactInformation) -> str | None:
first_email: EmailAddress = next(
iter(contact_information.get('emailAddresses') or ()), {}
)
return first_email.get('address')


def aggregate_numbers(contact):
Expand Down
14 changes: 8 additions & 6 deletions wazo_dird/plugins/personal/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io
import logging
import re
from typing import ClassVar

from flask import request
from flask import Response
Expand All @@ -16,6 +17,7 @@
from wazo_dird import auth
from wazo_dird.auth import required_acl
from wazo_dird.http import LegacyAuthResource
from wazo_dird.plugins.personal_service.plugin import _PersonalService

logger = logging.getLogger(__name__)

Expand All @@ -37,10 +39,10 @@ def _get_calling_user_uuid():


class PersonalAll(LegacyAuthResource):
personal_service = None
personal_service: ClassVar[_PersonalService]

@classmethod
def configure(cls, personal_service):
def configure(cls, personal_service: _PersonalService):
cls.personal_service = personal_service

@required_acl('dird.personal.create')
Expand Down Expand Up @@ -119,10 +121,10 @@ def format_json(contacts):


class PersonalOne(LegacyAuthResource):
personal_service = None
personal_service: ClassVar[_PersonalService]

@classmethod
def configure(cls, personal_service):
def configure(cls, personal_service: _PersonalService):
cls.personal_service = personal_service

@required_acl('dird.personal.{contact_id}.read')
Expand Down Expand Up @@ -170,10 +172,10 @@ def delete(self, contact_id):


class PersonalImport(LegacyAuthResource):
personal_service = None
personal_service: ClassVar[_PersonalService]

@classmethod
def configure(cls, personal_service):
def configure(cls, personal_service: _PersonalService):
cls.personal_service = personal_service

@required_acl('dird.personal.import.create')
Expand Down
Loading