Skip to content

Commit

Permalink
Use ruff to format and fix some missing type hitns (#905)
Browse files Browse the repository at this point in the history
* Use ruff to format and fix some missing type hitns

* Fix some typing errors
  • Loading branch information
michaelboulton committed Jan 17, 2024
1 parent 18fc014 commit 28a416e
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 46 deletions.
15 changes: 10 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
repos:
- repo: https://github.com/ambv/black
rev: 23.7.0
- repo: https://github.com/rhysd/actionlint
rev: v1.6.26
hooks:
- id: black
files: "(tavern|tests)"
- id: actionlint
args: ["-shellcheck="]
- repo: https://github.com/hadialqattan/pycln
rev: v2.4.0
hooks:
- id: pycln
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.280"
rev: "v0.1.11"
hooks:
- id: ruff-format
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- repo: https://github.com/pre-commit/mirrors-prettier
Expand Down
4 changes: 3 additions & 1 deletion constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --all-extras --output-file=constraints.txt --strip-extras pyproject.toml
# pip-compile --all-extras --output-file=constraints.txt --resolver=backtracking --strip-extras pyproject.toml
#
alabaster==0.7.13
# via sphinx
Expand Down Expand Up @@ -232,6 +232,8 @@ ruamel-yaml==0.17.31
# via pykwalify
ruamel-yaml-clib==0.2.7
# via ruamel-yaml
ruff==0.1.13
# via tavern (pyproject.toml)
secretstorage==3.3.3
# via keyring
six==1.16.0
Expand Down
1 change: 0 additions & 1 deletion example/advanced/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def get_db():
"CREATE TABLE numbers_table (name TEXT NOT NULL, number INTEGER NOT NULL)"
)


return db


Expand Down
1 change: 0 additions & 1 deletion example/cookies/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def get_db():
"CREATE TABLE numbers_table (name TEXT NOT NULL, number INTEGER NOT NULL)"
)


return db


Expand Down
3 changes: 1 addition & 2 deletions example/mqtt/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def create_device():

try:
r["clean"]
except (TypeError):
except TypeError:
return jsonify({"error": "checking for clean key"}), 500
except KeyError:
try:
Expand Down Expand Up @@ -196,7 +196,6 @@ def attempt(query):
with contextlib.suppress(Exception):
db.execute(query)


attempt("DELETE FROM devices_table")
attempt(
"CREATE TABLE devices_table (device_id TEXT NOT NULL, lights_on INTEGER NOT NULL)"
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ dev = [
"fluent-logger",
"itsdangerous",
"mypy",
"ruff>=0.1.11",
"mypy-extensions",
"coverage[toml]",
"flit >=3.2,<4",
Expand Down Expand Up @@ -103,9 +104,6 @@ requests = "tavern._plugins.rest.tavernhook:TavernRestPlugin"
[project.entry-points.tavern_mqtt]
paho-mqtt = "tavern._plugins.mqtt.tavernhook"

[tool.black]
target-version = ['py37']

[tool.mypy]
python_version = 3.8
ignore_missing_imports = true
Expand Down Expand Up @@ -164,6 +162,9 @@ target-version = "py38"
[tool.ruff.isort]
known-first-party = ["tavern"]

[tool.ruff.format]
docstring-code-format = true

[tool.tbump.version]
current = "2.7.1"

Expand Down
24 changes: 21 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --all-extras --generate-hashes --output-file=requirements.txt pyproject.toml
# pip-compile --all-extras --generate-hashes --output-file=requirements.txt --resolver=backtracking pyproject.toml
#
alabaster==0.7.13 \
--hash=sha256:1ee19aca801bbabb5ba3f5f258e4422dfa86f82f3e9cefb0859b283cdd7f62a3 \
Expand Down Expand Up @@ -948,6 +948,25 @@ ruamel-yaml-clib==0.2.7 \
--hash=sha256:f34019dced51047d6f70cb9383b2ae2853b7fc4dce65129a5acd49f4f9256646 \
--hash=sha256:f6d3d39611ac2e4f62c3128a9eed45f19a6608670c5a2f4f07f24e8de3441d38
# via ruamel-yaml
ruff==0.1.13 \
--hash=sha256:226b517f42d59a543d6383cfe03cccf0091e3e0ed1b856c6824be03d2a75d3b6 \
--hash=sha256:2f59bcf5217c661254bd6bc42d65a6fd1a8b80c48763cb5c2293295babd945dd \
--hash=sha256:5f0312ba1061e9b8c724e9a702d3c8621e3c6e6c2c9bd862550ab2951ac75c16 \
--hash=sha256:6bbbc3042075871ec17f28864808540a26f0f79a4478c357d3e3d2284e832998 \
--hash=sha256:7a36fa90eb12208272a858475ec43ac811ac37e91ef868759770b71bdabe27b6 \
--hash=sha256:9a1600942485c6e66119da294c6294856b5c86fd6df591ce293e4a4cc8e72989 \
--hash=sha256:9ebb40442f7b531e136d334ef0851412410061e65d61ca8ce90d894a094feb22 \
--hash=sha256:9fb6b3b86450d4ec6a6732f9f60c4406061b6851c4b29f944f8c9d91c3611c7a \
--hash=sha256:a623349a505ff768dad6bd57087e2461be8db58305ebd5577bd0e98631f9ae69 \
--hash=sha256:b13ba5d7156daaf3fd08b6b993360a96060500aca7e307d95ecbc5bb47a69296 \
--hash=sha256:dcaab50e278ff497ee4d1fe69b29ca0a9a47cd954bb17963628fa417933c6eb1 \
--hash=sha256:e261f1baed6291f434ffb1d5c6bd8051d1c2a26958072d38dfbec39b3dda7352 \
--hash=sha256:e3fd36e0d48aeac672aa850045e784673449ce619afc12823ea7868fcc41d8ba \
--hash=sha256:e6894b00495e00c27b6ba61af1fc666f17de6140345e5ef27dd6e08fb987259d \
--hash=sha256:ee3febce7863e231a467f90e681d3d89210b900d49ce88723ce052c8761be8c7 \
--hash=sha256:f57de973de4edef3ad3044d6a50c02ad9fc2dff0d88587f25f1a48e3f72edf5e \
--hash=sha256:f988746e3c3982bea7f824c8fa318ce7f538c4dfefec99cd09c8770bd33e6539
# via tavern (pyproject.toml)
secretstorage==3.3.3 \
--hash=sha256:2403533ef369eca6d2ba81718576c5e0f564d5cca1b58f73a8b23e7d4eeebd77 \
--hash=sha256:f356e6628222568e3af06f2eba8df495efa13b3b63081dafd4f7d9a7b7bc9f99
Expand Down Expand Up @@ -1073,7 +1092,6 @@ zipp==3.16.2 \
# via importlib-metadata

# WARNING: The following packages were not pinned, but pip requires them to be
# pinned when the requirements file includes hashes and the requirement is not
# satisfied by a package already installed. Consider using the --allow-unsafe flag.
# pinned when the requirements file includes hashes. Consider using the --allow-unsafe flag.
# pip
# setuptools
12 changes: 3 additions & 9 deletions scripts/smoke.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@
set -ex

pre-commit run ruff --all-files
pre-commit run black --all-files
pre-commit run ruff-format --all-files

# Separate as isort can interfere with other testenvs
tox --parallel -c tox.ini \
-e py3check

tox --parallel -c tox.ini \
-e py3 \
-e py3mypy
-e py3,py3mypy

tox -c tox-integration.ini \
-e py3-generic \
-e py3-advanced \
-e py3-cookies \
-e py3-components \
-e py3-hooks \
-e py3-mqtt
-e py3-generic,py3-mqtt
12 changes: 7 additions & 5 deletions tavern/_core/dict_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ def recurse_access_key(data, query: str):
Example:
>>> recurse_access_key({'a': 'b'}, 'a')
>>> recurse_access_key({"a": "b"}, "a")
'b'
>>> recurse_access_key({'a': {'b': ['c', 'd']}}, 'a.b[0]')
>>> recurse_access_key({"a": {"b": ["c", "d"]}}, "a.b[0]")
'c'
Args:
Expand Down Expand Up @@ -203,9 +203,9 @@ def _deprecated_recurse_access_key(current_val, keys):
Example:
>>> _deprecated_recurse_access_key({'a': 'b'}, ['a'])
>>> _deprecated_recurse_access_key({"a": "b"}, ["a"])
'b'
>>> _deprecated_recurse_access_key({'a': {'b': ['c', 'd']}}, ['a', 'b', '0'])
>>> _deprecated_recurse_access_key({"a": {"b": ["c", "d"]}}, ["a", "b", "0"])
'c'
Args:
Expand Down Expand Up @@ -351,7 +351,9 @@ def check_keys_match_recursive(
>>> check_keys_match_recursive({"a": {"b": "c"}}, {"a": {"b": "c"}}, []) is None
True
>>> check_keys_match_recursive({"a": {"b": "c"}}, {"a": {"b": "d"}}, []) # doctest: +IGNORE_EXCEPTION_DETAIL
>>> check_keys_match_recursive(
... {"a": {"b": "c"}}, {"a": {"b": "d"}}, []
... ) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
File "/home/michael/code/tavern/tavern/tavern/_core.util/dict_util.py", line 223, in check_keys_match_recursive
tavern._core.exceptions.KeyMismatchError: Key mismatch: (expected["a"]["b"] = 'c', actual["a"]["b"] = 'd')
Expand Down
7 changes: 6 additions & 1 deletion tavern/_core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
class TavernException(Exception):
"""Base exception
Fields are internal and might change in future
Fields are internal and might change in future without warning
Attributes:
is_final: whether this exception came from a 'finally' block
stage: stage that caused this issue
test_block_config: config for stage
"""

stage: Optional[Dict]
Expand Down
3 changes: 2 additions & 1 deletion tavern/_core/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import uuid
from abc import abstractmethod
from itertools import chain
from typing import List, Optional

import pytest
import yaml
Expand Down Expand Up @@ -120,7 +121,7 @@ def __init__(self, stream):
Resolver.__init__(self)
SourceMappingConstructor.__init__(self)

env_path_list = None
env_path_list: Optional[List] = None
env_var_name = "TAVERN_INCLUDE"


Expand Down
8 changes: 4 additions & 4 deletions tavern/_core/pytest/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,12 @@ def _load_fixture_values(self):
values.update(mark_values)

# Use autouse fixtures as well
for m in self.fixturenames:
if m in values:
logger.debug("%s already explicitly used", m)
for name in self.fixturenames:
if name in values:
logger.debug("%s already explicitly used", name)
continue

mark_values = {m: self.funcargs[m]}
mark_values = {name: self.funcargs[name]}
values.update(mark_values)

return values
Expand Down
4 changes: 2 additions & 2 deletions tavern/_core/schema/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
import tempfile
from typing import Dict
from typing import Dict, Mapping

import pykwalify
import yaml
Expand Down Expand Up @@ -129,7 +129,7 @@ def wrapfile(to_wrap):
os.remove(wrapped_tmp.name)


def verify_tests(test_spec, with_plugins: bool = True) -> None:
def verify_tests(test_spec: Mapping, with_plugins: bool = True) -> None:
"""Verify that a specific test block is correct
Todo:
Expand Down
7 changes: 4 additions & 3 deletions tavern/_core/schema/jsonschema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import re
from typing import Mapping

import jsonschema
from jsonschema import Draft7Validator, ValidationError
Expand Down Expand Up @@ -104,12 +105,12 @@ def oneOf(validator: Draft7Validator, oneOf, instance, schema):
)


def verify_jsonschema(to_verify, schema) -> None:
def verify_jsonschema(to_verify: Mapping, schema: Mapping) -> None:
"""Verify a generic file against a given jsonschema
Args:
to_verify (dict): Filename of source tests to check
schema (dict): Schema to verify against
to_verify: Filename of source tests to check
schema: Schema to verify against
Raises:
BadSchemaError: Schema did not match
Expand Down
3 changes: 2 additions & 1 deletion tavern/_plugins/mqtt/tavernhook.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from os.path import abspath, dirname, join
from typing import Dict, Optional

import yaml

Expand All @@ -18,7 +19,7 @@


def get_expected_from_request(response_block, test_block_config, session):
expected = None
expected: Optional[Dict] = None

# mqtt response is not required
if response_block:
Expand Down
3 changes: 2 additions & 1 deletion tavern/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging.config
from argparse import ArgumentParser
from textwrap import dedent
from typing import Dict

from .core import run

Expand Down Expand Up @@ -48,7 +49,7 @@ def main():
log_level = "INFO"

# Basic logging config that will print out useful information
log_cfg = {
log_cfg: Dict = {
"version": 1,
"formatters": {
"default": {
Expand Down
4 changes: 2 additions & 2 deletions tavern/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ def recurse_check_key_match(
except exceptions.KeyMismatchError as e:
self._adderr(e.args[0], e=e)

def _check_for_validate_functions(self, response_block) -> None:
def _check_for_validate_functions(self, response_block: Mapping) -> None:
"""
See if there were any functions specified in the response block and save them for later use
Args:
response_block (dict): block of external functions to call
response_block: block of external functions to call
"""

def check_ext_functions(verify_block):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def add_opts(self, pytestconfig):

def _make_fake_exc_info(self, exc_type):
# Copied from pytest tests
class FakeExcinfo(_pytest._code.ExceptionInfo):
class FakeExcinfo(_pytest._code.ExceptionInfo): # type:ignore
pass

try:
Expand Down

0 comments on commit 28a416e

Please sign in to comment.