Skip to content

Commit

Permalink
Merge pull request #65 from evo-company/add-typings
Browse files Browse the repository at this point in the history
add mypy
  • Loading branch information
kindermax committed Jul 27, 2022
2 parents c0f27c0 + 53ad16f commit 6824ccd
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
node_modules
__pycache__
.vscode
.tox
.pytest_cache
.mypy_cache
hiku.egg-info
5 changes: 4 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ jobs:
python -m pip install tox tox-gh-actions codecov
python -m pip install -r requirements-tox.txt
- name: Run flake8
if: startsWith(matrix.python-version, '3.10')
if: startsWith(matrix.python-version, '3.7')
run: tox -e flake8
- name: Run mypy
if: startsWith(matrix.python-version, '3.7')
run: tox -e mypy
- name: Run unit tests
run: tox -- --cov
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.7
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.10.5-slim as base
FROM python:3.7.13-slim as base

WORKDIR /work

Expand Down
6 changes: 3 additions & 3 deletions hiku/edn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from decimal import Decimal
from datetime import datetime
from itertools import chain
from json.encoder import encode_basestring, encode_basestring_ascii
from json.encoder import encode_basestring, encode_basestring_ascii # type: ignore # noqa: E501


class ImmutableDict(dict):
Expand All @@ -20,8 +20,8 @@ def _immutable(self):
raise TypeError("{} object is immutable"
.format(self.__class__.__name__))

__delitem__ = __setitem__ = _immutable
clear = pop = popitem = setdefault = update = _immutable
__delitem__ = __setitem__ = _immutable # type: ignore
clear = pop = popitem = setdefault = update = _immutable # type: ignore


class Symbol(str):
Expand Down
2 changes: 1 addition & 1 deletion hiku/expr/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

THIS = 'this'

_Func = namedtuple('__func__', 'expr, args')
_Func = namedtuple('_Func', 'expr, args')


class _DotHandler:
Expand Down
6 changes: 5 additions & 1 deletion hiku/federation/sdl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import typing as t

from math import isfinite
from typing import (
Optional,
Expand Down Expand Up @@ -100,7 +102,9 @@ def _encode_default_value(value) -> Optional[ast.ValueNode]:

if isinstance(value, Iterable) and not isinstance(value, str):
maybe_value_nodes = (_encode_default_value(item) for item in value)
value_nodes = list(filter(None, maybe_value_nodes))
value_nodes: t.List[ast.ValueNode] = list(
filter(None, maybe_value_nodes)
)
return ast.ListValueNode(values=[value_nodes])

raise TypeError(f"Cannot convert value to AST: {inspect(value)}.")
Expand Down
20 changes: 18 additions & 2 deletions hiku/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
are used to fetch any data from any data source.
"""
import typing as t

from abc import ABC, abstractmethod
from itertools import chain
from functools import reduce
from collections import OrderedDict
from typing import List

from .types import OptionalMeta, SequenceMeta, TypeRefMeta, Record, Any
from .types import (
OptionalMeta,
SequenceMeta,
TypeRefMeta,
Record,
Any,
GenericMeta,
)
from .utils import cached_property, const


Expand Down Expand Up @@ -52,7 +61,14 @@ class Option(AbstractOption):
Option('size', Integer, default=100)
"""
def __init__(self, name, type_, *, default=Nothing, description=None):
def __init__(
self,
name: str,
type_: GenericMeta,
*,
default: t.Any = Nothing,
description: t.Optional[str] = None
):
"""
:param name: name of the option
:param type_: type of the option or ``None``
Expand Down
9 changes: 6 additions & 3 deletions hiku/introspection/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import typing as t

from dataclasses import dataclass
from functools import partial
from collections import OrderedDict

Expand Down Expand Up @@ -35,8 +36,10 @@
)


class Directive(t.NamedTuple):
class Argument(t.NamedTuple):
@dataclass(frozen=True)
class Directive:
@dataclass(frozen=True)
class Argument:
name: str
type_ident: t.Any
description: str
Expand Down Expand Up @@ -696,7 +699,7 @@ class GraphQLIntrospection(GraphTransformer):
graph = apply(graph, [GraphQLIntrospection(graph)])
"""
__directives__ = _BUILTIN_DIRECTIVES
__directives__: t.Tuple[Directive, ...] = _BUILTIN_DIRECTIVES

def __init__(self, query_graph, mutation_graph=None):
"""
Expand Down
4 changes: 3 additions & 1 deletion hiku/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
.. _Datomic Pull API: http://docs.datomic.com/pull.html
.. _om.next: https://github.com/omcljs/om/wiki/Documentation-(om.next)
"""
import typing as t

from itertools import chain
from collections import OrderedDict
from collections.abc import Sequence
Expand All @@ -62,7 +64,7 @@ def _compute_hash(obj):


class Base:
__attrs__ = ()
__attrs__: t.Tuple[str, ...] = ()

def __repr__(self):
kwargs = ', '.join('{}={!r}'.format(attr, self.__dict__[attr])
Expand Down
3 changes: 2 additions & 1 deletion hiku/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ class Mapping(metaclass=MappingMeta):


class RecordMeta(TypingMeta):
__field_types__: OrderedDict

def __cls_init__(cls, field_types):
if hasattr(field_types, 'items'):
Expand All @@ -162,7 +163,7 @@ def accept(cls, visitor):


class Record(metaclass=RecordMeta):
pass
__field_types__: OrderedDict


class CallableMeta(TypingMeta):
Expand Down
5 changes: 5 additions & 0 deletions lets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ commands:
depends: [_build-tests]
cmd: docker-compose run --rm test-base tox -e flake8

mypy:
description: Run mypy
depends: [_build-tests]
cmd: docker-compose run --rm test-base tox -e mypy

reqs:
description: Update requirements.txt
cmd: pip-compile -U requirements.in
Expand Down
21 changes: 21 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[mypy]
;check_untyped_defs = True
;disallow_any_generics = True
;disallow_untyped_calls = True
;disallow_untyped_defs = True
follow_imports = silent
strict_optional = True
warn_redundant_casts = True
warn_unused_ignores = True

[mypy-google.*]
ignore_missing_imports = True

[mypy-graphql.*]
ignore_missing_imports = True

[mypy-sqlalchemy.*]
ignore_missing_imports = True

[mypy-prometheus_client.*]
ignore_missing_imports = True
Empty file added py.typed
Empty file.
7 changes: 7 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ deps = flake8
max-line-length = 80
exclude = *_pb2.py,.tox,.git,env,docs,.venv

[testenv:mypy]
basepython = python3
deps = mypy
setenv =
MYPYPATH={toxinidir}
commands = mypy --config-file {toxinidir}/mypy.ini -p hiku

[pytest]
addopts = -q --tb=native
testpaths = tests
Expand Down

0 comments on commit 6824ccd

Please sign in to comment.