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

Type annoation #6

Merged
merged 3 commits into from
Apr 19, 2022
Merged
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
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ repos:
files: ^.*\.rst$
language: python
additional_dependencies: [pygments, restructuredtext_lint]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.902
hooks:
- id: mypy
args: [--strict]
additional_dependencies: [pytest]
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"Maximus",
"miniconda",
"miniforge",
"mypy",
"ncipollo",
"nodeid",
"numcollected",
Expand Down
181 changes: 180 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pytest-cov = "^3.0.0"
pipdeptree = "^2.2.1"
tox = "^3.25.0"
pytest-sugar = "^0.9.4"
mypy = "^0.942"
black = "^22.3.0"
Pygments = "^2.11.2"
restructuredtext-lint = "^1.4.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
30 changes: 25 additions & 5 deletions src/pytest_prefer_nested_dup_tests/__impl.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,40 @@
def pytest_configure(config):
import pytest
import _pytest.config
import _pytest.fixtures
from typing import (
cast,
Dict,
List,
Optional,
)


class ExtendedItem(pytest.Item):
prefer_nested_dup_tests__parent_depth: int


def pytest_configure(config: _pytest.config.Config) -> None:
config.option.keepduplicates = True


def pytest_collection_modifyitems(session, config, items):
def pytest_collection_modifyitems(
session: pytest.Session,
config: _pytest.config.Config,
items: List[ExtendedItem],
) -> None:
session = session # ignore unused var warning

seen_best_nodes = {}
seen_best_nodes: Dict[str, ExtendedItem] = {}

for item in items:
item.prefer_nested_dup_tests__parent_depth = 0
parent = item.parent
parent: Optional[ExtendedItem] = cast(Optional[ExtendedItem], item.parent)
while parent != None:
parent = cast(ExtendedItem, parent)
item.prefer_nested_dup_tests__parent_depth = (
item.prefer_nested_dup_tests__parent_depth + 1
)
parent = parent.parent
parent = cast(Optional[ExtendedItem], parent.parent)
if item.nodeid not in seen_best_nodes.keys():
seen_best_nodes[item.nodeid] = item
else:
Expand Down
7 changes: 6 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
pytest_plugins = ["pytester"]
from typing import (
Sequence,
Union,
)

pytest_plugins: Union[str, Sequence[str]] = ["pytester"]
12 changes: 10 additions & 2 deletions tests/deeptest/deepertest/test___impl.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
def test___main():
pass
from typing import (
Any,
)

PytestFixture = Any


def test___main(testdir: PytestFixture) -> None:
testdir = testdir # ignore unused arg in sig
assert True
12 changes: 10 additions & 2 deletions tests/deeptest/test___impl.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
def test___main():
pass
from typing import (
Any,
)

PytestFixture = Any


def test___main(testdir: PytestFixture) -> None:
testdir = testdir # ignore unused arg in sig
assert True
26 changes: 18 additions & 8 deletions tests/test___impl.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# -*- coding: utf-8 -*-

import sys
from typing import (
Any,
)

PytestFixture = Any

def test___main():
pass

def test___main(testdir: PytestFixture) -> None:
testdir = testdir # ignore unused arg in sig
assert True

def test_drop_duplicated_dir(testdir):

def test_drop_duplicated_dir(testdir: PytestFixture) -> None:
testdir.makepyfile(
"""
def test_foo():
Expand All @@ -23,7 +29,7 @@ def test_foo():
assert result.ret == 0


def test_drop_duplicated_pkg(testdir):
def test_drop_duplicated_pkg(testdir: PytestFixture) -> None:
testdir.makepyfile(
**{
"pkg/__init__.py": "",
Expand All @@ -42,7 +48,7 @@ def test_foo():
assert result.ret == 0


def test_drop_duplicated_files(testdir):
def test_drop_duplicated_files(testdir: PytestFixture) -> None:
testdir.makepyfile(
**{
"tests/test_bar.py": """
Expand All @@ -66,7 +72,7 @@ def test_foo():
assert result.ret == 0


def test_nested_package(testdir):
def test_nested_package(testdir: PytestFixture) -> None:
# we need to hide our own "tests" module in order for this to work when testing ourself.
sys_modules_tests_old = sys.modules.get("tests")
del sys.modules["tests"]
Expand Down Expand Up @@ -131,14 +137,18 @@ def test_bar():
assert result.ret == 0
finally:
# fix up sys.modules to include our "tests" module again (not the one from the temp dir)
sys.modules["tests"] = sys_modules_tests_old
if sys_modules_tests_old:
sys.modules["tests"] = sys_modules_tests_old


def test___toplevel_coverage():
def test___toplevel_coverage(testdir: PytestFixture) -> None:
# this test solely exists to allow coverage.py to see the top level / outermost scope of our code
# this is necessary b/c our code gets imported before coverage.py hooks in
# we simply "hide" our module from python, import it, and then put it back
# we put it back, just in case something had modified it in memory before this test runs

testdir = testdir # ignore unused arg in sig

old_module = sys.modules["pytest_prefer_nested_dup_tests"] # keep a reference to it
del sys.modules["pytest_prefer_nested_dup_tests"] # hide it from python
import pytest_prefer_nested_dup_tests # import it as if new
Expand Down