Bug 158803 - Fix issues found by pyflakes
Summary: Fix issues found by pyflakes
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard: target:24.8.0
Keywords: difficultyBeginner, easyHack, skillPython, topicCleanup
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2023-12-21 09:56 UTC by Hossein
Modified: 2024-03-30 19:36 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hossein 2023-12-21 09:56:35 UTC
Description:
There are Python scripts for various purposes in LibreOffice: from UITests to wizards, SDK examples and other places. This task is defined to do static code checking on Python scripts to do cleanup and fix some errors as they are found.

Pyflakes is a tool for checking Python files for various problems. It can be installed using pip:

Pyflakes: A simple program which checks Python source files for errors 
https://github.com/PyCQA/pyflakes

Some of the issues reported by Pyflakes are:

* Unused imports
* Unnecessary re-imports
* Undefined variables
* Name Shadowing
* Syntax Errors

And many other issues, some of them described here:
https://github.com/PyCQA/pyflakes/issues?q=is%3Aissue+is%3Aclosed

At first, you have to run pyflakes to find those issues, and then fix them one by one. If you go to a specific path, and invoke "pyflakes .", it will try finding Python scripts inside the current folder, and report the errors. Then, you have to fix them one by one.

As an example, let's check a test inside "unotest" folder:
$ pyflakes sw/qa/uitest/writer_tests7/tdf156900.py
sw/qa/uitest/writer_tests7/tdf156900.py:18:13: local variable 'xToolkit' is assigned to but never used

The output shows there are some unused local variable. To fix it, this line should be removed:

xToolkit = self.xContext.ServiceManager.createInstance('com.sun.star.awt.Toolkit')

Some unused imports must remain, if they are there to test if importing something is actually possible, and give error message if it is not.

We have another issue for pyflakes, but it is specifically about finding unused imports in UITests:

tdf#132293 - Run pyflakes on python uitest files to find unused imports
https://bugs.documentfoundation.org/show_bug.cgi?id=132293

Therefore, if you are working on unused imports for UITests only, use the above issue instead. This new one is about all the other issues, and also Python scripts beyond UITests.
Comment 1 Commit Notification 2023-12-27 12:49:12 UTC
Chenxiong Qi committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/8b6e0dd9e47c20a79fd194f6d530d1c868911bda

tdf#158803 Fix issues detected by pyflakes

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 2 Commit Notification 2023-12-27 16:51:39 UTC
Bogdan B committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/f727a1bc203369dc33f912cbf9f0fc0e6bc67ec8

tdf#158803 Remove unused imports from desktop

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 3 Commit Notification 2023-12-29 08:55:19 UTC
Bogdan B committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/605c2888786b02e34ab586aa462cc1e84843dad2

tdf#158803 Remove unused imports from bin

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 4 Commit Notification 2023-12-29 16:34:29 UTC
Bogdan B committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/6e67802648356a00cdbe9e2e6770199e227a03cd

tdf#158803 Remove unused imports from odk

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 5 Commit Notification 2023-12-29 22:57:04 UTC
Bogdan B committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/4871de96cb5e31e5ab06cf97e02e09e0e04a4de8

tdf#158803 Remove unused imports from wizards

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 6 Commit Notification 2024-03-28 07:45:14 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/3f7923ebac4c87aa2966e6f19b506dfeda8fbdb8

tdf#158803 fix typo NotImplementedErrors -> NotImplementedError

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 7 Commit Notification 2024-03-28 07:45:17 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/8a92d296bee8ce3b010fab47c48d23f68c4b25a0

tdf#158803 filename is undefined, should be self.root

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 8 Commit Notification 2024-03-28 07:45:19 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/623ae0a44f55ca4bf6f99a0ea0e0dbe55a95ec67

tdf#158803 remove unused maths import

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 9 Commit Notification 2024-03-28 07:46:22 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/2db078c582fb254bc02322af41bda2a43dd91313

tdf#158803 statement ends with an unnessary semicolon

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 10 Commit Notification 2024-03-28 07:46:24 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/d305470db4fef3b497675c3f5067f74e8333c652

tdf#158803 unncessary semicolons

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 11 Commit Notification 2024-03-28 07:47:27 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/9f4e04b2582971759d2eabb142d4fb1293bf00cc

tdf#158803 remove unused imports

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 12 Commit Notification 2024-03-28 07:47:29 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/845dbe38844d90d4313652a7b390f3715fce6d7e

tdf#158803 remove unused imports

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 13 Commit Notification 2024-03-28 07:48:32 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/9d4844374395ecce6b2ff4f9a71b3c8dae23050a

tdf#158803 remove some unused imports, unnecessary semicolons and related styling

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 14 Commit Notification 2024-03-28 07:48:34 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/21fc89c651a8adece2571bf2e9b25a3bf0cd4f61

tdf#158803 unncessary semicolons

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 15 Commit Notification 2024-03-28 07:49:37 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/b470044cbf116b825ca86c1b054aaaaea7647646

tdf#158803 test for membership should be 'not in'

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 16 Commit Notification 2024-03-28 07:49:39 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/9e424d98e5af9f6219398e02cb05c454471c237d

tdf#158803 test for membership should be 'not in', unnecessary semicolons

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 17 Commit Notification 2024-03-28 07:49:42 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/84188d15656a8b309216d4eba2fc47787ebcdf3d

tdf#158803 fix fstring

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 18 Commit Notification 2024-03-28 07:50:45 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/5e80aa818cae690c67361e85bd37124cec16c05e

tdf#158803 test for membership should be 'not in', unnecessary semicolons

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 19 Commit Notification 2024-03-28 07:50:47 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/d4aef7fd04a1ee999b03194c01f4a792aa1846d8

tdf#158803 unncessary semicolons

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 20 Commit Notification 2024-03-28 07:50:49 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/07efbe8f0bb46bba81396d081c456a09c93f842a

tdf#158803 colons missing for print statement, replacing basestring with str

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 21 Commit Notification 2024-03-28 07:51:52 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/12efa6c63c2bee0d74e378289dd43bbc58c01781

tdf#158803 remove unused path.convert_to_unix import

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 22 Commit Notification 2024-03-28 07:51:55 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/7397de2ec5f9f3c2b42861bbfb344c425ffba9ea

tdf#158803 unnecessary semicolons

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 23 Commit Notification 2024-03-28 07:52:57 UTC
LeSasse committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/e347cff87bec2e8d802fb2dfb8abd3d7d94f8103

tdf#158803 remove unused imports

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 24 Commit Notification 2024-03-28 09:11:07 UTC
Noel Grandin committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/147063f58c6b7898e659321f581ea9551305a574

Revert "tdf#158803 remove some unused imports, unnecessary semicolons and related styling"

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 25 Leonard Sasse 2024-03-28 10:32:14 UTC
(In reply to Hossein from comment #0)
> Description:
> There are Python scripts for various purposes in LibreOffice: from UITests
> to wizards, SDK examples and other places. This task is defined to do static
> code checking on Python scripts to do cleanup and fix some errors as they
> are found.
> 
> Pyflakes is a tool for checking Python files for various problems. It can be
> installed using pip:
> 
> Pyflakes: A simple program which checks Python source files for errors 
> https://github.com/PyCQA/pyflakes
> 
> Some of the issues reported by Pyflakes are:
> 
> * Unused imports
> * Unnecessary re-imports
> * Undefined variables
> * Name Shadowing
> * Syntax Errors
> 
> And many other issues, some of them described here:
> https://github.com/PyCQA/pyflakes/issues?q=is%3Aissue+is%3Aclosed
> 
> At first, you have to run pyflakes to find those issues, and then fix them
> one by one. If you go to a specific path, and invoke "pyflakes .", it will
> try finding Python scripts inside the current folder, and report the errors.
> Then, you have to fix them one by one.
> 
> As an example, let's check a test inside "unotest" folder:
> $ pyflakes sw/qa/uitest/writer_tests7/tdf156900.py
> sw/qa/uitest/writer_tests7/tdf156900.py:18:13: local variable 'xToolkit' is
> assigned to but never used
> 
> The output shows there are some unused local variable. To fix it, this line
> should be removed:
> 
> xToolkit =
> self.xContext.ServiceManager.createInstance('com.sun.star.awt.Toolkit')
> 
> Some unused imports must remain, if they are there to test if importing
> something is actually possible, and give error message if it is not.
> 
> We have another issue for pyflakes, but it is specifically about finding
> unused imports in UITests:
> 
> tdf#132293 - Run pyflakes on python uitest files to find unused imports
> https://bugs.documentfoundation.org/show_bug.cgi?id=132293
> 
> Therefore, if you are working on unused imports for UITests only, use the
> above issue instead. This new one is about all the other issues, and also
> Python scripts beyond UITests.

I would propose even extending the scope of this issue and to fix issues raised by ruff: https://github.com/astral-sh/ruff

ruff is a superset of multiple python linting tools and extremely fast. Even when I run it on the whole LibreOffice/core repo, it is finished checking almost instantaneously, whereas other tools such as pyflakes take quite a bit longer. This fast execution also makes ruff a good candidate to be included in the CI to make sure that these kinds of errors don't creep back into the code base.
Comment 26 Commit Notification 2024-03-28 15:13:53 UTC
Bogdan Buzea committed a patch related to this issue.
It has been pushed to "master":

https://git.libreoffice.org/core/commit/5fa3abd3b28b7c30d015dc058f7719112003f51e

tdf#158803 Remove unused imports from uitest

It will be available in 24.8.0.

The patch should be included in the daily builds available at
https://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
https://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 27 Leonard Sasse 2024-03-30 19:36:48 UTC
As a follow up on my previous comment about using ruff as a potential linter in the CI, it can also be used to format the python code uniformly (and checking that formatting is consistent with the standard in the CI as well).

On the easy hacks page (https://wiki.documentfoundation.org/Development/EasyHacks)
it is stated, that:

> Please avoid larger reformatting of the code for the time being (except for the tasks listed below) - we're pondering auto/magic ways to do that mid- to long-term.

Hence, I thought I would suggest this, as I have noticed that formatting in the different modules is quite inconsistent and in my opinion ruff (which can be quite nicely configured to fit the idiosyncratic needs of any given project) will be an excellent tool to ensure a consistent formatting standard.