[GRASS-dev] [GRASS GIS] #2441: Underscore to avoid Python keywords used improperly in grass.script

#2441: Underscore to avoid Python keywords used improperly in grass.script
-------------------------------------------+--------------------------------
Reporter: wenzeslaus | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.0.0
Component: Default | Version: svn-trunk
Keywords: PEP8, python keywords, parser | Platform: Unspecified
      Cpu: Unspecified |
-------------------------------------------+--------------------------------
When a module parameter is the same as one of Python keywords, we are
adding and underscore before the name of parameter, so call of a module:

{{{
run_command('s.module', lambda="abc")
}}}

becomes

{{{
run_command('s.module', _lambda="abc")
}}}

But this is wrong, according to
[http://legacy.python.org/dev/peps/pep-0008/ PEP8] and commonly used
standard, prefixed underscore means private. To avoid conflicts you should
use underscore at the end:

{{{
_single_leading_underscore: weak "internal use" indicator. E.g. from M
import * does not import objects whose name starts with an underscore.

single_trailing_underscore_: used by convention to avoid conflicts with
Python keyword, e.g.

Tkinter.Toplevel(master, class_='ClassName')
}}}

If there are no objections I will commit the change soon hopefully with
tests and documentation (in case it is not already documented). Please
object now.

I plan to commit in backwards compatible manner, so the old syntax will
work (for some time).

The code which will change is in grass.script.core and will look like
this:

{{{
             if opt.startswith('_'):
                 opt = opt[1:]
             elif opt.endswith('_'):
                 opt = opt[:-1]
}}}

or this:

{{{
             if opt.endswith('_'):
                 opt = opt[:-1]
}}}

This should go to 7.0 to allow usage of the good way.

--
Ticket URL: <http://trac.osgeo.org/grass/ticket/2441&gt;
GRASS GIS <http://grass.osgeo.org>

#2441: Underscore to avoid Python keywords used improperly in grass.script
-------------------------------------------+--------------------------------
Reporter: wenzeslaus | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.0.0
Component: Default | Version: svn-trunk
Keywords: PEP8, python keywords, parser | Platform: Unspecified
      Cpu: Unspecified |
-------------------------------------------+--------------------------------

Comment(by glynn):

Replying to [ticket:2441 wenzeslaus]:

> The code which will change is in grass.script.core and will look like
this:
>
{{{
             if opt.startswith('_'):
                 opt = opt[1:]
             elif opt.endswith('_'):
                 opt = opt[:-1]
}}}

No objections, although `opt = opt.strip('_')` would probably suffice.
GRASS option names will never start or end with an underscore.

--
Ticket URL: <http://trac.osgeo.org/grass/ticket/2441#comment:1&gt;
GRASS GIS <http://grass.osgeo.org>

#2441: Underscore to avoid Python keywords used improperly in grass.script
-------------------------------------------+--------------------------------
Reporter: wenzeslaus | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.0.0
Component: Default | Version: svn-trunk
Keywords: PEP8, python keywords, parser | Platform: Unspecified
      Cpu: Unspecified |
-------------------------------------------+--------------------------------

Comment(by wenzeslaus):

Replying to [comment:1 glynn]:
> No objections, although `opt = opt.strip('_')` would probably suffice.
GRASS option names will never start or end with an underscore.

I will strip just one at the begging or one at the end. I don't want
people to use `__lambda__` or something like this.

I even consider to write to doc that `_lambda` is obsolete (in 7.0) and in
next versions (7.1 or 7.5) I would include a warning. Finally, I would
remove (7.2 or 8.0) the condition completely. This would allow for GRASS
option names starting with underscore which might be used for some
internal purpose (although this is probably not a good idea).

--
Ticket URL: <http://trac.osgeo.org/grass/ticket/2441#comment:2&gt;
GRASS GIS <http://grass.osgeo.org>

#2441: Underscore to avoid Python keywords used improperly in grass.script
-------------------------------------------+--------------------------------
Reporter: wenzeslaus | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.0.0
Component: Default | Version: svn-trunk
Keywords: PEP8, python keywords, parser | Platform: Unspecified
      Cpu: Unspecified |
-------------------------------------------+--------------------------------

Comment(by zarch):

Replying to [comment:2 wenzeslaus]:
> Replying to [comment:1 glynn]:
> > No objections, although `opt = opt.strip('_')` would
> > probably suffice. GRASS option names will never start
> > or end with an underscore.
>
> I will strip just one at the begging or one at the end.
> I don't want people to use `__lambda__` or something like this.

+1

> I even consider to write to doc that `_lambda` is obsolete
> (in 7.0) and in next versions (7.1 or 7.5) I would include
> a warning. Finally, I would remove (7.2 or 8.0) the
> condition completely. This would allow for GRASS option
> names starting with underscore which might be used for
> some internal purpose (although this is probably not a good idea).

Why not include the warning already in grass 7.0 and then clean the code
directly in grass 7.1?
Consider our long release time 7.2 means 6/8 years from now... and it seem
too long to me. :slight_smile:

--
Ticket URL: <http://trac.osgeo.org/grass/ticket/2441#comment:3&gt;
GRASS GIS <http://grass.osgeo.org>

#2441: Underscore to avoid Python keywords used improperly in grass.script
-------------------------------------------+--------------------------------
Reporter: wenzeslaus | Owner: grass-dev@…
     Type: defect | Status: new
Priority: normal | Milestone: 7.0.0
Component: Default | Version: svn-trunk
Keywords: PEP8, python keywords, parser | Platform: Unspecified
      Cpu: Unspecified |
-------------------------------------------+--------------------------------

Comment(by wenzeslaus):

Replying to [comment:3 zarch]:
> Replying to [comment:2 wenzeslaus]:
> > I even consider to write to doc that `_lambda` is obsolete
> > (in 7.0) and in next versions (7.1 or 7.5) I would include
> > a warning. Finally, I would remove (7.2 or 8.0) the
> > condition completely. This would allow for GRASS option
> > names starting with underscore which might be used for
> > some internal purpose (although this is probably not a good idea).
>
> Why not include the warning already in grass 7.0 and then clean the code
directly in grass 7.1?
> Consider our long release time 7.2 means 6/8 years from now... and it
seem too long to me. :slight_smile:

I think that 6 years is average for major version not minor :slight_smile: but yes, I
committed the underscore suffix in r62196 with the warning for prefixed
underscore. This should be backported as is to 70 while in trunk we should
remove the whole prefix part. I plan to do it in few days.

--
Ticket URL: <http://trac.osgeo.org/grass/ticket/2441#comment:4&gt;
GRASS GIS <http://grass.osgeo.org>