Skip to content

Allow different test output for different Python versions #10382

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions doc/development_guide/contributor_guide/tests/writing_test.rst
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ test runner. The following options are currently supported:
- "except_implementations": List of python implementations on which the test should not run
- "exclude_platforms": List of operating systems on which the test should not run

**Different output for different Python versions**

Sometimes the linting result can change between Python releases. In these cases errors can be marked as conditional.
Supported operators are ``<``, ``<=``, ``>`` and ``>=``.

.. code-block:: python

def some_func() -> X: # <3.14:[undefined-variable]
...

class X: ...

Since the output messages are different, it is necessary to add two separate files for it.
First ``<test-file-name>.314.txt``, this will include the output messages for ``>=3.14``, i.e. should be empty here.
Second ``<test-file-name>.txt``, this will be the default for all other Python versions.

.. note::

This does only work if the code itself is valid in all tested Python versions.
For new syntax, use ``min_pyver`` / ``max_pyver`` instead.

**Functional test file locations**

For existing checkers, new test cases should preferably be appended to the existing test file.
Expand Down
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/10382.internal
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Modified test framework to allow for different test output for different Python versions.

Refs #10382
18 changes: 16 additions & 2 deletions pylint/testutils/functional/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from __future__ import annotations

import configparser
import sys
from collections.abc import Callable
from os.path import basename, exists, join
from os.path import basename, exists, join, split
from pathlib import Path
from typing import TypedDict


Expand Down Expand Up @@ -99,7 +101,19 @@ def module(self) -> str:

@property
def expected_output(self) -> str:
return self._file_type(".txt", check_exists=False)
files = [
p.stem
for p in Path(self._directory).glob(f"{split(self.base)[-1]}.[0-9]*.txt")
]
# pylint: disable-next=bad-builtin
current_version = int("".join(map(str, sys.version_info[:2])))
output_options = [
int(version) for s in files if (version := s.rpartition(".")[2]).isalnum()
]
for opt in sorted(output_options, reverse=True):
if current_version >= opt:
return join(self._directory, f"{self.base}.{opt}.txt")
return join(self._directory, self.base + ".txt")

@property
def source(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import typing as t

a1: t.Generator[int, str, str]
a2: t.Generator[int, None, None]
a2: t.Generator[int, None, None] # >=3.13:[unnecessary-default-type-args]
a3: t.Generator[int]
b1: t.AsyncGenerator[int, str]
b2: t.AsyncGenerator[int, None]
b2: t.AsyncGenerator[int, None] # >=3.13:[unnecessary-default-type-args]
b3: t.AsyncGenerator[int]

c1: ca.Generator[int, str, str]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
[main]
py-version=3.10
load-plugins=pylint.extensions.typing

This file was deleted.

This file was deleted.

Loading