Skip to content

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Sep 22, 2025

One of the remains from my match PRs. One of the great things about match IMO is that it forces the reader to think about the conditional in a quite linear way. Its limitations, no not and and, make it more readable in some cases.

These are some if conditionals which weren't quite enough for match, but updating them might still be worth the effort. E.g.

-if not isinstance(expr, nodes.Attribute) or expr.attrname != "__init__": ...
+if not (isinstance(expr, nodes.Attribute) and expr.attrname == "__init__"): ...

@cdce8p cdce8p added Maintenance Discussion or action around maintaining pylint or the dev workflow Skip news 🔇 This change does not require a changelog entry labels Sep 22, 2025
for stmt in node.nodes_of_class(nodes.Call):
expr = stmt.func
if not isinstance(expr, nodes.Attribute) or expr.attrname != "__init__":
if not (isinstance(expr, nodes.Attribute) and expr.attrname == "__init__"):
Copy link
Member Author

Choose a reason for hiding this comment

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

I do think these changes here are a net improvement, but I'm curious if that's only my opinion or if others think so too.

Though it might be a stretch goal, we could think about if it makes sense to add a checker for these. Haven't looked into it if it would even be practical or if there are just too many conditions to check.

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea was to write them just as I'd write the match case, e.g.

nodes.Attribute(attrname = "__init__")

--
If inline match expressions ever get added, this could be

if not (expr match nodes.Attribute(attrname = "__init__")): ...

Copy link
Member

Choose a reason for hiding this comment

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

Kinda like the final result more, but if the fix is not automated I'm not sure I'm going to bother.

Copy link

codecov bot commented Sep 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.95%. Comparing base (f0b41c0) to head (f3701d7).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #10581      +/-   ##
==========================================
+ Coverage   95.93%   95.95%   +0.02%     
==========================================
  Files         176      176              
  Lines       19471    19456      -15     
==========================================
- Hits        18679    18669      -10     
+ Misses        792      787       -5     
Files with missing lines Coverage Δ
pylint/checkers/base/docstring_checker.py 98.82% <100.00%> (+1.12%) ⬆️
pylint/checkers/classes/class_checker.py 94.01% <100.00%> (ø)
pylint/checkers/logging.py 94.77% <100.00%> (ø)
pylint/checkers/modified_iterating_checker.py 100.00% <100.00%> (+2.12%) ⬆️
pylint/checkers/refactoring/refactoring_checker.py 98.23% <100.00%> (ø)
pylint/checkers/typecheck.py 96.50% <100.00%> (+0.18%) ⬆️
pylint/checkers/utils.py 95.96% <100.00%> (ø)
pylint/checkers/variables.py 97.41% <100.00%> (ø)
pylint/extensions/_check_docs_utils.py 93.69% <100.00%> (-0.03%) ⬇️
pylint/extensions/consider_ternary_expression.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This comment has been minimized.

for stmt in node.nodes_of_class(nodes.Call):
expr = stmt.func
if not isinstance(expr, nodes.Attribute) or expr.attrname != "__init__":
if not (isinstance(expr, nodes.Attribute) and expr.attrname == "__init__"):
Copy link
Member

Choose a reason for hiding this comment

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

Kinda like the final result more, but if the fix is not automated I'm not sure I'm going to bother.

Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit f3701d7

@cdce8p cdce8p merged commit a60b85d into pylint-dev:main Sep 23, 2025
44 checks passed
@cdce8p cdce8p deleted the improve-conditionals branch September 23, 2025 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Maintenance Discussion or action around maintaining pylint or the dev workflow Skip news 🔇 This change does not require a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants