Pylint across Python versions

T

thomas.lehmann

Hi,

somebody who can tell me about pylint experiences across
different Python version.

Example:
I'm using a construct like this:

if sys.version.startswith("3."):
unicode = str

The reason is that Python 3 does not have this
function anymore but pylint yells for Python < 3
about redefinition also it does not happen.

How to get forward with this?

Regards,
Thomas
 
M

Mark Lawrence

Hi,

somebody who can tell me about pylint experiences across
different Python version.

Example:
I'm using a construct like this:

if sys.version.startswith("3."):
unicode = str

The reason is that Python 3 does not have this
function anymore but pylint yells for Python < 3
about redefinition also it does not happen.

I've no idea what the above is saying but see below anyway.
How to get forward with this?

Regards,
Thomas
'3.4.0b2 (v3.4.0b2:ba32913eb13e, Jan 5 2014, 16:23:43) [MSC v.1600 32
bit (Intel)]'
 
C

Chris Angelico

Example:
I'm using a construct like this:

if sys.version.startswith("3."):
unicode = str

The reason is that Python 3 does not have this
function anymore but pylint yells for Python < 3
about redefinition also it does not happen.

How to get forward with this?

It's more common to spell that with a try/except. Does pylint complain
if you use this instead?

try:
unicode
except NameError:
unicode = str

Although it would be better to write your code for Python 3, and have
compatibility code at the top for Python 2. That would mean using
'str' everywhere, and then having this at the top:

try:
str = unicode
except NameError:
pass

That way, when you're ready to drop support for Python 2, you simply
delete the compat code and everything works. Otherwise, you have to
maintain messy code indefinitely.

Alternatively, to avoid redefinition at all, you could use your own
name everywhere:

try:
unicode_string = unicode
except NameError:
unicode_string = str

Use something less unwieldy if you prefer, but this avoids any chance
of collision :)

ChrisA
 
N

Ned Batchelder

Hi,

somebody who can tell me about pylint experiences across
different Python version.

Example:
I'm using a construct like this:

if sys.version.startswith("3."):
unicode = str

The reason is that Python 3 does not have this
function anymore but pylint yells for Python < 3
about redefinition also it does not happen.

How to get forward with this?

Regards,
Thomas

Pylint may have a difficult time understanding what you are doing here.
You can use pylint comments to tell it to shut up when you know better.

But also, you might find it easier to use the "six" module from PyPI to
handle these sorts of differences. It's easier than doing it ad-hoc
with your own logic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top