Skip to content

Commit

Permalink
Merge pull request #612 from nose-devs/pass-mypy
Browse files Browse the repository at this point in the history
Make mypy pass and add it to CI
  • Loading branch information
sirosen authored May 31, 2024
2 parents 46b9187 + f610da8 commit 73b0272
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 77 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ jobs:
pip install -e '.[dev]'
nose2 -v --pretty-assert
typing:
name: 'typing (mypy)'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install tox
- run: tox run -e mypy

test:
strategy:
fail-fast: false
Expand Down
7 changes: 6 additions & 1 deletion nose2/_toml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from __future__ import annotations

import types

TOML_ENABLED: bool = False
toml: types.ModuleType | None = None

try:
import tomllib as toml
Expand All @@ -15,7 +20,7 @@


def load_toml(file: str) -> dict:
if not TOML_ENABLED:
if toml is None:
raise RuntimeError("toml library not found. Please install 'tomli'.")
with open(file, "rb") as fp:
return toml.load(fp)
6 changes: 4 additions & 2 deletions nose2/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
# code developed in reference to that module and others within unittest2.
# unittest2 is Copyright (c) 2001-2010 Python Software Foundation; All
# Rights Reserved. See: http://docs.python.org/license.html
from __future__ import annotations

import argparse
import logging
import typing as t
import unittest

from nose2 import config, util
Expand Down Expand Up @@ -315,7 +317,7 @@ class PluginInterface:
"handleDir",
# ... etc?
)
hookClass = Hook
hookClass: type[Hook] = Hook

def __init__(self):
self.hooks = {}
Expand Down Expand Up @@ -362,7 +364,7 @@ class Event:
"""

_attrs = ("handled",)
_attrs: t.ClassVar[tuple[str, ...]] = ("handled",)
version = "0.4"

def __init__(self, **metadata):
Expand Down
8 changes: 6 additions & 2 deletions nose2/plugins/loader/eggdiscovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
"""

from __future__ import annotations

import logging
import os

Expand All @@ -24,8 +26,10 @@

try:
import pkg_resources

_has_pkg_resources = True
except ImportError:
pkg_resources = None
_has_pkg_resources = False


class EggDiscoveryLoader(events.Plugin, discovery.Discoverer):
Expand Down Expand Up @@ -80,7 +84,7 @@ def _find_tests_in_egg_dir(self, event, rel_path, dist):
def _find_tests_in_dir(self, event, full_path, top_level):
if os.path.exists(full_path):
return
elif pkg_resources and full_path.find(".egg") != -1:
elif _has_pkg_resources and full_path.find(".egg") != -1:
egg_path = full_path.split(".egg")[0] + ".egg"
for dist in pkg_resources.find_distributions(egg_path):
for modname in dist._get_metadata("top_level.txt"):
Expand Down
4 changes: 3 additions & 1 deletion nose2/plugins/printhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
each call.
"""

from __future__ import annotations

import sys

from nose2 import events

INDENT = []
INDENT: list[str] = []
__unittest = True


Expand Down
8 changes: 4 additions & 4 deletions nose2/tests/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,13 @@ def __enter__(self):
self.cwd = os.getcwd()
if self.chdir:
os.chdir(self.chdir)
self.stdout = sys.stdout = sys.__stdout__ = io.StringIO()
self.stderr = sys.stderr = sys.__stderr__ = io.StringIO()
self.stdout = sys.stdout = sys.__stdout__ = io.StringIO() # type: ignore[misc]
self.stderr = sys.stderr = sys.__stderr__ = io.StringIO() # type: ignore[misc]
return self

def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout = sys.__stdout__ = self._stdout
sys.stderr = sys.__stderr__ = self._stderr
sys.stdout = sys.__stdout__ = self._stdout # type: ignore[misc]
sys.stderr = sys.__stderr__ = self._stderr # type: ignore[misc]
if self.chdir:
os.chdir(self.cwd)
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def test(p):
assert THINGS, "setup didn't run I think"


test.paramList = (1,)
test.paramList = (1,) # type: ignore[attr-defined]
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def check(a):
def test_params(self, a):
pass

test_params.paramList = (1, 2)
test_params.paramList = (1, 2) # type: ignore[attr-defined]
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def test_params(self, a):
assert self.test_setup
assert self.setup

test_params.paramList = (1, 2)
test_params.paramList = (1, 2) # type: ignore[attr-defined]
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def check(a):
def test_params(self, a):
pass

test_params.paramList = (1, 2)
test_params.paramList = (1, 2) # type: ignore[attr-defined]
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ def test_params(self, a):
assert self.test_setup
assert self.setup

test_params.paramList = (1, 2)
test_params.paramList = (1, 2) # type: ignore[attr-defined]
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,65 @@ class SomeTests(unittest.TestCase):
def test_ok(self):
pass

test_ok.tags = ["method", "pass"]
test_ok.a = 0
test_ok.b = 1
test_ok.tags = ["method", "pass"] # type: ignore[attr-defined]
test_ok.a = 0 # type: ignore[attr-defined]
test_ok.b = 1 # type: ignore[attr-defined]

def test_typeerr(self):
raise TypeError("oops")

test_typeerr.tags = ["method"]
test_typeerr.tags = ["method"] # type: ignore[attr-defined]

def test_failed(self):
print("Hello stdout")
assert False, "I failed"

test_failed.tags = ["method"]
test_failed.tags = ["method"] # type: ignore[attr-defined]

def test_skippy(self):
raise unittest.SkipTest("I wanted to skip")

test_skippy.a = 1
test_skippy.b = 1
test_skippy.a = 1 # type: ignore[attr-defined]
test_skippy.b = 1 # type: ignore[attr-defined]

def test_gen_method(self):
def check(x):
assert x == 1

check.b = 2
check.b = 2 # type: ignore[attr-defined]
yield check, 1
yield check, 2

test_gen_method.a = 1
test_gen_method.b = 1
test_gen_method.a = 1 # type: ignore[attr-defined]
test_gen_method.b = 1 # type: ignore[attr-defined]

def test_params_method(self, a):
self.assertEqual(a, 1)

test_params_method.paramList = (1, 2)
test_params_method.a = 1
test_params_method.paramList = (1, 2) # type: ignore[attr-defined]
test_params_method.a = 1 # type: ignore[attr-defined]


def test_func():
assert 1 == 1


test_func.a = 1
test_func.b = 0
test_func.tags = ["func", "pass"]
test_func.a = 1 # type: ignore[attr-defined]
test_func.b = 0 # type: ignore[attr-defined]
test_func.tags = ["func", "pass"] # type: ignore[attr-defined]


def test_gen():
def check(a, b):
assert a == b

check.tags = ["func"]
check.tags = ["func"] # type: ignore[attr-defined]
for i in range(0, 5):
yield check, (i, i)


test_gen.testGenerator = True
test_gen.tags = ["func"]
test_gen.testGenerator = True # type: ignore[attr-defined]
test_gen.tags = ["func"] # type: ignore[attr-defined]


def test_gen_nose_style():
Expand All @@ -88,19 +88,19 @@ def test_fixt():
assert did_setup


test_fixt.setup = setup
test_fixt.setup = setup # type: ignore[attr-defined]


def test_params_func(a):
assert a == 1


test_params_func.paramList = (1, 2)
test_params_func.tags = ["func"]
test_params_func.paramList = (1, 2) # type: ignore[attr-defined]
test_params_func.tags = ["func"] # type: ignore[attr-defined]


def test_params_func_multi_arg(a, b):
assert a == b


test_params_func_multi_arg.paramList = ((1, 1), (1, 2), (2, 2))
test_params_func_multi_arg.paramList = ((1, 1), (1, 2), (2, 2)) # type: ignore[attr-defined] # noqa: E501
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,65 @@ class SomeTests(unittest.TestCase):
def test_ok(self):
pass

test_ok.tags = ["method", "pass"]
test_ok.a = 0
test_ok.b = 1
test_ok.tags = ["method", "pass"] # type: ignore[attr-defined]
test_ok.a = 0 # type: ignore[attr-defined]
test_ok.b = 1 # type: ignore[attr-defined]

def test_typeerr(self):
raise TypeError("oops")

test_typeerr.tags = ["method"]
test_typeerr.tags = ["method"] # type: ignore[attr-defined]

def test_failed(self):
print("Hello stdout")
assert False, "I failed"

test_failed.tags = ["method"]
test_failed.tags = ["method"] # type: ignore[attr-defined]

def test_skippy(self):
raise unittest.SkipTest("I wanted to skip")

test_skippy.a = 1
test_skippy.b = 1
test_skippy.a = 1 # type: ignore[attr-defined]
test_skippy.b = 1 # type: ignore[attr-defined]

def test_gen_method(self):
def check(x):
assert x == 1

check.b = 2
check.b = 2 # type: ignore[attr-defined]
yield check, 1
yield check, 2

test_gen_method.a = 1
test_gen_method.b = 1
test_gen_method.a = 1 # type: ignore[attr-defined]
test_gen_method.b = 1 # type: ignore[attr-defined]

def test_params_method(self, a):
self.assertEqual(a, 1)

test_params_method.paramList = (1, 2)
test_params_method.a = 1
test_params_method.paramList = (1, 2) # type: ignore[attr-defined]
test_params_method.a = 1 # type: ignore[attr-defined]


def test_func():
assert 1 == 1


test_func.a = 1
test_func.b = 0
test_func.tags = ["func", "pass"]
test_func.a = 1 # type: ignore[attr-defined]
test_func.b = 0 # type: ignore[attr-defined]
test_func.tags = ["func", "pass"] # type: ignore[attr-defined]


def test_gen():
def check(a, b):
assert a == b

check.tags = ["func"]
check.tags = ["func"] # type: ignore[attr-defined]
for i in range(0, 5):
yield check, (i, i)


test_gen.testGenerator = True
test_gen.tags = ["func"]
test_gen.testGenerator = True # type: ignore[attr-defined]
test_gen.tags = ["func"] # type: ignore[attr-defined]


def test_gen_nose_style():
Expand All @@ -88,19 +88,19 @@ def test_fixt():
assert did_setup


test_fixt.setup = setup
test_fixt.setup = setup # type: ignore[attr-defined]


def test_params_func(a):
assert a == 1


test_params_func.paramList = (1, 2)
test_params_func.tags = ["func"]
test_params_func.paramList = (1, 2) # type: ignore[attr-defined]
test_params_func.tags = ["func"] # type: ignore[attr-defined]


def test_params_func_multi_arg(a, b):
assert a == b


test_params_func_multi_arg.paramList = ((1, 1), (1, 2), (2, 2))
test_params_func_multi_arg.paramList = ((1, 1), (1, 2), (2, 2)) # type: ignore[attr-defined] # noqa: E501
Loading

0 comments on commit 73b0272

Please sign in to comment.