Skip to content

Commit

Permalink
fix type hinting in TgnApp
Browse files Browse the repository at this point in the history
  • Loading branch information
shmir committed Jun 23, 2022
1 parent e8902b5 commit 5c2cbdc
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 130 deletions.
9 changes: 4 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
language_version: python3.9
args: [--line-length=127]
- repo: https://github.com/python/black
rev: 21.12b0
rev: 22.3.0
hooks:
- id: black
language_version: python3.9
Expand All @@ -25,9 +25,8 @@ repos:
language_version: python3.9
args: [
--max-line-length=127,
'--ignore=D100,D101,D102,D103,D104,D105,D106,D107,D200,D210,D401,W503,E203'
'--ignore=D200'
]
# See https://stackoverflow.com/questions/61238318/pylint-and-pre-commit-hook-unable-to-import/61238571#61238571
- repo: local
hooks:
- id: pylint
Expand All @@ -39,12 +38,12 @@ repos:
--max-line-length=127,
--max-public-methods=32,
--max-args=8,
'--disable=too-few-public-methods,logging-fstring-interpolation,too-many-instance-attributes,no-else-return,too-many-locals,no-self-use,duplicate-code,broad-except,logging-not-lazy,unspecified-encoding',
'--disable=too-few-public-methods,logging-fstring-interpolation,unspecified-encoding',
'--good-names=ip,rc,eval',
'--load-plugins=pylint_pytest'
]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.930
rev: v0.961
hooks:
- id: mypy
verbose: true
Expand Down
17 changes: 10 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@ help:
@echo ' user=user name, default pypiadmin'
@echo ' password=user password, default pypiadmin'

clean:
rm -rf dist/*
rm -rf *.egg-info
rm -rf build

install:
make clean
python -m pip install -U pip
pip install -U -r requirements-dev.txt

.PHONY: build
test:
pytest --cache-clear --cov=trafficgenerator

build:
make test
rm -rf dist/*
rm -rf *.egg-info
rm -rf build
make clean
python setup.py bdist_wheel

upload:
make build
twine upload --repository-url http://$(repo):8036 --user $(user) --password $(password) dist/*

test:
pytest --cache-clear --cov=trafficgenerator
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
future
9 changes: 2 additions & 7 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@

[isort]
profile=black
line_length=127
forced_separate=trfficgenerator,tests

[flake8]
max-line-length=127
profile = black

[mypy]
ignore_missing_imports = True
allow_untyped_calls = False
allow_untyped_defs = False
allow_incomplete_defs = False
follow_imports = skip
no_strict_optional = True
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def main() -> None:
"""Packaging script."""
"""Package script."""
with open("requirements.txt", "r") as requirements:
install_requires = requirements.read().splitlines()
with open("README.md", "r") as readme:
Expand Down
Binary file added tests/DHCPv4_Loopback.ixncfg
Binary file not shown.
20 changes: 11 additions & 9 deletions tests/test_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
# pylint: disable=redefined-outer-name
import logging
from typing import Dict, Iterable, List, Type
from typing import Dict, List, Type

import pytest

Expand All @@ -15,16 +15,18 @@
class TgnTestObject(TgnObject):
"""Mock test object."""

# pylint: disable=too-many-instance-attributes

def get_attributes(self) -> Dict[str, str]:
"""Returns object data as its attributes."""
"""Return object data as its attributes."""
return self._data

def get_attribute(self, attribute: str) -> str:
"""Returns single data entry as a single attribute."""
"""Return single data entry as a single attribute."""
return self._data[attribute]

def get_children(self, *types: str) -> List[TgnObject]:
"""Returns all objects as children."""
"""Return all objects as children."""
return list(self.objects.values())

def _create(self, **attributes: object) -> str:
Expand All @@ -41,8 +43,8 @@ def get_obj_class(self, obj_type: str) -> Type[TgnObject]:


@pytest.fixture()
def tgn_object() -> Iterable[TgnTestObject]:
"""Yields dummy objects hierarchy."""
def tgn_object() -> TgnTestObject:
"""Yield dummy objects hierarchy."""
# pylint: disable=attribute-defined-outside-init
tgn_object = TgnTestObject(parent=None, objRef="root1", objType="root")
tgn_object.api = None
Expand All @@ -53,7 +55,7 @@ def tgn_object() -> Iterable[TgnTestObject]:
tgn_object.node1.node11 = TgnTestObject(objRef="node11", objType="node", parent=tgn_object.node1, name="name11")
tgn_object.node1.node12 = TgnTestObject(objRef="node12", objType="node", parent=tgn_object.node1, name="name12")
tgn_object.node1.leaf11 = TgnTestObject(objRef="leaf11", objType="leaf", parent=tgn_object.node1)
yield tgn_object
return tgn_object


def test_app() -> None:
Expand Down Expand Up @@ -101,7 +103,7 @@ def test_objects_dict(tgn_object: TgnTestObject) -> None:
objects_dict[tgn_object.node1][tgn_object.node1.node12] = "node 12 entry"
objects_dict[tgn_object.node1][tgn_object.node1.leaf11] = TgnObjectsDict()
objects_dict[tgn_object.node2] = "node 2 entry"
with pytest.raises(TgnError) as _:
with pytest.raises(TgnError):
objects_dict["invalid key"] = ""
assert objects_dict[tgn_object.node2] == "node 2 entry"
assert objects_dict[tgn_object.node2.name] == "node 2 entry"
Expand All @@ -119,7 +121,7 @@ def test_sub_dict(tgn_object: TgnTestObject) -> None:
assert sub_stats_dict[tgn_object.node1]["a"] == 1
assert sub_stats_dict[tgn_object.node2]["c"] == 3
with pytest.raises(KeyError):
sub_stats_dict["a"]
sub_stats_dict["a"] # pylint: disable=pointless-statement


def test_true_false() -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_tcl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

@pytest.fixture(scope="session")
def logger() -> logging.Logger:
"""Yields logger for package regression testing."""
"""Yield logger for package regression testing."""
logger = logging.getLogger("tgn")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
Expand All @@ -21,7 +21,7 @@ def logger() -> logging.Logger:

@pytest.fixture
def tcl(logger: logging.Logger) -> TgnTclWrapper:
"""Yields TgnTclWrapper."""
"""Yield TgnTclWrapper."""
return TgnTclWrapper(logger)


Expand Down
8 changes: 5 additions & 3 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
; Tox configuration for manual testing.

[tox]
envlist = py37,py37_64,py38,py38_64,py39,py39_64
envlist = py37,py38,py39,py310
skip_missing_interpreters=True

[testenv]
basepython=
py37: C:\Python\Python371\python.exe
py39_64: C:\Python\Python391\python.exe
py37: C:\Python371-32\python.exe
py38: C:\Python3810-64\python.exe
py39: C:\Python39\python.exe
py310: C:\Python310\python.exe

deps = -r{toxinidir}/requirements-dev.txt
commands=pytest
3 changes: 3 additions & 0 deletions trafficgenerator/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
PyTrafficGenerator package.
"""
4 changes: 1 addition & 3 deletions trafficgenerator/tgn_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
"""
import logging

from trafficgenerator.tgn_utils import ApiType


class TgnApp:
"""Base class for all TGN applications classes."""

def __init__(self, logger: logging.Logger, api_wrapper: ApiType) -> None:
def __init__(self, logger: logging.Logger, api_wrapper: object) -> None:
"""Initialize logger and API wrapper."""
self.logger = logger
self.api = api_wrapper
21 changes: 10 additions & 11 deletions trafficgenerator/tgn_conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import logging
import sys
from pathlib import Path
from typing import Iterable

import pytest
from _pytest.config.argparsing import Parser
Expand Down Expand Up @@ -46,26 +45,26 @@ def pytest_generate_tests(metafunc: Metafunc) -> None:

@pytest.fixture(scope="session")
def logger() -> logging.Logger:
"""Yields configured logger."""
"""Yield configured logger."""
logger = logging.getLogger("tgn")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler(sys.stdout))
yield logger
return logger


@pytest.fixture(scope="session")
def api(request: SubRequest) -> Iterable[ApiType]:
def api(request: SubRequest) -> ApiType:
"""Yield API type - generate tests will generate API types based on the api option."""
yield ApiType[request.param]
return ApiType[request.param]


@pytest.fixture(scope="session")
def server(request: SubRequest) -> Iterable[str]:
"""Yields server name in confing file - generate tests will generate servers based on the server option."""
yield request.param
def server(request: SubRequest) -> str:
"""Yield server name in confing file - generate tests will generate servers based on the server option."""
return request.param


@pytest.fixture(scope="session")
def server_properties(request: SubRequest, server: str) -> Iterable[dict]:
"""Yields server properties dict for the requested server."""
yield get_test_config(request.config.getoption("--tgn-config")).server_properties[server]
def server_properties(request: SubRequest, server: str) -> dict:
"""Yield server properties dict for the requested server."""
return get_test_config(request.config.getoption("--tgn-config")).server_properties[server]
Loading

0 comments on commit 5c2cbdc

Please sign in to comment.