diff options
author | thegeorg <thegeorg@yandex-team.com> | 2023-08-24 13:46:13 +0300 |
---|---|---|
committer | thegeorg <thegeorg@yandex-team.com> | 2023-08-24 14:01:39 +0300 |
commit | a0e8ac430192b8b6acda11beccd9abe68ccd5fba (patch) | |
tree | a96631f8d231132eee007d075128b07fa43b9fb5 | |
parent | b3702478a961fe7ab5fc435582ff9fe75b8a0460 (diff) | |
download | ydb-a0e8ac430192b8b6acda11beccd9abe68ccd5fba.tar.gz |
Fix some style issues in library/python/pytest
Two issues remain unfixed:
```
[fail] library/python/pytest/rewrite.py:96: [PLW0122] Use of exec (exec-used)
[fail] library/python/pytest/yatest_tools.py:49: [F821] undefined name 'unicode'
```
-rw-r--r-- | library/python/pytest/main.py | 25 | ||||
-rw-r--r-- | library/python/pytest/rewrite.py | 10 | ||||
-rw-r--r-- | library/python/pytest/ya.make | 2 | ||||
-rw-r--r-- | library/python/pytest/yatest_tools.py | 82 |
4 files changed, 91 insertions, 28 deletions
diff --git a/library/python/pytest/main.py b/library/python/pytest/main.py index 44046683eb..6059ff45a3 100644 --- a/library/python/pytest/main.py +++ b/library/python/pytest/main.py @@ -9,6 +9,7 @@ FORCE_EXIT_TESTSFAILED_ENV = 'FORCE_EXIT_TESTSFAILED' def main(): import library.python.pytest.context as context + context.Ctx["YA_PYTEST_START_TIMESTAMP"] = time.time() profile = None @@ -17,6 +18,7 @@ def main(): import pstats import cProfile + profile = cProfile.Profile() profile.enable() @@ -42,14 +44,18 @@ def main(): import _pytest.assertion from _pytest.monkeypatch import MonkeyPatch from . import rewrite + m = MonkeyPatch() m.setattr(_pytest.assertion.rewrite, "AssertionRewritingHook", rewrite.AssertionRewritingHook) prefix = '__tests__.' test_modules = [ - name[len(prefix):] for name in sys.extra_modules + # fmt: off + name[len(prefix) :] + for name in sys.extra_modules if name.startswith(prefix) and not name.endswith('.conftest') + # fmt: on ] doctest_packages = __res.find("PY_DOCTEST_PACKAGES") or "" @@ -64,8 +70,11 @@ def main(): return False doctest_modules = [ - name for name in sys.extra_modules + # fmt: off + name + for name in sys.extra_modules if is_doctest_module(name) + # fmt: on ] def remove_user_site(paths): @@ -85,11 +94,13 @@ def main(): return new_paths sys.path = remove_user_site(sys.path) - rc = pytest.main(plugins=[ - collection.CollectionPlugin(test_modules, doctest_modules), - ya, - conftests, - ]) + rc = pytest.main( + plugins=[ + collection.CollectionPlugin(test_modules, doctest_modules), + ya, + conftests, + ] + ) if rc == 5: # don't care about EXIT_NOTESTSCOLLECTED diff --git a/library/python/pytest/rewrite.py b/library/python/pytest/rewrite.py index 4cebcb1edd..38e80ebf5d 100644 --- a/library/python/pytest/rewrite.py +++ b/library/python/pytest/rewrite.py @@ -6,6 +6,7 @@ import ast import py from _pytest.assertion import rewrite + try: import importlib.util except ImportError: @@ -66,10 +67,10 @@ class AssertionRewritingHook(rewrite.AssertionRewritingHook): co = self._find_module(name, path) if co is not None: return importlib.util.spec_from_file_location( - name, - co.co_filename, - loader=self, - ) + name, + co.co_filename, + loader=self, + ) def _should_rewrite(self, name, fn, state): if name.startswith("__tests__.") or name.endswith(".conftest"): @@ -84,6 +85,7 @@ class AssertionRewritingHook(rewrite.AssertionRewritingHook): return importer.get_source(name) if six.PY3: + def load_module(self, module): co, _ = self.modules.pop(module.__name__) try: diff --git a/library/python/pytest/ya.make b/library/python/pytest/ya.make index f1b68bdbef..0a2b87360e 100644 --- a/library/python/pytest/ya.make +++ b/library/python/pytest/ya.make @@ -9,6 +9,8 @@ PY_SRCS( yatest_tools.py ) +STYLE_PYTHON() + PEERDIR( contrib/python/attrs contrib/python/python-dateutil diff --git a/library/python/pytest/yatest_tools.py b/library/python/pytest/yatest_tools.py index 0c3138a9fa..eb4818d2f7 100644 --- a/library/python/pytest/yatest_tools.py +++ b/library/python/pytest/yatest_tools.py @@ -16,7 +16,19 @@ TEST_MOD_PREFIX = '__tests__.' class Subtest(object): - def __init__(self, name, test_name, status, comment, elapsed, result=None, test_type=None, logs=None, cwd=None, metrics=None): + def __init__( + self, + name, + test_name, + status, + comment, + elapsed, + result=None, + test_type=None, + logs=None, + cwd=None, + metrics=None, + ): self._name = name self._test_name = test_name self.status = status @@ -48,20 +60,21 @@ class Subtest(object): return yatest_lib.tools.to_utf8(self._test_name) def __repr__(self): - return "Subtest [{}::{} - {}[{}]: {}]".format(self.name, self.test_name, self.status, self.elapsed, self.comment) + return "Subtest [{}::{} - {}[{}]: {}]".format( + self.name, self.test_name, self.status, self.elapsed, self.comment + ) def __hash__(self): return hash(str(self)) class SubtestInfo(object): - skipped_prefix = '[SKIPPED] ' @classmethod def from_str(cls, s): if s.startswith(SubtestInfo.skipped_prefix): - s = s[len(SubtestInfo.skipped_prefix):] + s = s[len(SubtestInfo.skipped_prefix) :] skipped = True else: @@ -94,10 +107,32 @@ class Status(object): NOT_LAUNCHED = -200 CANON_DIFF = -300 FLAKY = -1 - BY_NAME = {'good': GOOD, 'fail': FAIL, 'xfail': XFAIL, 'xpass': XPASS, 'missing': MISSING, 'crashed': CRASHED, - 'skipped': SKIPPED, 'flaky': FLAKY, 'not_launched': NOT_LAUNCHED, 'timeout': TIMEOUT, 'diff': CANON_DIFF} - TO_STR = {GOOD: 'good', FAIL: 'fail', XFAIL: 'xfail', XPASS: 'xpass', MISSING: 'missing', CRASHED: 'crashed', - SKIPPED: 'skipped', FLAKY: 'flaky', NOT_LAUNCHED: 'not_launched', TIMEOUT: 'timeout', CANON_DIFF: 'diff'} + BY_NAME = { + 'good': GOOD, + 'fail': FAIL, + 'xfail': XFAIL, + 'xpass': XPASS, + 'missing': MISSING, + 'crashed': CRASHED, + 'skipped': SKIPPED, + 'flaky': FLAKY, + 'not_launched': NOT_LAUNCHED, + 'timeout': TIMEOUT, + 'diff': CANON_DIFF, + } + TO_STR = { + GOOD: 'good', + FAIL: 'fail', + XFAIL: 'xfail', + XPASS: 'xpass', + MISSING: 'missing', + CRASHED: 'crashed', + SKIPPED: 'skipped', + FLAKY: 'flaky', + NOT_LAUNCHED: 'not_launched', + TIMEOUT: 'timeout', + CANON_DIFF: 'diff', + } class Test(object): @@ -152,6 +187,7 @@ COLOR_THEME = { class YaCtx(object): pass + ya_ctx = YaCtx() TRACE_FILE_NAME = "ytest.report.trace" @@ -223,11 +259,21 @@ def get_unique_file_path(dir_path, filename, cache=collections.defaultdict(set)) filename_len = len(dir_path) + len(extension) + tail_length + len(os.sep) if filename_len < max_path: filename = yatest_lib.tools.trim_string(filename, max_path - filename_len) - filename = yatest_lib.tools.trim_string(filename, get_max_filename_length(dir_path) - tail_length - len(extension)) + extension + filename = ( + yatest_lib.tools.trim_string(filename, get_max_filename_length(dir_path) - tail_length - len(extension)) + + extension + ) candidate = os.path.join(dir_path, filename) key = dir_path + filename - counter = sorted(cache.get(key, {0, }))[-1] + counter = sorted( + cache.get( + key, + { + 0, + }, + ) + )[-1] while os.path.exists(candidate): cache[key].add(counter) counter += 1 @@ -258,8 +304,8 @@ def normalize_name(name): ("\t", "\\t"), ("\r", "\\r"), ] - for l, r in replacements: - name = name.replace(l, r) + for from_, to in replacements: + name = name.replace(from_, to) return name @@ -328,7 +374,7 @@ def _suffix_test_modules_tree(): if not module.startswith(TEST_MOD_PREFIX): continue - module = module[len(TEST_MOD_PREFIX):] + module = module[len(TEST_MOD_PREFIX) :] node = root for name in reversed(module.split('.')): @@ -364,7 +410,7 @@ def _unify_path(path): node, res = suff_tree, [] assert path.endswith(py_ext), path - parts = path[:-len(py_ext)].split(SEP) + parts = path[: -len(py_ext)].split(SEP) # Use SEP as trailing terminator to make an extra step # and find a proper match when parts is a full matching path @@ -400,7 +446,7 @@ def colorize_pytest_error(text): for pos in range(err_start + 1, len(text) - 1): if text[pos] == '\n': - if not text[pos + 1:].startswith(error_prefix): + if not text[pos + 1 :].startswith(error_prefix): err_end = pos + 1 break else: @@ -410,8 +456,10 @@ def colorize_pytest_error(text): filters = [ # File path, line number and function name - (re.compile(r"^(.*?):(\d+): in (\S+)", flags=re.MULTILINE), - r"[[unimp]]\1[[rst]]:[[alt2]]\2[[rst]]: in [[alt1]]\3[[rst]]"), + ( + re.compile(r"^(.*?):(\d+): in (\S+)", flags=re.MULTILINE), + r"[[unimp]]\1[[rst]]:[[alt2]]\2[[rst]]: in [[alt1]]\3[[rst]]", + ), ] for regex, substitution in filters: bt = regex.sub(substitution, bt) |