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 #265

Draft
wants to merge 2 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
10 changes: 10 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ repos:
rev: 23.10.1
hooks:
- id: black
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.6.1
hooks:
- id: mypy
language_version: "3.10"
additional_dependencies:
- "types-requests"
- "types-setuptools"
- "sqlalchemy-stubs"

25 changes: 24 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,27 @@ ignore = [
exclude = [
".tox",
".eggs",
]
]

[tool.mypy]
python_version = "3.10"
show_error_codes = true

check_untyped_defs = true
disallow_untyped_calls = false
disallow_untyped_defs = false

warn_unused_configs = true
warn_unused_ignores = true
warn_redundant_casts = true
no_warn_no_return = true

# strict_equality = true
# extra_checks = true

# [[tool.mypy.overrides]]
# module = [
# "wazo_lib_rest_client.*"
# ]
# ignore_missing_imports = true

48 changes: 34 additions & 14 deletions xivo_dao/helpers/asterisk.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Copyright 2016-2024 The Wazo Authors (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later
from collections.abc import Iterable
from __future__ import annotations

from collections.abc import Collection, Iterable
from typing import Literal

from xivo_dao.helpers import errors
from sqlalchemy import Table


# taken from the definition of the "ast_true" function in Asterisk source code
_TRUTH_VALUES = [
Expand All @@ -14,37 +19,52 @@
'on',
]

AST_TRUE = Literal[
'yes',
'true',
'y',
't',
'1',
'on',
]


def convert_ast_true_to_int(value):
def convert_ast_true_to_int(value: AST_TRUE) -> int:
return int(value in _TRUTH_VALUES)


def convert_int_to_ast_true(value):
StrBool = Literal['yes', 'no']


def convert_int_to_ast_true(value: int) -> StrBool:
if value:
return 'yes'
return 'no'


class AsteriskOptionsMixin:
EXCLUDE_OPTIONS = set()
EXCLUDE_OPTIONS_CONFD = set()
AST_TRUE_INTEGER_COLUMNS = set()
EXCLUDE_OPTIONS: set[str] = set()
EXCLUDE_OPTIONS_CONFD: set[str] = set()
AST_TRUE_INTEGER_COLUMNS: set[str] = set()

_options: list[list[str]]
__table__: Table

@property
def options(self):
def options(self) -> list[list[str]]:
return self.all_options(self.EXCLUDE_OPTIONS_CONFD)

def all_options(self, exclude=None):
def all_options(self, exclude=None) -> list[list[str]]:
native_options = list(self.native_options(exclude))
return native_options + self._options

def native_options(self, exclude=None):
def native_options(self, exclude=None) -> Iterable[list[str]]:
for column in self.native_option_names(exclude):
value = self.native_option(column)
if value is not None:
yield [column, value]

def native_option(self, column_name):
def native_option(self, column_name: str) -> str | None:
value = getattr(self, self._attribute(column_name), None)
if value is not None and value != "":
if column_name in self.AST_TRUE_INTEGER_COLUMNS:
Expand All @@ -53,7 +73,7 @@ def native_option(self, column_name):
return str(value)
return None

@options.setter
@options.setter # type: ignore[attr-defined,no-redef]
def options(self, options):
option_names = self.native_option_names(self.EXCLUDE_OPTIONS_CONFD)
self.reset_options()
Expand Down Expand Up @@ -82,11 +102,11 @@ def set_options(self, option_names, options):
else:
self.add_extra_option(column, value)

def validate_options(self, options):
def validate_options(self, options: Iterable[Collection[str]]):
if not isinstance(options, Iterable):
raise errors.wrong_type('options', 'list of pair of strings')

def validate_option(self, option):
def validate_option(self, option: Collection[str]):
if not isinstance(option, Iterable):
raise errors.wrong_type('options', 'list of pair of strings')
if not len(option) == 2:
Expand All @@ -95,7 +115,7 @@ def validate_option(self, option):
if not isinstance(i, str):
raise errors.wrong_type('options', f"value '{i}' is not a string")

def set_native_option(self, column, value):
def set_native_option(self, column: str, value):
if column in self.AST_TRUE_INTEGER_COLUMNS:
value = convert_ast_true_to_int(value)
setattr(self, self._attribute(column), value)
Expand Down