[Python-projects] Pylint and gettext. How to reconcile?

Sylvain Thénault sylvain.thenault at logilab.fr
Thu Apr 20 08:33:48 CEST 2006


On Wednesday 19 April à 15:44, Pierre_Rouleau at ImpathNetworks.com wrote:
> Bonjour Sylvain,

Bonjour Pierre, 

> Background
> ===========
> 
> I have several modules with user interface internationalized with gettext that
> uses the _()  convention for translating string:
> 
> a_possibly_translated_string = _(u"Hi there!")
> 
> Now, some of these modules do not import gettext, the gettext is activated by
> another module ran before.
> Inside these auxiliary modules, the _() function is mapped to the gettext string
> mapping at run time, but Pylint does not know that and generates a E0602 warning
> that complains about the undefined variable '_'.
> 
> One way to get rid of the warning is to detect Pylint processing and define a
> dummy function locally::
> 
> import os
> if os.environ.has_key('PYLINT_IMPORT'):
>     def _(astring):
>         "No i18n suring pylint checking"
>         return astring
> 
> 
> Since several of these modules also contain doctest support, I normally place
> the following code inside the module, near the top:
> 
> # Provide a default implementation of _() in case gettext is not active when the
>  module is imported.
> try:
>     _("test")
> except NameError:
>     def _(a_string):
>         "Default implementation of the gettext string translation function: no
> translation"
>         return a_string
> except TypeError:
>     # during doctesting, _ might mean something else
>     pass
> 
> 
> Question
> ========
> 
> Is there a easier way to accomplish the same (preventing Pylint E0602 warning)
> in those modules?
> Ideally, the solution would not require any extra code in the tested module.
> 
> I use the following dummy-variable-rgx but it does not filter those messages.
> 
> dummy-variables-rgx=_|dummy|unused_arg|event|unused_.*

you can use the additional-builtins option of the variables checker:

additional-builtins = _, otherdynamicfunction

-- 
Sylvain Thénault                               LOGILAB, Paris (France).

http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org




More information about the Python-Projects mailing list