In code, list.clear doesn't throw error - it's just ignored

Chris Angelico rosuav at gmail.com
Wed Nov 23 14:28:45 EST 2022


On Thu, 24 Nov 2022 at 06:26, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
>
> Jon Ribbens <jon+usenet at unequivocal.eu> writes:
> >If you want to catch this sort of mistake automatically then you need
> >a linter such as pylint:
>
>   output
>
> <string>, line 1
> list.clear
> Warning: Attribute used as statement.
>
> <string>, line 5
> list.clear
> Warning: Attribute used as statement.
>
>   source code
>
> import ast, sys
>
> def check( point, source ):
>     if isinstance( point, ast.Expr ) and\
>     type( point.value )== ast.Attribute:
>         print( "<string>, line", point.lineno, file=sys.stderr )
>         print( source.split( '\n' )[ point.lineno-1 ], file=sys.stderr )
>         print\
>         ( "Warning:", "Attribute used as statement.", file=sys.stderr )
>         print()
>
> def mylinter( source ):
>     for point in ast.walk( ast.parse( example )):
>        check( point, source )
>
> example = """\
> list.clear
> list.clear()
> x = list.clear
> print( list.clear )
> list.clear
> """
>
> mylinter( example )
>

Uhh, yes? You just created an extremely simplistic linter. Your point?

ChrisA


More information about the Python-list mailing list