Skip to content

fix several tests related to constants #10013

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

Merged
merged 3 commits into from
Feb 17, 2025
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
32 changes: 14 additions & 18 deletions pylint/lint/expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,32 +136,28 @@ def expand_modules(
is_namespace = modutils.is_namespace(spec)
is_directory = modutils.is_directory(spec)
if not is_namespace:
if filepath in result:
# Always set arg flag if module explicitly given.
result[filepath]["isarg"] = True
else:
result[filepath] = {
"path": filepath,
"name": modname,
"isarg": True,
"basepath": filepath,
"basename": modname,
"isignored": False,
}
default: ModuleDescriptionDict = {
"path": filepath,
"name": modname,
"isarg": True,
"basepath": filepath,
"basename": modname,
"isignored": False,
}
result.setdefault(filepath, default)["isarg"] = True
has_init = (
not (modname.endswith(".__init__") or modname == "__init__")
and os.path.basename(filepath) == "__init__.py"
modparts[-1] != "__init__" and os.path.basename(filepath) == "__init__.py"
)
if has_init or is_namespace or is_directory:
for subfilepath in modutils.get_module_files(
os.path.dirname(filepath) or ".", ignore_list, list_all=is_namespace
os.path.dirname(filepath) or ".", [], list_all=is_namespace
):
subfilepath = os.path.normpath(subfilepath)
if filepath == subfilepath:
continue
if _is_in_ignore_list_re(
os.path.basename(subfilepath), ignore_list_re
) or _is_in_ignore_list_re(subfilepath, ignore_list_paths_re):
if _is_ignored_file(
subfilepath, ignore_list, ignore_list_re, ignore_list_paths_re
):
result[subfilepath] = {
"path": subfilepath,
"name": "",
Expand Down
10 changes: 8 additions & 2 deletions pylint/testutils/lint_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,16 @@ def error_msg_for_unequal_messages(
)
if missing:
msg.append("\nExpected in testdata:")
msg.extend(f" {msg[0]:3}: {msg[1]}" for msg in sorted(missing))
msg.extend( # pragma: no cover
f" {msg[0]:3}: {msg[1]} (times {times})"
for msg, times in sorted(missing.items())
)
if unexpected:
msg.append("\nUnexpected in testdata:")
msg.extend(f" {msg[0]:3}: {msg[1]}" for msg in sorted(unexpected))
msg.extend(
f" {msg[0]:3}: {msg[1]} (times {times})"
for msg, times in sorted(unexpected.items())
)
error_msg = "\n".join(msg)
if self._config and self._config.getoption("verbose") > 0:
error_msg += "\n\nActual pylint output for this file:\n"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies = [
# Also upgrade requirements_test_min.txt.
# Pinned to dev of second minor update to allow editable installs and fix primer issues,
# see https://github.com/pylint-dev/astroid/issues/1341
"astroid>=3.3.8,<=4.0.0.dev0",
"astroid>=4.0.0a0,<=4.1.0.dev0",
"colorama>=0.4.5; sys_platform=='win32'",
"dill>=0.2; python_version<'3.11'",
"dill>=0.3.6; python_version>='3.11'",
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_min.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.[testutils,spelling]
# astroid dependency is also defined in pyproject.toml
astroid==3.3.8 # Pinned to a specific version for tests
astroid==4.0.0-a0 # Pinned to a specific version for tests
typing-extensions~=4.12
py~=1.11.0
pytest~=8.3
Expand Down
2 changes: 1 addition & 1 deletion tests/checkers/unittest_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestVariablesChecker(CheckerTestCase):

def test_all_elements_without_parent(self) -> None:
node = astroid.extract_node("__all__ = []")
node.value.elts.append(astroid.Const("test"))
node.value.elts.append(astroid.Const("test", parent=None))
root = node.root()
with self.assertNoMessages():
self.checker.visit_module(root)
Comment on lines 23 to 28
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a good test. Why should adding a constant not result in messages? That's just asserting implementation quirks. We should test the opposite, we should test that synthetic constants result in the same messages.
IMO

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# [line-too-long, missing-module-docstring, undefined-all-variable, undefined-all-variable, undefined-all-variable, undefined-all-variable, undefined-all-variable]
# ↓ equivalent to __all__ = ["C", "O", "N", "S", "T"]
__all__ = list("CONST")
#
CONST = 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
line-too-long:1:0:None:None::Line too long (163/100):UNDEFINED
missing-module-docstring:1:0:None:None::Missing module docstring:HIGH
undefined-all-variable:1:0:None:None::Undefined variable name 'C' in __all__:UNDEFINED
undefined-all-variable:1:0:None:None::Undefined variable name 'N' in __all__:UNDEFINED
undefined-all-variable:1:0:None:None::Undefined variable name 'O' in __all__:UNDEFINED
undefined-all-variable:1:0:None:None::Undefined variable name 'S' in __all__:UNDEFINED
undefined-all-variable:1:0:None:None::Undefined variable name 'T' in __all__:UNDEFINED
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# [line-too-long, missing-module-docstring, undefined-all-variable, undefined-all-variable, undefined-all-variable, undefined-all-variable, undefined-all-variable]
# ↓ equivalent to __all__ = ("C", "O", "N", "S", "T")
__all__ = tuple("CONST")

CONST = 42
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
line-too-long:1:0:None:None::Line too long (163/100):UNDEFINED
missing-module-docstring:1:0:None:None::Missing module docstring:HIGH
undefined-all-variable:1:0:None:None::Undefined variable name 'C' in __all__:UNDEFINED
undefined-all-variable:1:0:None:None::Undefined variable name 'N' in __all__:UNDEFINED
undefined-all-variable:1:0:None:None::Undefined variable name 'O' in __all__:UNDEFINED
undefined-all-variable:1:0:None:None::Undefined variable name 'S' in __all__:UNDEFINED
undefined-all-variable:1:0:None:None::Undefined variable name 'T' in __all__:UNDEFINED
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test valid __all__ format."""
__all__ = list("CONST")

__all__ = list(["CONST"])

CONST = 42
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test valid __all__ format."""
__all__ = tuple("CONST")

__all__ = tuple(["CONST"])

CONST = 42
22 changes: 15 additions & 7 deletions tests/lint/unittest_expand_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,28 @@ def test__is_in_ignore_list_re_match() -> None:
# A directory that is not a python package.
REPORTERS_PATH = Path(__file__).parent.parent / "reporters"
test_reporters = { # pylint: disable=consider-using-namedtuple-or-dataclass
str(REPORTERS_PATH / "unittest_json_reporter.py"): {
"path": str(REPORTERS_PATH / "unittest_json_reporter.py"),
"name": "reporters.unittest_json_reporter",
"isarg": False,
str(REPORTERS_PATH / "__init__.py"): {
"basename": "reporters",
"basepath": str(REPORTERS_PATH / "__init__.py"),
"isarg": True,
"name": "reporters",
"path": str(REPORTERS_PATH / "__init__.py"),
"isignored": False,
},
str(REPORTERS_PATH / "unittest_json_reporter.py"): {
"basename": "reporters",
"basepath": str(REPORTERS_PATH / "__init__.py"),
"isarg": False,
"name": "reporters.unittest_json_reporter",
"path": str(REPORTERS_PATH / "unittest_json_reporter.py"),
"isignored": False,
},
str(REPORTERS_PATH / "unittest_reporting.py"): {
"basename": "reporters",
"basepath": str(REPORTERS_PATH / "__init__.py"),
"isarg": False,
"path": str(REPORTERS_PATH / "unittest_reporting.py"),
"name": "reporters.unittest_reporting",
"isarg": False,
"basepath": str(REPORTERS_PATH / "__init__.py"),
"basename": "reporters",
"isignored": False,
},
}
Expand Down
Empty file added tests/reporters/__init__.py
Empty file.