Skip to content
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

Porting Python 2 to Python 3 example contradicts its own advice #78275

Closed
BruceRichardson mannequin opened this issue Jul 11, 2018 · 3 comments
Closed

Porting Python 2 to Python 3 example contradicts its own advice #78275

BruceRichardson mannequin opened this issue Jul 11, 2018 · 3 comments
Labels
docs Documentation in the Doc dir

Comments

@BruceRichardson
Copy link
Mannequin

BruceRichardson mannequin commented Jul 11, 2018

BPO 34094
Nosy @brettcannon, @zware

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-07-11.18:47:32.183>
created_at = <Date 2018-07-11.10:42:09.854>
labels = ['invalid', 'docs']
title = 'Porting Python 2 to Python 3 example contradicts its own advice'
updated_at = <Date 2018-07-11.18:47:32.182>
user = 'https://bugs.python.org/BruceRichardson'

bugs.python.org fields:

activity = <Date 2018-07-11.18:47:32.182>
actor = 'brett.cannon'
assignee = 'docs@python'
closed = True
closed_date = <Date 2018-07-11.18:47:32.183>
closer = 'brett.cannon'
components = ['Documentation']
creation = <Date 2018-07-11.10:42:09.854>
creator = 'Bruce Richardson'
dependencies = []
files = []
hgrepos = []
issue_num = 34094
keywords = []
message_count = 3.0
messages = ['321436', '321477', '321484']
nosy_count = 4.0
nosy_names = ['brett.cannon', 'docs@python', 'zach.ware', 'Bruce Richardson']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue34094'
versions = []

@BruceRichardson
Copy link
Mannequin Author

BruceRichardson mannequin commented Jul 11, 2018

https://docs.python.org/3/howto/pyporting.html#use-feature-detection-instead-of-version-detection

In this section, the (very good) advice is "It would be better to treat Python 2 as the exceptional case instead of Python 3 and assume that future Python versions will be more compatible with Python 3 than Python 2"

However, it then goes on to present the best solution (for dealing with library imports) is this:

try:
from importlib import abc
except ImportError:
from importlib2 import abc

This is literally treating Python 3 as the exception, completely contradicting the advice given a few lines earlier. Practically, it also has the effect that, as Python 3 adoption spreads, import errors and automatic retries will become *more* common and then universal, adding a small amount of delay and noise to the entire Python estate. And that's not considering the case where both libraries are installed to cope with old code relying on the old library (in which case you surely want new code to default to using the new library)

If the example is simply changed to

try:
from importlib2 import abc
except ImportError:
from importlib import abc

then both the contradiction and the practical problems go away

@BruceRichardson BruceRichardson mannequin added the docs Documentation in the Doc dir label Jul 11, 2018
@zware
Copy link
Member

zware commented Jul 11, 2018

I don't agree with your conclusion here: importlib2 is a PyPI package that backports Python 3's importlib to Python 2, thus the ImportError will only be raised on Python 2 with the example as written.

@BruceRichardson
Copy link
Mannequin Author

BruceRichardson mannequin commented Jul 11, 2018

Ah, doh. my bad.

On 11 July 2018 at 16:09, Zachary Ware <report@bugs.python.org> wrote:

Zachary Ware <zachary.ware@gmail.com> added the comment:

I don't agree with your conclusion here: importlib2 is a PyPI package that
backports Python 3's importlib to Python 2, thus the ImportError will only
be raised on Python 2 with the example as written.

----------
nosy: +zach.ware


Python tracker <report@bugs.python.org>
<https://bugs.python.org/issue34094\>


@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

2 participants