Skip to content

Implement late-binding loop check#265

Merged
cooperlees merged 5 commits into
PyCQA:mainfrom
Zac-HD:late-binding-closures
Jul 1, 2022
Merged

Implement late-binding loop check#265
cooperlees merged 5 commits into
PyCQA:mainfrom
Zac-HD:late-binding-closures

Conversation

@Zac-HD
Copy link
Copy Markdown
Member

@Zac-HD Zac-HD commented Jun 23, 2022

Closes #263.

  • Avoid duplicate warnings when inside nested loops (i.e. dedupe before appending)
  • Only warn on names used in a Load context, e.g. ... for _ in ... is OK in a function, even if _ is also a loop variable
  • Assigning to attributes or indexed items should not be considered an access, e.g. self.count += 1 does not make self a loop variable. I'm generally fine ruling that detecting bugs around attributes is out of scope, but the false-alarms need to be fixed

I've run this over our codebase at work (the motivating example 😅), and there are only a few false-positives where the function is used and then discarded within the loop iteration that defines it. These cases are also infeasible to reliably detect, so I'm comfortable leaving them to be handled by either manual var=var capture or # noqa comments.

@Zac-HD Zac-HD force-pushed the late-binding-closures branch 3 times, most recently from ea64354 to 249dc66 Compare June 23, 2022 23:41
@Zac-HD Zac-HD force-pushed the late-binding-closures branch from 249dc66 to 69df590 Compare June 23, 2022 23:47
Zac-HD added 4 commits June 23, 2022 17:36
i.e. don't complain about the default value of arguments, since that's an explicit solution to late binding!
@Zac-HD Zac-HD requested a review from cooperlees June 24, 2022 08:04
@Zac-HD
Copy link
Copy Markdown
Member Author

Zac-HD commented Jul 1, 2022

Ping @cooperlees?

Copy link
Copy Markdown
Collaborator

@cooperlees cooperlees left a comment

Choose a reason for hiding this comment

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

Sorry about the delay. Been busy.

Makes sense to me. Includes Carl's suggestion + well documented + tested

  • Plus links to a great doc for people to read to fully understand (like I did)

I feel this is going to find a lot of cases doing this. So lets release this on it's own and wreak havoc.

@cooperlees cooperlees merged commit aaad1d6 into PyCQA:main Jul 1, 2022
@Zac-HD Zac-HD deleted the late-binding-closures branch July 1, 2022 18:13
@Zac-HD
Copy link
Copy Markdown
Member Author

Zac-HD commented Jul 1, 2022

No worries, life happens! Bring on the havoc, we'll see if people are complaining about false-alarms or just alarms they don't like 😁

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Check for late-binding closures (e.g. lambdas) in loops

2 participants