Skip to content

[fix] AttributeError crash when a slice is used as a class decorator #10350

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

Conversation

Pierre-Sassoulas
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas commented Apr 23, 2025

Type of Changes

Type
βœ“ πŸ› Bug fix

Description

This was partially implemented by an AI bot (codegen-bot), credited as co-author in the commit. I've done some chores (moving functional tests, fixing the new test, creating the changelog, checking that the changes are actually required and that the not required changes are not actually another crash), and some I did not (creating branches, it could have opened the PR if I wasn't working on a fork, creating the regression tests, making the initial fix). Tried to kept the history truthful to what happened. Overall not sure if it's more work or less work than just copy pasting the regression code and fixing myself (probably more), but it certainly feel new and exciting.

Suggestions on what to try next with this AI bot are welcome, I started with "noqa" handling but it seems a little too complicated. I probably should use this on task with less finesse and more grunt work, like refactor (?)

Closes #10334

@Pierre-Sassoulas Pierre-Sassoulas added this to the 3.3.7 milestone Apr 23, 2025
@Pierre-Sassoulas Pierre-Sassoulas force-pushed the codegen-bot/fix-slice-decorator-crash branch from 68b72f9 to 49baeb3 Compare April 23, 2025 06:44
@Pierre-Sassoulas Pierre-Sassoulas force-pushed the codegen-bot/fix-slice-decorator-crash branch from 1e5e8c1 to d3c29cc Compare April 23, 2025 07:01
@Pierre-Sassoulas Pierre-Sassoulas force-pushed the codegen-bot/fix-slice-decorator-crash branch from d3c29cc to 754cfcb Compare April 23, 2025 07:03
Copy link

codecov bot commented Apr 23, 2025

Codecov Report

All modified and coverable lines are covered by tests βœ…

Project coverage is 95.90%. Comparing base (8a733fc) to head (5b30d91).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10350   +/-   ##
=======================================
  Coverage   95.90%   95.90%           
=======================================
  Files         176      176           
  Lines       19122    19122           
=======================================
  Hits        18339    18339           
  Misses        783      783           
Files with missing lines Coverage Ξ”
pylint/checkers/deprecated.py 99.06% <100.00%> (ΓΈ)
pylint/checkers/utils.py 95.92% <ΓΈ> (ΓΈ)
πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request fixes an AttributeError crash that occurred when a slice object was used as a class decorator. The changes include the addition of regression tests to reproduce the issue and adjustments in the Pylint checkers to prevent incorrect attribute access during inference.

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.

File Description
tests/functional/r/regression_02/regression_10334.txt Adds expected test output indicating non-callable error.
tests/functional/r/regression_02/regression_10334.py Provides a test case using a slice as a decorator.
pylint/checkers/utils.py Updates attribute checks to safely verify presence of qname.
pylint/checkers/deprecated.py Adds a qname attribute check to avoid AttributeError.
Files not reviewed (1)
  • doc/whatsnew/fragments/10334.bugfix: Language not supported

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas requested a review from cdce8p April 23, 2025 19:39
Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Indeed, I would probably prefer to avoid all the hasattr calls. Is that possible?

@Pierre-Sassoulas
Copy link
Member Author

The one I managed to avoid was guarded by a rather restrictive isinstance check, maybe we could do that instead. (It's protective but also we risk to have issue opened for it to add use case one by one if we were too protective). Or do you have another way to do it ?

@DanielNoord
Copy link
Collaborator

I think that would probably be the best course of action here?

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas force-pushed the codegen-bot/fix-slice-decorator-crash branch from 5f91b3d to 5b30d91 Compare April 25, 2025 20:12

This comment has been minimized.

DanielNoord
DanielNoord previously approved these changes Apr 26, 2025
Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

We can always change back to hasattr. Let's just try this first

@Pierre-Sassoulas
Copy link
Member Author

My concern is that we might add a bunch of false negatives (even false positive ?) as we're affecting at least 20 messages or more (based on a quick look up of checker files where this helper is used). Not something we should risk in a patch (especially a x.y.6 patch which should be super stable imo). hasattr prevents the crash and has no side effect except being slower.

@DanielNoord
Copy link
Collaborator

Hmm I think the tests are probably a good indication that we are still covering the most common patterns. But up to you! I don't have a super strong preference other than that I do think we should use isinstance in the "final" version.

cdce8p
cdce8p previously approved these changes May 1, 2025
@cdce8p
Copy link
Member

cdce8p commented May 1, 2025

My concern is that we might add a bunch of false negatives (even false positive ?) as we're affecting at least 20 messages or more (based on a quick look up of checker files where this helper is used). Not something we should risk in a patch (especially a x.y.6 patch which should be super stable imo). hasattr prevents the crash and has no side effect except being slower.

Not sure that's really an issue here. The decorated_with functions checks if something is decorated with a specific decorator. If the inferred node isn't a ClassDef or FunctionDef, it's most likely not a decorator we're looking for. AFAICT from astroid, there are only a handful other classes with qname(). Mainly those for partial functions, lambdas and unknown. If we miss one of those, i.e. a new false-negative, so be it πŸ€·πŸ»β€β™‚οΈ If a true error arises, we can still go back and fix it with x.y.7. Should be fairly simple since we already now were to look.

@Pierre-Sassoulas Pierre-Sassoulas dismissed stale reviews from cdce8p and DanielNoord via 6f7ab65 May 1, 2025 20:07
@Pierre-Sassoulas
Copy link
Member Author

Fair enough :) Added "A lot of checks that dealt with decorators (too many to list) are now shortcut if the decorator can't immediately be inferred to a function or class definition." to the changelog.

@Pierre-Sassoulas Pierre-Sassoulas enabled auto-merge (squash) May 1, 2025 20:22
Copy link
Contributor

github-actions bot commented May 1, 2025

πŸ€– According to the primer, this change has no effect on the checked open source code. πŸ€–πŸŽ‰

This comment was generated for commit 6f7ab65

@cdce8p cdce8p disabled auto-merge May 1, 2025 21:27
@cdce8p cdce8p merged commit 77481a6 into pylint-dev:main May 1, 2025
49 of 51 checks passed
Copy link
Contributor

github-actions bot commented May 1, 2025

The backport to maintenance/3.3.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-maintenance/3.3.x maintenance/3.3.x
# Navigate to the new working tree
cd .worktrees/backport-maintenance/3.3.x
# Create a new branch
git switch --create backport-10350-to-maintenance/3.3.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 77481a6e36fa7fa6b8530e9a300af840867e4df4
# Push it to GitHub
git push --set-upstream origin backport-10350-to-maintenance/3.3.x
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-maintenance/3.3.x

Then, create a pull request where the base branch is maintenance/3.3.x and the compare/head branch is backport-10350-to-maintenance/3.3.x.

@Pierre-Sassoulas Pierre-Sassoulas deleted the codegen-bot/fix-slice-decorator-crash branch May 1, 2025 21:41
Pierre-Sassoulas added a commit that referenced this pull request May 2, 2025
…10350)

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
Co-authored-by: Marc Mueller <[email protected]>
@Pierre-Sassoulas
Copy link
Member Author

Did the backport manually in #10361

cdce8p added a commit that referenced this pull request May 2, 2025
…10350) (#10361)

Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com>
Co-authored-by: Marc Mueller <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AttributeError: 'Slice' object has no attribute 'name' when a slice is used as a class decorator
3 participants