Skip to content

Commit

Permalink
Useful pylint fixes
Browse files Browse the repository at this point in the history
After combing through all the stuff pylint has found, this seems to me
the useful part.

The only questionable thing here, I think, could be the stuff related to
`open()` and explicit encoding argument. This changes behaviour!
Without specifying, the encoding is platform dependent. See
https://docs.python.org/3/library/functions.html#open for details.

Fixes ycm-core#1672
  • Loading branch information
bstaletic committed Oct 7, 2023
1 parent adce5d8 commit 376fba1
Show file tree
Hide file tree
Showing 29 changed files with 66 additions and 81 deletions.
4 changes: 1 addition & 3 deletions .ycm_extra_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
# For more information, please refer to <http://unlicense.org/>

from sysconfig import get_path
import platform
import os.path as p
import subprocess

DIR_OF_THIS_SCRIPT = p.abspath( p.dirname( __file__ ) )
DIR_OF_THIRD_PARTY = p.join( DIR_OF_THIS_SCRIPT, 'third_party' )
Expand Down Expand Up @@ -120,7 +118,7 @@ def FindCorrespondingSourceFile( filename ):
def PathToPythonUsedDuringBuild():
try:
filepath = p.join( DIR_OF_THIS_SCRIPT, 'PYTHON_USED_DURING_BUILDING' )
with open( filepath ) as f:
with open( filepath, encoding = 'utf8' ) as f:
return f.read().strip()
except OSError:
return None
Expand Down
13 changes: 6 additions & 7 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def FindLatestMSVC( quiet ):
).strip().decode()
if '.' in latest_full_v:
try:
latest_v = int( latest_full_v.split( '.' )[ 0 ] )
latest_v = int( latest_full_v.split( '.', 1 )[ 0 ] )
except ValueError:
raise ValueError( f"{ latest_full_v } is not a version number." )

Expand Down Expand Up @@ -464,10 +464,10 @@ def ParseArguments():
DEFAULT_RUST_TOOLCHAIN + '" is tested/'
'supported by the maintainers of YCM/ycmd' )
parser.add_argument( '--java-completer', action = 'store_true',
help = 'Enable Java semantic completion engine.' ),
help = 'Enable Java semantic completion engine.' )
parser.add_argument( '--ts-completer', action = 'store_true',
help = 'Enable JavaScript and TypeScript semantic '
'completion engine.' ),
'completion engine.' )
parser.add_argument( '--system-libclang', action = 'store_true',
help = 'Use system libclang instead of downloading one '
'from llvm.org. NOT RECOMMENDED OR SUPPORTED!' )
Expand Down Expand Up @@ -834,7 +834,6 @@ def CleanCsCompleter( build_dir, version ):
if os.path.isfile( file_path ):
os.remove( file_path )
elif os.path.isdir( file_path ):
import shutil
shutil.rmtree( file_path )


Expand Down Expand Up @@ -953,14 +952,14 @@ def EnableGoCompleter( args ):

def WriteToolchainVersion( version ):
path = p.join( RUST_ANALYZER_DIR, 'TOOLCHAIN_VERSION' )
with open( path, 'w' ) as f:
with open( path, 'w', encoding = 'utf8' ) as f:
f.write( version )


def ReadToolchainVersion():
try:
filepath = p.join( RUST_ANALYZER_DIR, 'TOOLCHAIN_VERSION' )
with open( filepath ) as f:
with open( filepath, encoding = 'utf8' ) as f:
return f.read().strip()
except OSError:
return None
Expand Down Expand Up @@ -1234,7 +1233,7 @@ def Print( msg ):

def WritePythonUsedDuringBuild():
path = p.join( DIR_OF_THIS_SCRIPT, 'PYTHON_USED_DURING_BUILDING' )
with open( path, 'w' ) as f:
with open( path, 'w', encoding = 'utf8' ) as f:
f.write( sys.executable )


Expand Down
4 changes: 2 additions & 2 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ def BuildYcmdLibs( args ):
'--core-tests'
]

for key in COMPLETERS:
for key, value in COMPLETERS.items():
if key in args.completers:
build_cmd.extend( COMPLETERS[ key ][ 'build' ] )
build_cmd.extend( value[ 'build' ] )

if args.msvc and platform.system() == 'Windows':
build_cmd.extend( [ '--msvc', str( args.msvc ) ] )
Expand Down
2 changes: 1 addition & 1 deletion update_clang_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def ExtractTar( uncompressed_data, destination ):
tar_file.extractall( destination )

# Determine the directory name
return os.path.join( destination, a_member.name.split( '/' )[ 0 ] )
return os.path.join( destination, a_member.name.split( '/', 1 )[ 0 ] )


def ExtractLZMA( compressed_data, destination ):
Expand Down
4 changes: 2 additions & 2 deletions update_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def GenerateNormalizationTestCases( output_file ):
JoinUnicodeToUtf8( captures[ 4 ].split() ) + '"},\n' )

res[ -1 ] = res[ -1 ].rstrip( ',\n' )
with open( output_file, 'w' ) as f:
with open( output_file, 'w', encoding = 'utf8' ) as f:
f.writelines( res )


Expand All @@ -602,7 +602,7 @@ def GenerateGraphemeBreakTestCases( output_file ):
for x in split_data ] ).rstrip( ',' ) + '}},\n' )

res[ -1 ] = res[ -1 ].rstrip( ',\n' )
with open( output_file, 'w' ) as f:
with open( output_file, 'w', encoding = 'utf8' ) as f:
f.writelines( res )


Expand Down
2 changes: 1 addition & 1 deletion ycmd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def ParseArguments():
def SetupLogging( log_level ):
numeric_level = getattr( logging, log_level.upper(), None )
if not isinstance( numeric_level, int ):
raise ValueError( 'Invalid log level: %s' % log_level )
raise ValueError( f'Invalid log level: { log_level }' )

# Has to be called before any call to logging.getLogger()
logging.basicConfig( format = '%(asctime)s - %(levelname)s - %(message)s',
Expand Down
4 changes: 2 additions & 2 deletions ycmd/completers/cpp/clang_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,9 +436,9 @@ def DebugInfo( self, request_data ):

database_item = responses.DebugInfoItem(
key = 'compilation database path',
value = '{0}'.format( database_directory ) )
value = f'{ database_directory }' )
flags_item = responses.DebugInfoItem(
key = 'flags', value = '{0}'.format( list( flags ) ) )
key = 'flags', value = f'{ list( flags ) }' )
filename_item = responses.DebugInfoItem(
key = 'translation unit', value = filename )

Expand Down
3 changes: 1 addition & 2 deletions ycmd/completers/cpp/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,13 +673,12 @@ def UserIncludePaths( user_flags, filename ):
it = iter( user_flags )
for user_flag in it:
user_flag_len = len( user_flag )
for flag in include_flags:
for flag, container in include_flags.items():
if user_flag.startswith( flag ):
flag_len = len( flag )
include_path = ( next( it ) if user_flag_len == flag_len else
user_flag[ flag_len: ] )
if include_path:
container = include_flags[ flag ]
container.append( ToUnicode( include_path ) )
break
except StopIteration:
Expand Down
2 changes: 1 addition & 1 deletion ycmd/completers/cs/cs_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def _GetSolutionFile( self, filepath ):
return self._solution_for_file[ filepath ]


class CsharpSolutionCompleter( object ):
class CsharpSolutionCompleter:
def __init__( self,
solution_path,
keep_logfiles,
Expand Down
10 changes: 5 additions & 5 deletions ycmd/completers/language_server/generic_lsp_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def __init__( self, user_options, server_settings ):
cmd = utils.FindExecutable( self._command_line[ 0 ] )

if cmd is None:
utils.LOGGER.warn( "Unable to find any executable with the path %s. "
"Cannot use %s completer.",
self._command_line[ 0 ],
self._name )
raise RuntimeError( f"Invalid cmdline: { str( self._command_line ) }" )
utils.LOGGER.warning( "Unable to find any executable with the path %s. "
"Cannot use %s completer.",
self._command_line[ 0 ],
self._name )
raise RuntimeError( f"Invalid cmdline: { self._command_line }" )

self._command_line[ 0 ] = cmd
for idx in range( len( self._command_line ) ):
Expand Down
12 changes: 6 additions & 6 deletions ycmd/completers/language_server/language_server_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,8 @@ def _ReadHeaders( self, data ):
key, value = utils.ToUnicode( line ).split( ':', 1 )
headers[ key.strip() ] = value.strip()
except Exception:
LOGGER.exception( 'Received invalid protocol data from server: '
+ str( line ) )
LOGGER.exception(
'Received invalid protocol data from server: %s', line )
raise

read_bytes += 1
Expand Down Expand Up @@ -1096,7 +1096,7 @@ def _StartServerNoLock( self, request_data ):
self._project_directory,
lambda globs: WatchdogHandler( self, globs ),
self._port,
lambda request: self.WorkspaceConfigurationResponse( request ),
self.WorkspaceConfigurationResponse,
self.GetDefaultNotificationHandler() )
else:
self._stderr_file = utils.CreateLogfile(
Expand All @@ -1116,7 +1116,7 @@ def _StartServerNoLock( self, request_data ):
lambda globs: WatchdogHandler( self, globs ),
self._server_handle.stdin,
self._server_handle.stdout,
lambda request: self.WorkspaceConfigurationResponse( request ),
self.WorkspaceConfigurationResponse,
self.GetDefaultNotificationHandler() )
)

Expand Down Expand Up @@ -2865,7 +2865,7 @@ def ExecuteCommand( self, request_data, args ):
if response is not None:
return response

if len( edits ):
if edits:
fixits = [ WorkspaceEditToFixIt(
request_data,
e[ 'edit' ],
Expand Down Expand Up @@ -3190,7 +3190,7 @@ def _SymbolInfoListToGoTo( request_data, symbols ):
"""Convert a list of LSP SymbolInformation into a YCM GoTo response"""

def BuildGoToLocationFromSymbol( symbol ):
location, line_value = _LspLocationToLocationAndDescription(
location, _ = _LspLocationToLocationAndDescription(
request_data,
symbol[ 'location' ] )

Expand Down
9 changes: 0 additions & 9 deletions ycmd/completers/python/python_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,3 @@ def _OffsetToPosition( start_end, filename, text, newlines ):
if len( loc ) == 2:
break
return loc

# Invalid position - it's outside of the text. Just return the last
# position in the text. This is an internal error.
LOGGER.error( "Invalid offset %s in file %s with text %s and newlines %s",
offset,
filename,
text,
newlines )
raise RuntimeError( "Invalid file offset in diff" )
7 changes: 3 additions & 4 deletions ycmd/completers/typescript/typescript_completer.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def _ReaderLoop( self ):
LOGGER.info( 'Received %s event from TSServer', eventname )
continue
if msgtype != 'response':
LOGGER.error( 'Unsupported message type', msgtype )
LOGGER.error( 'Unsupported message type: %s', msgtype )
continue

seq = message[ 'request_seq' ]
Expand Down Expand Up @@ -373,9 +373,8 @@ def _Reload( self, request_data ):

filename = request_data[ 'filepath' ]
contents = request_data[ 'file_data' ][ filename ][ 'contents' ]
tmpfile = NamedTemporaryFile( delete = False )
tmpfile.write( utils.ToBytes( contents ) )
tmpfile.close()
with NamedTemporaryFile( delete = False ) as tmpfile:
tmpfile.write( utils.ToBytes( contents ) )
self._SendRequest( 'reload', {
'file': filename,
'tmpfile': tmpfile.name
Expand Down
2 changes: 1 addition & 1 deletion ycmd/tests/clang/flags_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def Settings( **kwargs ):
assert_that( filename, equal_to( '/foo' ) )


def test_FlagsForFile_BadNonUnicodeFlagsAreAlsoRemoved( *args ):
def test_FlagsForFile_BadNonUnicodeFlagsAreAlsoRemoved( self, *args ):
flags_object = flags.Flags()

def Settings( **kwargs ):
Expand Down
4 changes: 2 additions & 2 deletions ycmd/tests/clang/include_cache_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def test_IncludeCache_Cached_NewMtime( self ):
include_cache = IncludeCache()
assert_that( include_cache._cache, equal_to( {} ) )
foo_path = os.path.join( tmp_dir, 'foo' )
with open( foo_path, 'w' ) as foo_file:
with open( foo_path, 'w', encoding = 'utf8' ) as foo_file:
foo_file.write( 'foo' )

old_includes = include_cache.GetIncludes( tmp_dir )
Expand All @@ -124,7 +124,7 @@ def test_IncludeCache_Cached_NewMtime( self ):
sleep( 2 )

bar_path = os.path.join( tmp_dir, 'bar' )
with open( bar_path, 'w' ) as bar_file:
with open( bar_path, 'w', encoding = 'utf8' ) as bar_file:
bar_file.write( 'bar' )

new_includes = include_cache.GetIncludes( tmp_dir )
Expand Down
2 changes: 1 addition & 1 deletion ycmd/tests/clangd/debug_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def test_DebugInfo_ExtraConf_UseLocalOverDatabase( self, app ):

with TemporaryClangProject( tmp_dir, database ):
extra_conf = os.path.join( tmp_dir, '.ycm_extra_conf.py' )
with open( extra_conf, 'w' ) as f:
with open( extra_conf, 'w', encoding = 'utf8' ) as f:
f.write( '''
def Settings( **kwargs ):
return { 'flags': [ '-x', 'c++', '-I', 'ycm' ] }
Expand Down
12 changes: 6 additions & 6 deletions ycmd/tests/clangd/diagnostics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,19 +474,19 @@ def test_Diagnostics_UpdatedOnBufferVisit( self, app ):
source_contents = """#include "header.h"
int main() {return S::h();}
"""
with open( source_file, 'w' ) as sf:
with open( source_file, 'w', encoding = 'utf8' ) as sf:
sf.write( source_contents )

header_file = os.path.join( tmp_dir, 'header.h' )
old_header_content = """#pragma once
struct S{static int h();};
"""
with open( header_file, 'w' ) as hf:
with open( header_file, 'w', encoding = 'utf8' ) as hf:
hf.write( old_header_content )

flags_file = os.path.join( tmp_dir, 'compile_flags.txt' )
flags_content = """-xc++"""
with open( flags_file, 'w' ) as ff:
with open( flags_file, 'w', encoding = 'utf8' ) as ff:
ff.write( flags_content )

messages_request = { 'contents': source_contents,
Expand All @@ -502,7 +502,7 @@ def test_Diagnostics_UpdatedOnBufferVisit( self, app ):
new_header_content = """#pragma once
static int h();
"""
with open( header_file, 'w' ) as f:
with open( header_file, 'w', encoding = 'utf8' ) as f:
f.write( new_header_content )

# Send BufferSaved notification for the header
Expand Down Expand Up @@ -548,7 +548,7 @@ def test_Diagnostics_UpdatedOnBufferVisit( self, app ):
break

# Restore original content
with open( header_file, 'w' ) as f:
with open( header_file, 'w', encoding = 'utf8' ) as f:
f.write( old_header_content )

# Send BufferSaved notification for the header
Expand All @@ -571,5 +571,5 @@ def test_Diagnostics_UpdatedOnBufferVisit( self, app ):
break

# Assert no dirty files
with open( header_file, 'r' ) as f:
with open( header_file, 'r', encoding = 'utf8' ) as f:
assert_that( f.read(), equal_to( old_header_content ) )
2 changes: 1 addition & 1 deletion ycmd/tests/clangd/utilities_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_ClangdCompleter_GetClangdCommand_CustomBinary( self ):
( 13, 0, 0 ),
( 13, 10, 10 ),
( 100, 100, 100 ) ] )
def test_ClangdCompleter_CheckClangdVersion( *args ):
def test_ClangdCompleter_CheckClangdVersion( self, *args ):
assert_that( clangd_completer.CheckClangdVersion( 'clangd' ),
equal_to( True ) )
assert_that( clangd_completer.CheckClangdVersion( 'clangd' ),
Expand Down
4 changes: 2 additions & 2 deletions ycmd/tests/cs/debug_info_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ def test_GetCompleter_RoslynFound( self ):

@patch( 'ycmd.completers.cs.cs_completer.PATH_TO_OMNISHARP_ROSLYN_BINARY',
None )
def test_GetCompleter_RoslynNotFound( *args ):
def test_GetCompleter_RoslynNotFound( self, *args ):
assert_that( not GetCompleter( user_options_store.GetAll() ) )


@patch( 'ycmd.completers.cs.cs_completer.FindExecutableWithFallback',
wraps = lambda x, fb: x if x == 'roslyn' else fb )
@patch( 'os.path.isfile', return_value = True )
def test_GetCompleter_RoslynFromUserOption( *args ):
def test_GetCompleter_RoslynFromUserOption( self, *args ):
user_options = user_options_store.GetAll().copy(
roslyn_binary_path = 'roslyn' )
assert_that( GetCompleter( user_options )._roslyn_path,
Expand Down
Loading

0 comments on commit 376fba1

Please sign in to comment.