Page MenuHomePhabricator

pep8 E402 vs __version__ and others
Closed, ResolvedPublic

Description

There is a new pep8 rule which was added in July 2014, and will be released soon. If it is deployed on the build machines, it would prevent any other changes from being merged.

The rule is

E402 module level import not at top of file, and looks like

pywikibot/data/api.py:10:1: E402 module level import not at top of file

https://github.com/jcrocholl/pep8/issues/264
https://github.com/jcrocholl/pep8/commit/1ee296bca0fa611d3dbe87c5c5c8009e448d2556

pywikibot has several imports not at the top of the file, due to cyclic dependencies, but that number is small compared to the number of these errors caused by __version__ appearing before the imports.

Note that pep8 also needed to change their code to relocate __version__ to appear below imports.

https://github.com/jcrocholl/pep8/commit/373e0ac1138f0e24422b5e2e78f02ed0557d5c23

There is a broader issue of the usefulness of these __version__ variables for every file, and there is some $Id$ voodoo in the end of bot.py's handle_args which we should revisit.

Event Timeline

jayvdb raised the priority of this task from to High.
jayvdb updated the task description. (Show Details)
jayvdb added a project: Pywikibot.
jayvdb added subscribers: jayvdb, valhallasw, XZise, Xqt.
Restricted Application added subscribers: Aklapper, Unknown Object (MLST). · View Herald TranscriptJan 23 2015, 5:18 AM

Of what usefulness is the version in all files anyway? As far as I could see it isn't the last commit hash that file was changed.

This could also be handled together with T73788 depending on what we plan to do with __version__.

XZise set Security to None.

And another question: Does this affect “conditional imports”, for example when we import depending on whether we need it for Python 2 or Python 3?

And another question: Does this affect “conditional imports”, for example when we import depending on whether we need it for Python 2 or Python 3?

The upcoming version does have dedicated code to allow conditional imports, but we'd need to verify that it allows all of our oddities and file issues if it doesnt when we feel that it should.

Of what usefulness is the version in all files anyway? As far as I could see it isn't the last commit hash that file was changed.

It is the object hash. It could be useful to determine if the file is modified, both locally or via a reported log, but it isnt necessary to achieve that goal either. It may not even be the most efficient way to do it.

$ git hash-object pywikibot/version.py
ce957752e3b8bfb4ecca30c89785b80b93c59c8c
$ grep version pywikibot/version.py
version = '$Id: ce957752e3b8bfb4ecca30c89785b80b93c59c8c $'
$ echo '# foo ' >> pywikibot/version.py
$ git hash-object pywikibot/version.py
bf31be3d89f9f93728390f8e0a37873ad0fa18b6

Okay but I doubt the usefulness. When someone has a problem and gives the version number how does it help? Can I search for a file with the given hash in the git file history? Otherwise I don't see how that hash helps… even if you only want to detect if the file is changed you need to know the original hash and for that you need to know the version the file is in.

I'd say it should be removed any and only used where we actually have a version number like in __init__.__version__ to tell the current package version (if that is not already done automatically). On that topic it would be probably helpful if the nightlies have something like '2.0b3-20150124' so we know when that nightly was build (and if possible maybe the short git hash just to be sure). But again not in every file but only in the __init__.py probably.

IMO it is definitely useless for pywikibot, but it might be useful for versioning of scripts , or reporting script modification status via user-agent ( T57016 ).

I'm not sure how you would see script modifications via the user-agent. You can basically only send the SHA1 (or the first N nibbles) for that. Otherwise you'd need a database with all the SHA1 hashes of the scripts. But then it's probably better if it is done not via the __version__ but by manually generating the hash and comparing it to the database (as __version__ doesn't change when you edit it locally without committing it (or similar)).

For versioning of scripts I'm not sure. There is no sequence so you don't know if a311a20ff092847c90f5064bd1c792487b75cb46 is newer or older than f86e43093a4660aaea2b71d5a6694278d79ef656. And then again there is afaik no “reverse search” to determine the version via the SHA1 has (but git could be configured to use the git hash).

jayvdb added subscribers: hashar, Legoktm.

It appears a new version of pep8 has been deployed, and jenkins is rightly complaining about ... everything!
https://integration.wikimedia.org/ci/job/pywikibot-core-tox-flake8/4758/console

I don't think anything special was deployed on the WMF/CI side, just that the package was pushed to pypi, so tox downloaded the new version which is causing the failures..?

gerritbot subscribed.

Change 189168 had a related patch set uploaded (by John Vandenberg):
Disable pep8 E402

https://gerrit.wikimedia.org/r/189168

Patch-For-Review

I don't think anything special was deployed on the WMF/CI side, just that the package was pushed to pypi, so tox downloaded the new version which is causing the failures..?

Hmm. on my Ubuntu and Fedora Core, these packages managed by the host ... not tox ... and I've checked that neither platform has already upgraded their packages for pep8. There is a tox setting to control that. Now when I run tox, I see

$ tox
ERROR: unknown environment 'flake83'

On my Fedora 20 system I don't get the new errors.

Change 189168 merged by jenkins-bot:
Disable new pep8 v1.6 rules E402 and E731

https://gerrit.wikimedia.org/r/189168

As a start, we can at least remove __version__ from the tests.

As a start, we can at least remove __version__ from the tests.

Does version contain any usefull information anymore. Other than svn where we got a consecutive number which identified the latest commit, we now get any hash number without any benefit. imho this could be removed or it should be replaced by a more informative value.

Note that the pep8 has been adjusted to match our use of __version__, and this will be changing soon in the pep8 checker (now called pycodestyle) - patch at https://github.com/PyCQA/pycodestyle/pull/523.

jayvdb lowered the priority of this task from High to Medium.Jun 8 2016, 12:58 AM
In T87409#2189435, @Xqt wrote:

imho this could be removed or it should be replaced by a more informative value.

+1.

There are only a few scripts that actually use __version__ :

  • bot.py:
    • handle_args prints out module versions in verbose mode
  • version.py:
    • A few functions, e.g. getfileversion, obviously depend on __version__.
  • compat2core.py:
    • Might require a minor update to remove __version__ from converted scripts.

Change 348942 had a related patch set uploaded (by Dalba):
[pywikibot/core@master] Remove version variable from the modules of the test package

https://gerrit.wikimedia.org/r/348942

Change 348942 merged by jenkins-bot:
[pywikibot/core@master] Remove version variable from the modules of the test package

https://gerrit.wikimedia.org/r/348942

Change 363210 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] Remove __version string from scripts

https://gerrit.wikimedia.org/r/363210

Change 363210 merged by jenkins-bot:
[pywikibot/core@master] Remove version string from scripts

https://gerrit.wikimedia.org/r/363210

Change 403599 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [cleanup] Remove version string from scripts/archive/featured.py

https://gerrit.wikimedia.org/r/403599

Change 403599 merged by jenkins-bot:
[pywikibot/core@master] [cleanup] Remove version string from scripts

https://gerrit.wikimedia.org/r/403599

Change 403608 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [cleanup] remove __version__ string from pywikibot framework scripts

https://gerrit.wikimedia.org/r/403608

Change 403608 merged by jenkins-bot:
[pywikibot/core@master] [cleanup] remove __version__ string from pywikibot framework scripts

https://gerrit.wikimedia.org/r/403608

Xqt claimed this task.

@Xqt Should we revert this change which disabled PEP8 checking for import-on-top? (probably only E402)

Change 406406 had a related patch set uploaded (by Xqt; owner: Xqt):
[pywikibot/core@master] [style] Re-enable E402 check

https://gerrit.wikimedia.org/r/406406

Dvorapa changed the task status from Resolved to Declined.Jan 29 2018, 10:18 AM

Well, it is not resolved, just removing version did not help, there are still many errors, to which a solution would not be easy if any solution at all

Mostly broken on constructs like this:

import os
os.environ['PYWIKIBOT2_NO_USER_CONFIG'] = 2
import config
os.environ['PYWIKIBOT2_NO_USER_CONFIG'] = 0
if sys.version_info[0] > 2:
    import python3lib
else:
    import python2lib
try:
    import lib
except:
    print('lib is required')
    sys.exit(1)
Xqt changed the task status from Declined to Resolved.Feb 2 2018, 6:28 AM

This issue is resolved prior by disabling E402 and removing version strings which was the most discussion part. This might be enough for now.

Change 430608 had a related patch set uploaded (by Dalba; owner: Dalba):
[pywikibot/core@master] tox.ini: Enable E402 and disable I201 checks

https://gerrit.wikimedia.org/r/430608

Change 430608 merged by jenkins-bot:
[pywikibot/core@master] tox.ini: Enable E402 and disable I201 checks

https://gerrit.wikimedia.org/r/430608